Her

Home Web Programming PHP Watermarking

Watermarking

Author: Will Author's URL: www.avengex.com More by this author

WatermarkingIsn't it such a pain when you have a lot of work to do in Photoshop and don't have the time? Who wants to watermark 1000 images time after time when you can make one watermarking image and do the rest with a classy PHP script?

Step 1

Make yourself a watermark image in your favorite imaging program. Mine is Photoshop, and transparent the background then save it as watermark.png.

Step 2

In your favorite webpage editing program, mine is Dreamweaver, make yourself a new file called image.php, and then paste this here code:

if(!$_GET['src']) {
exit("No source

image, watermarking cancelled.");
}
header('Content-type: image/png');
$watermark =

imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height =

imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
if(eregi

('.gif',$_GET['src'])) {
$image = imagecreatefromgif($_GET['src']);
}
elseif(eregi

('.jpeg',$_GET['src'])||eregi('.jpg',$_GET['src'])) {
$image = imagecreatefromjpeg($_GET['src']);<br

/>
}
elseif(eregi('.png',$_GET['src'])) {
$image = imagecreatefrompng($_GET['src']);
}

else {
exit("Your image is not a gif, jpeg or png image. Sorry..");
}
$size =

getimagesize($_GET['src']);
$dest_x = $size[0] - $watermark_width - 10;
$dest_y = $size[1] -

$watermark_height - 10;
imagecolortransparent($watermark,imagecolorat($watermark,0,0));
imagecopyresampled

($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height);<br

/>
imagepng($image);
imagedestroy($image);
imagedestroy($watermark);

This basically gets the extension, ?src= off the URL and grabs the image. If it's not .jpeg, .gif or .png then it says sorry and exits PHP. Else it adds the watermark on top of the image and then displays it.