Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Watermarking Images from a Directory

PHP to Watermark

7:14 pm on Jul 23, 2009 (gmt 0)

5+ Year Member

Hello all,
I have this script which searches a directory and if found, displays all images in there.

However, I want these images to be watermarked with a little png file that I have as my watermark and stored in a directory.

I have come up with this script, but it throws out various errors. Please could the experienced review my code as advise where I have gone wrong.

foreach($img_name as $img) {
//echo "<p><img src='$img' /><br /></p>" ; This line displays the images perfectly. However, I want the code below to work for the watermark.

$img = "<img src='$img' />";

$watermark = imagecreatefrompng('D:/web/mysite/photos/Canadaso-Comblo-2008/watermark.png');
$watermark_width = imagesx($watermark); // Watermark's width
$watermark_height = imagesy($watermark); // Watermark's height
$image = imagecreatetruecolor($watermark_width, $watermark_height);

$image = imagecreatefromjpeg($img);
$size = getimagesize($img);

$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);

//The below curly braces are continuation of pervious codes.


3:23 am on Jul 24, 2009 (gmt 0)

10+ Year Member

maybe if you include these lines before imagejpeg($image);
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-type: image/jpeg');
3:33 am on Jul 24, 2009 (gmt 0)

10+ Year Member

i mean that you need 2 files: 
file 1:
foreach($img_name as $img) {
echo "<img src="create_image.php" />"

file 2 (create_image.php)
image process

9:11 pm on Jul 24, 2009 (gmt 0)

5+ Year Member

hmm, without getting into imagemagick and creating a new image, you might be able to do this in CSS, where you lay a div that contains the watermark over the image. There will be some backwards compatibility browser issues, but I've done similar things with excellent results.