This is an article discussion thread for discussing the SitePoint article, "Watermark Images on the Fly in PHP"
| SitePoint Sponsor |
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![]()
Brock Ferguson
Lead Developer, Caribou CMS
A Subscription/Membership CMS and Ecommerce Platform - FREE Trial




That is one of the best articles I have read in watermarking. Excellent resource. Thank you very much.
its not that amazing! :)
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
Your mind is like a parachute. It works best when open.
(HH The Dalai Lama)
Maybe tryOriginally Posted by Chillijam
PHP Code:<?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.
.:Blog:.


DocDave,
Any chance you could post the code you actually use, please? I still haven't managed to sort out my issues, mainly because I have very little time to do it.
TIA
CJ
Your mind is like a parachute. It works best when open.
(HH The Dalai Lama)

Ok here you go, the variable $photo_file is the file that was uploaded earlier in the routine. There is other stuff in the routine to size the image and what not. Hope this helps.
PHP Code:// Get the uploaded photos dimensions
$photo_size = getimagesize($photo_file);
$photo_width = $photo_size[0];
$photo_height = $photo_size[1];
// Create an image from the uploaded jpg
$photo_image = imagecreatefromjpeg($photo_file);
// Turn on Alpha Blending for the uploaded jpg
ImageAlphaBlending($photo_image, true);
// Check that the uploaded photo's width is larger than the
// requested fullsize photo width, otherwise use the original width
if ($photo_width > $INFO['max_img_width'])
{
$pic_width = $INFO['max_img_width'];
}
else
{
$pic_width = $photo_width;
}
// Calculate the the fullsize photo height based on the width
$pic_height = round($photo_height / ($photo_width / $pic_width));
// Create the new image
$pic_img = imagecreatetruecolor($pic_width,$pic_height);
imagecopyresized($pic_img,$photo_image,0,0,0,0,$pic_width,$pic_height,$photo_width,$photo_height);
// Define the watermark png file
$logo_file = $INFO['server_path']."/".$INFO['gallery_dir']."/htm/watermark.png";
// Get the logo dimensions from the file
$logo_size = getimagesize($logo_file);
$logo_width = $logo_size[0];
$logo_height = $logo_size[1];
// Create an image from the watermark png file
$logo_image = ImageCreateFromPNG($logo_file);
// Copy watermark logo image onto the photo image
ImageCopy($pic_img, $logo_image, $INFO['mark_x'], $INFO['mark_y'], 0, 0, $logo_width, $logo_height);
// Define the location of the jpg file to be created
$pic_file = $INFO['server_path']."/".$INFO['gallery_dir']."/pic/".$save_as_name;
// Create and store a jpg at the fullsize pic quality from the resized image created
imagejpeg($pic_img, $pic_file, $INFO['img_quality']);
// Clean-up any other left over images
ImageDestroy($photo_image);
ImageDestroy($logo_image);
ImageDestroy($pic_img);
![]()
.:Blog:.


Perfection! Thanks Dave
Your mind is like a parachute. It works best when open.
(HH The Dalai Lama)
I'm stuck with the following issue:
I've got two servers, one (free of cost) server containing some image archives (from now on referred to as server 1). This server does not run PHP and all access I have to that server is an FTP login. The other server (from now on referred to as server 2) is a true web server with PHP installed, and this one costs money.
So now I want to watermark the images from my PHP-less server 1 and show them on my site on server 2 (which does have PHP), WITHOUT using the bandwith of server 2, since then it will cost money you see ;)
I'm thinking about this for some time now and I guess what I want is impossible. Can anyone confirm that?
Please send a mail to alpha@zoepe.nl -> thank you!
I thought that I was a decent PHP programmer, yet I can't get this bloody thing to generate anything but errors. I've got like 200 pictures to watermark, I'll be doing it for a while, unless one of you dudes can make sence of this.
I screwed with it for a while longer... and now I have white where it should be clear. I seem to remember someone with the same type of problem but with black... I'll have to refer back in the thread.
Keep ya posted. ;)
[edit: now its black too lol I might have to try and use DocDaves script and see what comes of it]
Bookmarks