Last updated on June 5th, 2018 at 08:04 pm

In this tut i will explain you that how we can prevent or best capture image from the people who download your image and sell it over internet and you never know who do this. To prevent your image from this we have solution, we create a copyright watermark over image. But the standard watermarking procedure of editing the image in a photo-editing application is time consuming. And we dont have so much time to do this. PHP offers a far better solution to solve this problem and save your time.


PHP 4+ and GD 2.0+ represent a powerful combination when it comes to creating or altering images dynamically. We print a transparent gif-image on a jpeg-photo. To achive best result i prefer you to use Gif format image watermark as compare to PNG.

Now first we can create PHP Code to create a text watermark over image

Text watermark Code

function textwatermark($src, $watermark, $save=NULL) { 
 list($width, $height) = getimagesize($src);
 $image_p = imagecreatetruecolor($width, $height);
 $image = imagecreatefromjpeg($src);
 imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height); 
 $txtcolor = imagecolorallocate($image_p, 255, 255, 255);
 $font = 'monofont.ttf';
 $font_size = 50;
 imagettftext($image_p, $font_size, 0, 20, 100, $txtcolor, $font, $watermark);
 if ($save<>'') {
 imagejpeg ($image_p, $save, 100); 
 } else {
 header('Content-Type: image/jpeg');
 imagejpeg($image_p, null, 100);
$src = 'aneh.jpg';
$save = '';
$watermark = 'Aneh Thakur';
textwatermark($src, $watermark, $save);

In the above code i create a small function textwatermark(“image source”, “watermark text”, “save location”) and pass three parametter inside it.

  1. First we get width and height of source image.
  2. Use php GD function imagecreatetruecolor();.
  3. Use php function to create image from jpeg.
  4. then re sample image using imagecopyresampled().
  5. then we use collor allocation function imagecolorallocate().
  6. In this i am also using font family ‘monofont.ttf’.
  7. Use imagettftext() to add text over image.
  8. Now we use header() to tell the Web browser that we are going to output an image in JPEG format.

Image Watermark Code

Now we add GIF watermark image over Image.

function watermarkImage($SourceFile, $WaterMark, $DestinationFile=NULL, $opacity) { 
 $main_img = $SourceFile; 
 $watermark_img = $WaterMark; 
 $padding = 3; 
 $opacity = $opacity;
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic

if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");

$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];

$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;

// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');

$SourceFile = 'aneh.jpg';
$DestinationFile = '';
$WaterMark = 'watermark.gif';
watermarkImage ($SourceFile, $WaterMark, $DestinationFile, 50);

In above code most of functions are same we use in text watermark and one new function i use in this is imagecopymerge() to merge watermark over image we also declaire opacity in it.


One thought on “Create watermark on image with php”

Comments are closed.