Welcome to WebmasterWorld Guest from 54.160.244.240

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)

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 0


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.

Code:
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);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

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

closedir($dir_list);

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

Junior Member

10+ Year Member

joined:Apr 22, 2005
posts:185
votes: 0


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 July 24, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 22, 2005
posts:185
votes: 0


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
headers
...

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

New User

5+ Year Member

joined:June 18, 2009
posts:36
votes: 0


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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members