Watermarking
Listed In PHP and MySQL » Images and GD — Viewing Full TutorialStep 1
Make yourself a watermark image in your favourite imaging program. Mine is Photoshop, and then make the backtounr transparent. Save it as watermark.png.
Step 2
In your favourite webpage editing program, mine is Dreamweaver, make yourself a new file called image.php, and then paste in this 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']);
}
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);
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.
How do you make it work?
Go to your image.php and add the ?src= extension pointing to the image you'd like to watermark.
