Webdesign,code & ...stuff!

Dec 11, 2008

Simple PHP Image Watermark

Have you ever wanted to add an alpha-transparent watermark to an image that you post on your website?
Here is a simple PHP script that watermarks JPEG and PNG images.

<?php
function watermark($sourcefile, $watermarkfile) {
# $sourcefile = Filename of the picture to be watermarked.
# $watermarkfile
= Filename of the 24-bit PNG watermark file.
//Get the resource ids of the pictures
$watermarkfile_id = imagecreatefrompng($watermarkfile);
imageAlphaBlending($watermarkfile_id, false);
imageSaveAlpha($watermarkfile_id, true);

$fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));

switch(
$fileType) {
case(
"gif"):
$sourcefile_id = imagecreatefromgif($sourcefile);
break;

case(
"png"):
$sourcefile_id = imagecreatefrompng($sourcefile);
break;

default:
$sourcefile_id = imagecreatefromjpeg($sourcefile);
}

//Get the sizes of both pix
$sourcefile_width = imageSX($sourcefile_id);
$sourcefile_height = imageSY($sourcefile_id);
$watermarkfile_width = imageSX($watermarkfile_id);
$watermarkfile_height = imageSY($watermarkfile_id);

$dest_x = ( $sourcefile_width / 2 ) - ( $watermarkfile_width / 2 );
$dest_y = ( $sourcefile_height / 2 ) - ( $watermarkfile_height / 2 );

// if a gif, we have to upsample it to a truecolor image
if($fileType == "gif") {
// create an empty truecolor container
$tempimage = imagecreatetruecolor($sourcefile_width, $sourcefile_height);

// copy the 8-bit gif into the truecolor image
imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0, $sourcefile_width, $sourcefile_height);// copy the source_id int
$sourcefile_id = $tempimage;
}
imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0, $watermarkfile_width, $watermarkfile_height);

//Create a jpeg out of the modified picture
switch($fileType) {
// remember we do not need gif any more, so we use only png or jpeg.
// See the code above to see how we handle gifs
case("png"):
header("Content-type: image/png");
imagepng ($sourcefile_id);
break;

default:
header("Content-type: image/jpg");
imagejpeg ($sourcefile_id);
}
imagedestroy($sourcefile_id);
imagedestroy($watermarkfile_id);
}
watermark("main.jpg","watermark.png");
?>

Sample output below...No comment on the picture pls... its sacred y know..
main.jpg
watermark.png
watermarked output jpeg

2 comments:

Administrator said...

I found a free online image watermark tool called Batch Watermarker that can automate the whole process of image watermarking. If you are away from your PC and don't want to download any software, you can just use this online version of watermarking tool for free. It's quick and it's easy to use.

You can also download their full version of Batch Watermark software program if you want to watermark multiple image files in a folder with just a click of the mouse.

reynold said...

thanks for the info...