How to add watermark to an image using PHP

This is tutorial about how to add watermark to an image using PHP.Watermarks are use full to display copyright information or name of your website on any image.In this process you need two images first is the watermark image and second is image on which watermark will display.Follow these steps to add watermark to an image using PHP.

1.Choose image paths,output format and transparency –Assign the relative path of watermark image to variable $watermark_path and path of original image to $image_path.Choose the format of output image from PNG,JPEG and GIF.Remember that output image format does not depends on both image.$pct is the transparency of watermark image varies from 0 to 100.

Define variable

PHP

1

2

3

4

<?php$image_path='battlefield.jpg';

$watermark_path='battlefield.jpg';

$output="png";

$pct=50;

2. Create resource of both images-Depending on types of images create resources for both images.Here is the function for creating image resource.The function is given below.

Create image resource

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

functionimage_resource($image)

{

$ext=pathinfo($image,PATHINFO_EXTENSION);

if($ext=="jpg"){

$resource=imagecreatefromjpeg($image);

}

elseif($ext=="gif"){

$resource=imagecreatefromgif($image);

}elseif($ext=="png"){

$resource=imagecreatefrompng($image);

}

return($resource);

}

3.Get width and height of images-Using PHP function imagesx() and imagesy()get the width and the height of both images.