Home   Best Sellers   Blogging   Coding & Design   Technology   SEO   Travel & living   Career   Videos   Tips   Online tools     
Home  »     »     »  PHP function to add watermark on an image

PHP function to add watermark on an image

Tuesday, November 16, 2010

How to apply watermark on a image?

Watermarking means applying an image with a transparent/non transparent background on another image to protect the image from unauthorised use of it by other parties which doesn;t own the image.

The following function can be used to add watermark on images.

The function accepts 2 parameters

1. the source image - the image on which watermark has to be applied
2. the position of the watermark image on the source image.

*****************CODE START***************


function apply_watermark($imagesource, $position = 'bottomright'){
$info = getimagesize($imagesource);
case 1 : { $image = imagecreatefromgif($imagesource); break;}//gif
case 2 : { $image = imagecreatefromjpeg($imagesource); break; }//jpg
case 3 : { $image = imagecreatefrompng($imagesource); break; }//png
default : { exit("Unsupporter file type"); }
print "Can't read required image data";

//watermark image
$watermark = @imagecreatefrompng(getenv('DOCUMENT_ROOT').'/images/logo.png');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
case 'topleft':
$startwidth = 0;
$startheight = 0;
case 'topright':
$startwidth = ($imagewidth - $watermarkwidth);
$startheight = 0;
case 'middle':
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
case 'bottomleft':
$startwidth = 0;
$startheight = ($imageheight - $watermarkheight);
case 'bottomright':
$startwidth = ($imagewidth - $watermarkwidth);
$startheight = ($imageheight - $watermarkheight);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);

// Output final image
case 1 : { imagegif($image, $imagesource, 100); break;}//gif
case 2 : { imagejpeg($image, $imagesource, 100); break; }//jpg
case 3 : { imagepng($image, $imagesource, 100); break; }//png
default : { exit("Unsupporter file type"); break;}


*****************CODE END***************

Share this!

How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
PHP function to add watermark on an image.

comments powered by Disqus

This Weeks 7 Popular Posts

Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   

Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »