Thursday, 11 April 2013

Watermarking an image with your logo using PHP


When You are developing a photo oriented websites,You have to make sure that each photos upload in your site must not be copied. Or atleast it should be copyrighted to You. may be there are many softwares available for removing water marks, But it will certainly prevent normal users from copying photos from your site.
  So here Iam explaining how to watermark an image using your own Logo.


<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('entekalyanamlogo.png');
$im = imagecreatefromjpeg('photo.jpg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);

?>


Watermarking an Image With Text using PHP


When You are developing a photo oriented websites,You have to make sure that each photos upload in your site must not be copied. Or atleast it should be copyrighted to You. may be there are many softwares available for removing water marks, But it will certainly prevent normal users from copying photos from your site.
  So here Iam explaining how to watermark an image using your company name or some other texts..


<?php
// Load the stamp and the photo to apply the watermark to
$im = imagecreatefromjpeg('photo.jpg');

// First we create our stamp image 
$stamp = imagecreatetruecolor(100, 70);
imagefilledrectangle($stamp, 0, 0, 99, 69, 0x0000FF);
imagefilledrectangle($stamp, 9, 9, 90, 60, 0xFFFFFF);
$im = imagecreatefromjpeg('photo.jpg');
imagestring($stamp, 5, 5, 20, 'companyname', 0x0000FF);//Here you can set needed text in place of company name
// first 5 is the font weight
//second 5 is X cordinate
//20 is the Y cordinate
imagestring($stamp, 3, 20, 40, '(c) 2013', 0x0000FF);

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Merge the stamp onto our photo with an opacity of 50%
imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);

// Save the image to file and free memory
imagepng($im, 'photo_stamp.png');
imagedestroy($im);

?>




You can also apply water mark by following function also:-



<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) { 
   list($width, $height) = getimagesize($SourceFile);
   $image_p = imagecreatetruecolor($width, $height);
   $image = imagecreatefromjpeg($SourceFile);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height); 
   $black = imagecolorallocate($image_p, 0, 0, 0);
   $font = 'arial.ttf';
   $font_size = 10; 
   imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
   if ($DestinationFile<>'') {
      imagejpeg ($image_p, $DestinationFile, 100); 
   } else {
      header('Content-Type: image/jpeg');
      imagejpeg($image_p, null, 100);
   };
   imagedestroy($image); 
   imagedestroy($image_p); 
};
?>
<?php
$SourceFile = 'photo.jpg';
$DestinationFile = 'image1-watermark.jpg'; 
$WaterMarkText = 'Copyright mycompany.com';
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);
?>