How To Add Watermark in Images Using PHP – Source Code

By Saruque Ahamed Mollick

Hello programmers in this PHP Tutorial I’m gonna show you how to add watermark to image using PHP. I will create a PHP file which will process two images, One image will be our main image and another one is our watermark image. I will create a PHP file which will take these two images and the watermark image will be placed on the main image.

You might also be inteested in How To Add text to image in PHP



Generally, a watermark image is used as a logo or brand identification image. This watermark is generally transparent type. But our main target in this tutorial is to learn how to add a watermark image to another image using PHP.

Crop Image in PHP

How to add watermark to image using PHP with Explanation

Adding watermark to images in PHP is not a hard task to do. what you need to know is some PHP function to work with images. Those functions are –

  1. imagecreatefrompng()  PHP function
  2. imagesx() and imagesy() PHP function
  3. imagecreatetruecolor() PHP function
  4. imagecreatefromjpeg() PHP function
  5. getimagesize() PHP function
  6. imagecopymerge() PHP function
  7. imagejpeg() PHP function

You just need to have some basic knowledge of these functions and some basic idea on what these functions do.

PHP code to add watermark to any image

The below code will process two images and display the newly created watermarked image from those two images on your browser while running this code.

<?php
  header('Content-type: image/jpeg');
      $source='image.jpg';
    $watermark=imagecreatefrompng('watermark.png');
    $watermark_height= imagesy($watermark);
    $watermark_width=imagesx($watermark);
    $image=imagecreatetruecolor($watermark_width, $watermark_height);
    $image=imagecreatefromjpeg($source);
    $image_size=getimagesize($source);
    $x=$image_size[0]-$watermark_width-10;
    $y=$image_size[1]-$watermark_width-10;
    imagecopymerge($image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_width, 100);
    imagejpeg($image);

?>

Also Read,

How To Display A Random Image From Directory In PHP?

Get the image size (height and width) in PHP

Code Explanation Line By Line of Adding Watermark to images in PHP

Just save the above PHP code as a PHP file and in the directory suppose I have two image files. One is image.jpg which is our main image. And another one is watermark.png .

The watermark.png file is going to be used as a watermark here on the image image.jpg .

So what I am going to do is simply took our main image and then will put watermark.png image on that main image.

So before going to explain how we can do this, I would like to tell you what is the purpose of those above functions listed and used in this PHP code.

 header('Content-type: image/jpeg')

Easy to use PHP function to show random image from a particular directory

This is our Content-type of this PHP file. Many of us do not use this content type if he or she is intending to use HTML content type for the page. Because by default PHP mark this file as HTML page type.

But here we want that, when I will run my PHP file I wish that I will get my watermarked image on my browser page. That’s why I have set header(‘Content-type: image/jpeg’); as I wish to have an image type page.

$source='image.jpg';

Here, $source variable refers my main image file. You can use your filename followed by your file path. My main image’s filename is image.jpg

Get the width and height of an image in PHP

Functions used in this tutorial of adding watermark to images using PHP

$watermark=imagecreatefrompng('watermark.png');

This function takes a filename as a parameter and Returns an image resource identifier on success, FALSE on errors.
You can use URL too if fopen wrappers have been enabled.

$watermark_height= imagesy($watermark);

This function imagesy()  returns the height of an image resource. But remember the resource image must be get returned by one of the image creation functions in PHP.

$watermark_width=imagesx($watermark);

This PHP function imagesx() is very much similar to the previous one. But it returns the width of an image resource.

In both the lines, our image resource is our watermark.png image file. We have got the height and width of that image file.

$image=imagecreatetruecolor($watermark_width, $watermark_height)

By this line, we create a new true color image from our watermark image height and width.

This is actually a black image of the same height and width of watermark.png file.

imagecreatefromjpeg() function in PHP almost do the same job as imagecreatefrompng() function. But here our source image resource fill is jpeg type.

imagecopymerge($image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_width, 100);

Now this line is very much important to add watermark to image using PHP.

In this function, we have several parameters.

The first parameter is $image, this is the place where we can put our main image and just after it we have to use our second parameter. The second parameter is for our watermark.png file.

$x and $y might be confusing for you. This is nothing but the position where we gonna keep our watermark image.

Then 0 and 0 these are the initial or starting position of our x and y-axis.

And the last parameter is known as pct. This is similar two opacity and this value varies from zero to hundred.

imagejpeg($image);

This is the function which gonna give us the output watermarked final image.

How to save the watermarked image created by this PHP code

To save this image in your local drive path just simply add another parameter like the below

imagejpeg($image,'watermarked.jpg');

This function will create a new file watermarked.jpg which is nothing but the watermarked image. After running this PHP code on your server you can check that in the same path a new file will be generated if everything goes perfect. And the new file is nothing but your final output of this program.

Leave a Reply

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