Thursday, February 24, 2011

Watermark on image


With GD library in PHP, you can easily add a watermark to any image. The idea of this is to merge two image on top of one another. This possibly is the one of the easiest things that can be achieved by GD library. But watermark literally means a small semi transparent image will be placed on another one. For this you need to have a semi transparent image. Which will preferably be a PNG file.

Use the following code to achieve watermarking on image:

$stamp = imagecreatefrompng(PATH_OF_THE_SEMITRANSPARENT_IMAGE);
$im = imagecreatefromjpeg(PATH_OF_THE_ACTUAL_IMAGE);
$marge_right = 0;
$marge_bottom = 0;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

The above code will put the watermark at the bottom right corner of the image. Adjust the value of $marge_right and $marge_bottom to your needs. After the watermark is created the original image will be overwritten with the watermarked image. However to change this refer to the line:

Change the $source to the destination file name, which will have the watermark, keeping intact the original image.

Best of luck,

No comments:

Post a Comment