PHP Thumb » Plugins » Plugin Development

Plugin: Watermarking Issue

(8 posts)
  1. shockerusa
    Member

    Hello,

    I'm trying to create an plugin to watermark images, however i'm running into a problem.

    My goal is to keep the orginal size images and just watermark them, while also creating thumbnails of them.
    The problem is when I'm trying to watermark them and save them, php throws an warning.

    Here is my code for the plugin:


    class GdWatermarkLib
    {
    /**
    * Instance of GdThumb passed to this class
    *
    * @var GdThumb
    */
    protected $parentInstance;
    protected $currentDimensions;
    protected $workingImage;
    protected $newImage;
    protected $options;

    public function createWatermark ($watermark, &$that)
    {
    // bring stuff from the parent class into this class...
    $this->parentInstance = $that;
    $this->currentDimensions = $this->parentInstance->getCurrentDimensions();
    $this->workingImage = $this->parentInstance->getWorkingImage();

    $width = $this->currentDimensions['width'];
    $height = $this->currentDimensions['height'];

    $watermarksize = getimagesize($watermark);
    $dest_x = $width - $watermarksize[0] - 5;
    $dest_y = $height - $watermarksize[1] - 5;

    $watermark = imagecreatefromjpeg($watermark);
    imagecopymerge($this->workingImage, $watermark, $dest_x, $dest_y, 0, 0, $watermarksize[0], $watermarksize[1], 100);

    return $that;
    }

    }

    $pt = PhpThumb::getInstance();
    $pt->registerPlugin('GdWatermarkLib', 'gd');

    Here are my lines of code to call it.


    require_once 'functions/ThumbLib.inc.php';

    $thumb = PhpThumbFactory::create('test.jpg');

    $thumb->createWatermark('watermark.jpg')->save('watermarked_image.jpg');

    The code above just takes the image, applies the watermark and saves it.

    When I run the code above I get

    Warning: imagecopymerge(): supplied argument is not a valid Image resource in C:\AppServ\www\imagetest\thumb_plugins\gd_watermark.inc.php on line 64

    However, if I try to resize it before applying the watermark, it works fine. See code below.


    require_once 'functions/ThumbLib.inc.php';

    $thumb = PhpThumbFactory::create('test.jpg');

    $thumb->resize(600, 600)->createWatermark('watermark.jpg')->save('watermarked_image.jpg');

    Environment:
    Windows XP
    Apache 2
    PHP Version 5.2.1
    GD Version 2.0.28

    Do you have any ideas what might be causing that?

    Thanks,

    Justin

    P.S Awesome script!

    Posted 2 years ago #
  2. I'll take a look into it and get back to you :)

    Posted 2 years ago #
  3. Justin, got a fix for you... here's the code I got working:
    <?php
    class GdWatermarkLib
    {
    /**
    * Instance of GdThumb passed to this class
    *
    * @var GdThumb
    */
    protected $parentInstance;
    protected $currentDimensions;
    protected $workingImage;
    protected $newImage;
    protected $options;

    public function createWatermark ($watermark, &$that)
    {
    // bring stuff from the parent class into this class...
    $this->parentInstance = $that;
    $this->currentDimensions = $this->parentInstance->getCurrentDimensions();

    $width = $this->currentDimensions['width'];
    $height = $this->currentDimensions['height'];

    $watermarksize = getimagesize($watermark);
    $dest_x = $width - $watermarksize[0] - 5;
    $dest_y = $height - $watermarksize[1] - 5;
    $watermark = imagecreatefromjpeg($watermark);

    imagecopymerge($this->parentInstance->getOldImage(), $watermark, $dest_x, $dest_y, 0, 0, $watermarksize[0], $watermarksize[1], 100);

    return $that;
    }
    }

    $pt = PhpThumb::getInstance();
    $pt->registerPlugin('GdWatermarkLib','gd');

    And I tested it with:

    $thumb = PhpThumbFactory::create('test.jpg');
    $thumb->createWatermark('watermark.jpg')->show();

    And just for fun (to make sure everything works together):

    $thumb = PhpThumbFactory::create('test.jpg');
    $thumb->createWatermark('watermark.jpg')->createReflection(40, 40, 80, true, '#a4a4a4')->show();

    Posted 2 years ago #
  4. shockerusa
    Member

    It works! Thank you!

    Posted 2 years ago #
  5. Any time :)

    Posted 2 years ago #
  6. Could you extend it with alpha blending png support?
    Thanx in advance :)

    Posted 2 years ago #
  7. Use

    class GdWatermarkLib
    {
    /**
    * Instance of GdThumb passed to this class
    *
    * @var GdThumb
    */
    protected $parentInstance;
    protected $currentDimensions;
    protected $workingImage;
    protected $newImage;
    protected $options;

    public function createWatermark ($watermark, $that)
    {
    // bring stuff from the parent class into this class...
    $this->parentInstance = $that;
    $this->currentDimensions = $this->parentInstance->getCurrentDimensions();

    $width = $this->currentDimensions['width'];
    $height = $this->currentDimensions['height'];

    $watermarksize = getimagesize($watermark);
    $dest_x = $width - $watermarksize[0] - 5;
    $dest_y = $height - $watermarksize[1] - 5;
    //$watermark = imagecreatefrompng($watermark);

    $pathinfo = pathinfo($watermark);
    $var1 = $pathinfo['extension'];
    $var2 = "png";
    $var3 = "jpeg";
    $var4 = "jpg";
    $var5 = "gif";
    if(strcasecmp($var1, $var2) == 0){
    $watermark = @imagecreatefrompng($watermark);
    }elseif((strcasecmp($var1, $var3) == 0) || (strcasecmp($var1, $var4) == 0)){
    $watermark = @imagecreatefromjpeg($watermark);
    }elseif(strcasecmp($var1, $var5) == 0){
    $watermark = @imagecreatefromgif($watermark);
    }

    imagecopy($this->parentInstance->getOldImage(), $watermark, $dest_x, $dest_y, 0, 0, $watermarksize[0], $watermarksize[1]);

    return $that;
    }
    }

    $pt = PhpThumb::getInstance();
    $pt->registerPlugin('GdWatermarkLib','gd');

    for all image-Types

    Posted 2 years ago #
  8. ecarlevaro
    Member

    Hi! I`m having troubles with memory limit when i use the watermark plugin with 60 images of 700x600 pixeles at 150 KB each image (and the watermark is only a PNG image of 1.32 KB).

    In fact, with 60 images, memory_get_usage() say is in use 67 MB of memory!!!.

    I'm not sure it is possible optimize the watermark plugin code.

    The code i'm using is similar:

    <?php
    class GdWatermarkLib
    {
     /**
     * Instance of GdThumb passed to this class
     *
     * @var GdThumb
     */
     protected $parentInstance;
     protected $currentDimensions;
     protected $workingImage;
     protected $newImage;
     protected $options;
    
     public function createWatermark ($watermark, $mask_position, $mask_padding, $that)
     {
     // bring stuff from the parent class into this class...
     $this->parentInstance = $that;
     $this->currentDimensions = $this->parentInstance->getCurrentDimensions();
     $this->mask_position = $mask_position;
    
     $width = $this->currentDimensions['width'];
     $height = $this->currentDimensions['height'];
    
     $watermarksize = getimagesize($watermark);
     $dest_x = $width - $watermarksize[0] - 55;
     $dest_y = $height - $watermarksize[1] - 55;
     //$watermark = imagecreatefrompng($watermark);
    
     $pathinfo = pathinfo($watermark);
     $var1 = $pathinfo['extension'];
     $var2 = "png";
     $var3 = "jpeg";
     $var4 = "jpg";
     $var5 = "gif";
     if(strcasecmp($var1, $var2) == 0){
     $watermark = @imagecreatefrompng($watermark);
     }elseif((strcasecmp($var1, $var3) == 0) || (strcasecmp($var1, $var4) == 0)){
     $watermark = @imagecreatefromjpeg($watermark);
     }elseif(strcasecmp($var1, $var5) == 0){
     $watermark = @imagecreatefromgif($watermark);
     }
    
     switch($mask_position) {
      case 'cc':
      // Center
      $dest_x = round(($width - $watermarksize[0]) / 2);
      $dest_y = round(($height - $watermarksize[1]) / 2);
      break;
      case 'lt':
      // Left Top
      $dest_x = $mask_padding;
      $dest_y = $mask_padding;
      break;
      case 'rt':
      // Right Top
      $dest_x = $width - $mask_padding - $watermarksize[0];
      $dest_y = $mask_padding;
      break;
      case 'lb':
      // Left Bottom
      $dest_x = $mask_padding;
      $dest_y = $height - $mask_padding - $watermarksize[1];
      break;
      case 'rb':
      // Right Bottom
      $dest_x = $width - $mask_padding - $watermarksize[0];
      $dest_y = $height - $mask_padding - $watermarksize[1];
      break;
      case 'cb':
      // Center Bottom
      $dest_x = round(($width - $watermarksize[0]) / 2);
      $dest_y = $height - $mask_padding - $watermarksize[1];
      break;
      }
    
     imagecopy($this->parentInstance->getOldImage(), $watermark, $dest_x, $dest_y, 0, 0, $watermarksize[0], $watermarksize[1]);
    
     return $that;
     }
    }
    
    $pt = PhpThumb::getInstance();
    $pt->registerPlugin('GdWatermarkLib','gd');
    
    ?>

    If there is possible optimize something in this code, i will be grateful.

    I just need write text on the image, so i'm using watermark plugin but if there is another GD function to write text over the image it will be useful to me

    Posted 11 months ago #

RSS feed for this topic

Reply

You must log in to post.