Page 1 of 1

PNG Watermarking Not Quite Working

Posted: Sat Jul 12, 2008 10:48 pm
by paqman
I'm trying to get a script to automatically watermark images uploaded through a form with a png on the server. The problem I'm running into is that when the script copies the watermark onto the image, anywhere that the watermark doesn't cover is replaced with solid black:

Watermarked:
http://fridasaltiel.com/admin/watermark ... sample.jpg
Not watermarked:
http://fridasaltiel.com/admin/sample.jpg

I think it has something to do with the imagecreatetruecolor()... Any help would be greatly appreciated!

Code: Select all

<? header('content-type: image/jpeg');  
 
$watermark = imagecreatefrompng('watermark.png'); 
$size = getimagesize($_GET['src']);   
$watermark_width = $size[0];//imagesx($watermark);  
$watermark_height = imagesy($watermark);  
$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefromjpeg($_GET['src']);  
$dest_x = 0;  
$dest_y = 0;
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $size[0], $size[1]);  
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark); 
?>
 

Re: PNG Watermarking Not Quite Working

Posted: Sun Jul 13, 2008 6:48 am
by onion2k
You're not copying the area of the watermark, you're copying the area of the destination image.

Code: Select all

$size = getimagesize($_GET['src']);
That gets the dimensions of the destination image ... you then copy the watermark to the destination using

Code: Select all

imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $size[0], $size[1]);
The $size values are going to be wrong. If your watermark image is 300*400 for example, you're copying 400*600 from it... the extra space is going to be solid black because there's no image data for it.

imagecreatetruecolor() isn't actually used because you overwrite $image with the imagecreatefromjpeg() call on the next line.