Watermark all images in a folder
Monday, August 20th, 2007 in PHPI was checking out the searches from this blog and one in particular caught my eye. It was something like “watermark images from folder“. I remember that not a long time ago I had the same problem.
One of the clients I work for, in the development stage, asked me not to watermark the images of his products because… I really don’t remember the reason. What I do remember is that at some later point, he changed his mind (the website was online with a few hundred products added) and now he wanted to watermark images to prevent theft (some of his competition borrowed them).
So, I came up with 2 solutions. The first one was to watermark the images and save them on his server (a bulk watermark, while reading the whole directory) and another one was to watermark on demand.
Case 1
In order to watermark all images from a folder, you have to read the whole directory. This can be done in a very simple way. Let’s suppose you have your images in a folder called “pictures“. You’ll have to read the directory and for each image you find, you apply the watermark on it and then save it in some other folder. If your php script uses this folder to show the images, instead of changing it, you simply change the names of the folders.
In this case, We’ll assume that your original images reside in the “/pictures” folder and we’ll use a folder called “/temp” to store our new images. Below is the script. For watermarking the images, it will use the script from the watermark image post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php require_once( "watermark.php" ); $original_directory = "pictures/"; $watermarked_images = "temp/"; if ($handle = opendir($original_directory)) { while (false !== ($file = readdir($handle))) { if(!is_file($original_directory.$file)) continue; if(exif_imagetype($original_directory.$file)==2) { watermark($original_directory.$file, "watermark.png", $watermarked_images.$file); } } closedir($handle); } ?> |
That’s it. The script above reads the whole directory you specify and for every file it founds it checks if it’s a jpg image (that’s what exif_imagetype() does) and in case it’s true than it puts the watermark on it and saves it in the temp folder.
Here you have the files for PHP watermark images from folder script. Let me know if it helps or you have trouble.
Case 2
This one it’s very similar to the first, meaning that we’ll use the same watermark function but in a slightly different way. Instead of saving the watermarked images on the server, we’ll simply show them on request. Same as the first example, we’ll assume that the original images are stored in the “pictures” folder.
The difference will be that in our html, instead of asking for an image, we’ll ask for a php page that will do the job for us.
So, that’s it. I hope you like it and I hope you like my cat :)






14 comments
hello
i m getting this error message :
Fatal error: Call to undefined function: exif_imagetype() in /home/wallpape/public_html/index.php on line 18
hei salman,
Your’re getting this error because your PHP instalation does not support exif_imagetype.
You could change the script to check your image type according to the extension. If you need help with that let me know and we’ll see what we can do.
You might want to take a look at this also
Can you show some custom code to work with png images? I want to watermark some png images, not jpg, and my change to your code doesn’t work. Please assist
thank you
Hi Jag,
If you need to create watermarked files from png images, in watermark.php change the line
$image=imagecreatefromjpeg($original_image);
into
$image=imagecreatefrompng($original_image);
It’s the first line from the watermark function. Now, if you will need to generate a watermarked png file and not a jpeg one, you will also need to change the lines:
if(!empty($destination))
imagejpeg($image,$destination);
else
imagejpeg($image);
into
if(!empty($destination))
imagepng($image,$destination);
else
imagepng($image);
I hope this answers your question.
Heya,
The script works perfect but after 50 or so images it will just stop. Also how would i put the watermark image in the bottom right of every image instead of the middle? :)
Thanks
After 50 images or so it stops because probably the script reaches the script timeout period. Try increasing that or set set_time_limit(0) at the top of your php script.
To position the watermark on bottom right, replace this
/*
we establish the position of the watermark over the image
*/
$startwidth = ($imagewidth - $watermarkwidth) / 2;
$startheight = ($imageheight - $watermarkheight) / 2;
with this:
$startwidth = $imagewidth - $watermarkwidth ;
$startheight = $imageheight - $watermarkheight;
If you save watermarked picture, i notice the quality is now lower (smaller in size) as orginal JPG was.
ABCD0004.JPG (50,7 KB), watermarked and saved is only 39 KB, so lower quality. How can i preserve the same size/quality?
thanks
Hi Mattias,
image_jpeg() creates the watermarked image. On lines 36 and 38 in watermark.php you’ll notice that function being used. imagejpeg() takes as third parameter image quality. For best quality use 100 as third parameter. More info on this function here: imagejpeg() official info
Hello,
Great Script!
However, the only problem I’m having is it’s rotating my vertical/portrait shots into landscape format.
I want them to stay portrait if they’re portrait.
Is this possible?
Thanks
Huck
Rotating? I don’t really understand what you mean. This script doesn’t rotate anything from what I remember and if you give it a landscape image for example and ask it to resize that in a portrait manner ( with cropping and stuff like that ), that’s what it will do. Please give more info on this.
Great Script!
It’s working great and is exactly what I needed.
However, for some reason, the script is rotating vertical (portrait) pictures into landscape format, then applying the watermark.
Why is it doing this?
I take it that it’s calculations are causing the problem. My watermark image is large enough to fit 52in. wide Landscape JPEGs @ 72dpi, so it’s really big, but resizes perfectly for smaller images, which is really all I need.
But I don’t need it to rotate the image for me at all.
Any clues on how to resolve this?
Thanks in advance for your help!
Kevin
Can you give a live example? I can’t understand exactly what you’re trying to do.
Okay, I’ve adjusted it into 2 scripts, 1 for vertical, one for horizontal.
Both are working well, except the verticals come out horizontal, but the watermark placement & size is correct.
So I’m thinking I can add some php scripting to rotate the vertical images 90 Degrees CCW after watermarking.
However, I can’t seem to get it to work.
Any suggestions?
Thanks again for the great script.
Huck
Thanks,
Its great, Working superb, thanks for gave script
Thanks
srinivas
Leave a comment