Skip to main content

PHP function to add watermark on an image


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***************

<?php

function apply_watermark($imagesource, $position = 'bottomright'){
$info = getimagesize($imagesource);
if($info){
switch($info[2]){
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"); }
}
}
else{
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);
switch($position){
case 'topleft':
$startwidth = 0;
$startheight = 0;
break;
case 'topright':
$startwidth = ($imagewidth - $watermarkwidth);
$startheight = 0;
break;
case 'middle':
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
break;
case 'bottomleft':
$startwidth = 0;
$startheight = ($imageheight - $watermarkheight);
break;
case 'bottomright':
default:
$startwidth = ($imagewidth - $watermarkwidth);
$startheight = ($imageheight - $watermarkheight);
break;
}
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);

// Output final image
switch($info[2]){
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;}
}
imagedestroy($watermark);
}


?>

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



Comments

Popular posts from this blog

Sachin Tendulkar's emotional farewell Speech at Wankhede Stadium, Mumbai - Full Text and Video

Full Text and Video of Sachin Tendulkar's emotional farewell Speech on Nov 16, 2014 On November 16, 2013 - Sachin Ramesh Tendulkar played his last international cricket match against West-Indies at the at Wankhede Stadium, Mumbai. After the match he gave an emotional farewell speech. No one would be able forget the speech. The eyes of the gathered people were tearfull. Through out the speech Sachin thanked each and everyone who supported and encouraged him to build up his glorius career which spanned 24 years.

How to add "Link to this page" option under blogger posts?

Steps in adding Link to this page to your blogger posts Links to your page can improve your page rank. So it is a good option to add HTML code for linking to your web page. So that reader can copy and paste it on their web page. if another website links to your web page, this is considered an external link to your website. External links to your website are the most important source of ranking power and in SEO terminology it is considered as third party ranking vote for your page.

PETMAN Robot from Boston Dynamics

PETMAN from Boston Dynamics Here comes another amazing robot from Boston Dynamics . PETMAN is the name given to this wonderful anthropomorphic robot. This will be used for testing special clothing used by US military personnel. Features of PETMAN Amazing body balance: robot balances itself as it walks, squats and does calisthenics.


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 »
Member
Search This Blog