PNG Watermarking Not Quite Working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

PNG Watermarking Not Quite Working

Post 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); 
?>
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PNG Watermarking Not Quite Working

Post 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.
Post Reply