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



Popular posts from this blog

Payback Points - How to redeem - How to merge multiple payback accounts - Block Payback card - Payback customer care

How to apply for a new ration card and what are the documents required?

List of documents required for new ration card application in Kerala and how to apply


Application for a new ration card should be addressed to Taluk Supply Officer (TSO) / City Rationing Officer (CRO) of applicant's residing area.

Primary document required are Residence certificate, Income certificate and incase the applicant's name is in another ration card then he/she should submit the reduction certificate ontained from previous TSO / CRO as proof for removing his/her name from old Ration card.

You can get the new application from your currently residing Taluk Supply Office.

Joomla and Forum Integration - Integrating Forums to Joomla

Joomla is one of the most popular CMS opensource packages. It is very easier to develop website's using Joomla. You just need to download Joomla package from Joomla's Official website www.joomla.org  and install it on your domain and later adding customizations to templates and feature and Your website is ready :). Now a days most websites provides a forum section for it users for discussing various article topics, gathering opinions etc.



Following are some best know forum opensource packages which can be integrated with Joomla and create a new forum experience for users


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