Last updated on November 29th, 2022 at 03:50 pm

Let us take any image type .JPG OR .PNG OR .GIF

Here for original image I am using an image named “thank you1.jpg”, For watermark I am using a PNG file named “logo_big.png”
thank you1.jpg
logo_big.png
Also refer to Simple Watermark Script Using PHP

All you have to do is create a PHP file of your desired name then upload the two image files to the same location and run the script.

Note:-Your GD library must be enabled
Here is how the code works. You can see there is a line which declares variable $stamp

$stamp = new mywatermark('thankyou1.jpg','logo_big.png','mistonline.in');

It takes 3 arguments

  • 1st is the Main picture image that is “thank you1.jpg”
  • 2nd is the watermark image which is “logo_big.png”
  • 3rd is the filename which starts with “mistonline.in”

The variable $this->pictureInfo[2] will have 3 values,

  • if the main image used is GIF
  • if the main image used is JPG
  • if the main image used is PNG

I have tried to give comments and explanation on most of the functions used below. Please refer them accordingly. If you are not familiar with any of the functions used I have given a direct link to PHP.NET website for detailed explanation of those.

If you want to dumb your GD Library details you can uncomment this line number 31 which says var_dump(gd_info());

<?php
mywatermark::GDversion();
$stamp = new mywatermark('thank you1.jpg','logo_big.png','mistonline.in');
 $stamp->stampPicture();
  
class mywatermark{
 
// class variables
private $fileHandle = null;
private $newPictureName = null;
private $pictureInfo = null;
private $pictureName = null;
private $prefix = null;
private $stampInfo = null;
private $stampName = null;
private $stampXpos = null;
private $stampYpos = null;
 
function __construct($picture, $stamp, $prefix = 'mistonline.in'){
 
$this->pictureName = $picture; // original picture to place stamp/watermark on
$this->stampName = $stamp; // stamp/watermark picture
$this->prefix = $prefix; // prefix of new stamped picture
 
}// end of construct
 
public static function GDversion(){
 
// GD 2.0.28 or newer is recommended version to use
// http://www.php.net/manual/en/function.gd-info.php
//var_dump(gd_info()); // dump information about your GD version
 
return true;
}// end of GDversion
 
private function openImage($fileName, $type){
 
// open picture with correct image function. Add more types if needed.
// GIF: http://php.net/manual/en/function.imagecreatefromgif.php
// JPG/JPEG: http://php.net/manual/en/function.imagecreatefromjpeg.php
// PNG: http://php.net/manual/en/function.imagecreatefrompng.php
switch ($type){
 
case 1: // GIF
$this->fileHandle = imagecreatefromgif($fileName);
break;// case 1
 
case 2: // JPG/JPEG
$this->fileHandle = imagecreatefromjpeg($fileName);
break;// case 2
 
case 3: // PNG
$this->fileHandle = imagecreatefrompng($fileName);
break;// case 3
 
default:
die('Unsupported filetype: '.$fileName);
}
 
return $this->fileHandle;
}// end of openImage
 
public function stampPicture(){
 
// get picture info such as width, height and extension
// http://php.net/manual/en/function.getimagesize.php
$this->pictureInfo = getimagesize($this->pictureName) or die('Error getting picture info. Double check file path.');
$this->stampInfo = getimagesize($this->stampName) or die('Error getting stamp info. Double check file path.');
 
// open images with class method openImage()
$this->pictureFile = $this->openImage($this->pictureName, $this->pictureInfo[2]);
$this->stampFile = $this->openImage($this->stampName, $this->stampInfo[2]);
// position the stamp in the lower right corner
$this->stampXpos = $this->stampInfo[0]; // width – width – margin
$this->stampYpos = $this->stampInfo[1]; // height – height – margin
 
// set a new name for the stamped picture and keep the original picture intact
$this->newPictureName = $this->prefix.$this->pictureName;
 
// alpha blending: http://php.net/manual/en/function.imagealphablending.php
imagealphablending($this->pictureFile, true);
imagesavealpha($this->pictureFile, true);
 
// merge the two images: http://php.net/manual/en/function.imagecopymerge.php
imagecopymerge($this->pictureFile, $this->stampFile, $this->stampXpos, $this->stampYpos, 0, 0, $this->stampInfo[0], $this->stampInfo[1], 50);
 
// output the stamped image as GIF, JPG/JPEG or PNG. Add more types if needed.
// default type is the same as the original file
// GIF: http://php.net/manual/en/function.imagegif.php
// JPG/JPEG: http://php.net/manual/en/function.imagejpeg.php
// PNG: http://php.net/manual/en/function.imagepng.php
switch ($this->pictureInfo[2]){
 
case 1: // GIF
imagegif($this->pictureFile, $this->newPictureName);
echo "<a href='".$this->newPictureName."'>Click Here To View The Image Created</a>";
break;// case 1
 
case 2:// JPG/JPEG
imagejpeg($this->pictureFile, $this->newPictureName);
echo "<a href='".$this->newPictureName."'>Click Here To View The Image Created</a>";
break;// case 2
 
case 3: // PNG
imagepng($this->pictureFile, $this->newPictureName);
echo "<a href='".$this->newPictureName."'>Click Here To View The Image Created</a>";
break;// case 3
 
default:
die('Unsupported filetype: '.$fileName);
 
}
 
return true;
}// end of stampPicture
 
function destruct(){
unset($fileHandle, $newPictureName, $pictureInfo, $pictureName, $prefix, $stampInfo, $stampName, $stampXpos, $stampYpos);
}// end of destruct
 
}// end of class
//mistonline.in
?>

DEMO

Note:-This tutorial was originally posted on Sep 25, 2008 and Bugs fixed with Demo added on May 20, 2016, Updated again on Aug 4, 2022

Leave a Reply

Your email address will not be published. Required fields are marked *