Watermark Images on the Fly in PHP

This is an article discussion thread for discussing the SitePoint article, “Watermark Images on the Fly in PHP

Two words: YOU ROCK!

Two words: Thank you :slight_smile:

That is one of the best articles I have read in watermarking. Excellent resource. Thank you very much.

its not that amazing! :slight_smile:
great article…also interesting to try with TTF fonts for dynamic text.

Can anyone that has had success with the above please advise what version of GD and what version of libpng you are running…?

Great tutorial btw, shame i cant get it working because my version of libpng is incompattible.

Thanks

For larger or more heavily trafficked sites, I imagine this could begin to take a toll on performance if a watermarked image is reassembled every time it’s requested.

The next step for this script would be to write the assembled image to a cache directory, similar to how some weblog apps write static copies of their entries. Could be called as a cron job or upon uploading a new image or etc…

Nice script, I’d been contemplating doing something like this both for watermarks and borders.

This is great, but I have a question.

I would like to use a png with a transparent bg as my watermark image, but the result is black pixels where the watermark’s transparency should be. Is this a feature of my version of GD/PHP/whatever, or is it a limitation of the basic script source (as supplied)?

TIA

Maybe try


<?php  

header('content-type: image/jpeg');  

$watermark = imagecreatefrompng('watermark.png');  
$imgtran = imagecolorallocate($image, 0, 0, 0); //whatever the rgb transparent color is
imagecolortransparent($watermark, $imgtran); //make that color transparent
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);  
$image = imagecreatetruecolor($watermark_width, $watermark_height);  
//$image = imagecreatefromjpeg($_GET['src']);  
$image = imagecreatefrompng($_GET['src']);
$size = getimagesize($_GET['src']);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;  
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagetruecolortopalette($image, true, 256); //use this to force 256 bit transparent pngs to show up in IE.
//imagejpeg($image);  
imagepng($image);
imagedestroy($image);  
imagedestroy($watermark);  

?>

if your getting black pixel you need php 4.3.3 at least for gd 2

Hi Brock,
great article, but I was not able to get it to work. Bear with me as I am not very knowlagble with php…

but from your article, I assume we have to save the script (e.g. image.php) and then call the image to be watermarked as image.php?image=pic.jpg

but when i do this i get this error:

<br />
<b>Warning</b>: imagecopymerge(): supplied argument is not a valid Image resource in <b>/home/httpd/vhosts/domain.com/httpdocs/image.php</b> on line <b>13</b><br />
<br />
<b>Warning</b>: imagejpeg(): supplied argument is not a valid Image resource in <b>/home/httpd/vhosts/domain.com/httpdocs/image.php</b> on line <b>14</b><br />
<br />
<b>Warning</b>: imagedestroy(): supplied argument is not a valid Image resource in <b>/home/httpd/vhosts/domain.com/httpdocs/image.php</b> on line <b>15</b><br />

all the files are in the same folder. Any idea what i am doing wrong?

The default way the script works is by giving the $_GET[‘src’] variable (?src=value] with value being the complete path to the file (/home/user/public_html/images/file.jpg for example).

Heh, I just thinking about watermarking some images of my site that are getting heavily linked to, and voila, where’s a quick script.

But this is just a first step. What I really want to do, is develop this script so that it looks at the requests and determines is it from somewhere outside the website? Or is from within? Referrer value is what I want know.

If it’s from within, I would rather not “stain” the pictures with my “url” and copy write information. But if it’s an external link, well, I’d like to at least let them know from where the picture their viewing, came from.

Any thoughts on this?

Thanx

In The PHP Anthology, HarryF deals with the hot-linking issue using sessions. Good book and base classes to build on, if you do the whole oop php thing. Still having trouble with 5 though (install that is).

I have used this class as a base to what i ended up making an overly robust thumbnail generation monstrosity…but it was fun.

In your code:

$image = imagecreatetruecolor($watermark_width, $watermark_height);

$image = imagecreatefromjpeg($_GET[‘src’]);

Why are you doing both these lines one after the other? Isn’t that just a waist of CPU time as the $image created in the 1st line is overwritten by the next call?

This works all fine and dandy, but why doesn’t it work when you use it in a html table?

Why does everyone always use pngs in these tutorials, why would I want to use that format when every image I have is either a jpeg or gif! My digital camera only outputs jpeg… just some food for thought

You can use jpegs to create images on the fly. The png is only used as the watermark on your image. This is generally due to the transparent background. This looks nice on images

This script actually doesn’t work for me. I’m not sure if Photoshop created .png files have terrible transparency or what, but the watermark uses white where it should be transparent. I have GD2.0+. Any suggestions?

Great Article. I love the ability to watermark in PHP. I have been using it for a while now. Works great! Here is an example of the output is creates:

The logo and Copyright info is the Watermark.