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!