PHP Thumb » Plugins » Plugin Development

Plugin: Watermarking Issue

(7 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 1 year ago #
  2. I'll take a look into it and get back to you :)

    Posted 1 year 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 1 year ago #
  4. shockerusa
    Member

    It works! Thank you!

    Posted 1 year ago #
  5. Any time :)

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

    Posted 1 year 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 11 months ago #

RSS feed for this topic

Reply

You must log in to post.