Thursday 4 February 2016

How to Insert Watermark on to Image using PHP GD Library



Hello Friends, In this tutorial we are going to learn how can we insert watermark or watertext or website link on to an Image. Because in most of the ecommerce website we can see the product image with watermark or watertext present on that image. So, I make this useful tutorial with video.

Before going to start with PHP GD Library, First we have to enable php_gd2 PHP extension.

I will use following function for Insert watermark.

  • imagecreatefrompng(image) — Create a new image from given image
  • imagesx(image) - Return the width of Given Image
  • imagesy(image) - Return the height of given image
  • imagettftext(image source, font size, angle, x axis, y axis, text color, font path, watermar text) - Add text on to image
  • imagepng(image) - create png image
  • imagedestroy(image) - destroy image

Now first I make function with name insert watermark.

 function insert_watermark()  

I have one image with name image.png.


How to Insert Watermark on to Image using PHP GD Library


Now I store this into one image variable with the help of imagecreatefrompng() function. This function create image from png image file.

 $image = imagecreatefrompng("image.png");  

Then after I define text color using imagecolorallocate() function. In this function first parameter is image source and other parameter is value of RGB color.

 $textcolor = imagecolorallocate($image, 255, 255, 255);  

After define text color I want to proceed for insert text on to image. For this I will use imagettftext() function. With the help of this function we can control over the font size. For this function I have to download font and store in our working folder.

After storing font under working folder, I start using this imagettftext() function. In this function first parameter is image source, second parameter is angle of text, third is x axis, forth is y axis, fifth is text color, text color which I have define earlier in this code, sixth is font path and last is water mark text.

 imagettftext($image, 14, 0, imagesx($image)-125, imagesy($image)-20, $textcolor, 'arial.ttf', "Webslesson");  

After defining text to image I have define content type by using header() function.

 header("Content-type: image/png");  

After defining content type I have to define type of image by using imagepng() function.

 imagepng($image);  

And Lastly I want to destroy image for clear memory, I have to use imagedestroy() function.

 imagedestroy($image);  

Now Finally I have make function for add watermark text on to a image.

 <?php  
 function insert_watermark()  
 {  
      $image = imagecreatefrompng("image.png");  
      $textcolor = imagecolorallocate($image, 255, 255, 255);  
      imagettftext($image, 14, 0, imagesx($image)-125, imagesy($image)-20, $textcolor, 'arial.ttf', "Webslesson");  
      header("Content-type: image/png");  
      imagepng($image);  
      imagedestroy($image);       
 }  
 ?>  

Now I use this function and insert watermark on to a image by simple echo statement.

 echo '<img src="'.insert_watermark().'" />';  

1 comment:

  1. If we want to change the text position then what should we do. Like for example if I want to add the text in middle of the image or left top corner or something else then we we need to do.

    ReplyDelete