Tutorialsplane

Add Watermark On Image in PHP


Add Watermark On Image in PHP- It is very easy and simple to add watermarks on images in PHP. GD library and GD functions are used for adding watermarks on image in PHP. Here in this tutorial we are going to cover two things – 1. Add Text as watermark. 2. Add Image as Watermark.


How to Add Watermark On Image in PHP?

You can add the watermark on images in following ways –

Add Text As Watermark

Three functions are used to add the text as watermark in php –

Syntax

Here is the syntax for adding the text watermark on image.

Text Watermark : imagettftext() Syntax

<?php imagettftext($imageResource, $fontsize, $waterMarkAngle, $x, $y, $color, $fontFilePath, $watermarkText);
??>

imagettftext() function is main function which is responsible for adding the text watermark on the image. This function accepts the following parameters-

Text Watermark On Image : Example 1 –

Let us pass the font color white, angle 0 degree and font size 30px.

Add Text As Watermark On Image Example:

<?php $image = "test.png";
list ($width, $height) = getimagesize($image);
$imageResource = imagecreatetruecolor($width, $height);

$imgmark = imagecreatefrompng($image);
//Pass the image ressource
imagecopy($imageResource, $imgmark, 0, 0, 0, 0, $width, $height);

$watermarkText = "Tutorialsplane.com";
// font path
$font = "arial.ttf";
$fontsize = "30";
//Pass the image ressource
$white = imagecolorallocate($imageResource, 255, 255, 255);
//Pass the image ressource
imagettftext($imageResource, $fontsize, 0, 20, 10, $white, $font, $watermarkText);

header("Content-type:image/png");
//Pass the image ressource
imagepng($imageResource);
//Pass the image ressource
imagedestroy($imageResource);
??>

Try it »

If you run the above example it will produce the output something like this –

Text Watermark on Image : Example 2 –

Let us pass the font color white, angle 45 degree and font size 40px.

Add Text As Watermark On Image PHP Script Example:

<?php $image = "test.png";
list ($width, $height) = getimagesize($image);
$imageResource = imagecreatetruecolor($width, $height);

$imgmark = imagecreatefrompng($image);
//Pass the image ressource
imagecopy($imageResource, $imgmark, 0, 0, 0, 0, $width, $height);

$watermarkText = "Tutorialsplane.com";
// font path
$font = "arial.ttf";
$fontsize = "40";
//Pass the image ressource
$white = imagecolorallocate($imageResource, 255, 255, 255);
//Pass the image ressource
imagettftext($imageResource, $fontsize, 45, 180, 400, $white, $font, $watermarkText);

header("Content-type:image/png");
//Pass the image ressource
imagepng($imageResource);
//Pass the image ressource
imagedestroy($imageResource);
??>

Try it »

If you run the above example it will produce the output something like this –

Add image as Watermark

Apart from texts you can also add image as watermarks. Sometimes we need some image to be used as watermark such as logo of company on each images. Php imagecopy() function is enough to add image watermarks on another image. You can add the image watermarks simply as below –

Syntax

Syntax for image watermark is as –

Text Watermark : imagettftext() Syntax

<?php imagecopy($dstImage ,$srcImage , $dstX , $dstY , $srcX , $srcY , $srcW , $srcH );
??>

The above function is enough for adding the image watermark on another image.
The imagecopy function copy the source Image($srcImage) onto the destination image($dstImage). Input parameter of imagecopy() function are as –

Add Image Watermark : Example –

Let us add watermark simply using the imagecopy() function as below- .

Add image As Watermark On Image PHP Script Example:

<?php $logo = imagecreatefrompng('logo.png');
$image = imagecreatefrompng('test.png');

$margeRight = 50;
$margeBottom = 50;
$lx = imagesx($logo);
$ly = imagesy($logo);

imagecopy($image, $logo, imagesx($image) - $lx - $margeRight, imagesy($image) - $ly - $margeBottom, 0, 0, imagesx($logo), imagesy($logo));
// Output and free memory
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
??>

Try it »

The above example will add the source image onto the destination image. If you run the above example it will produce the output something like this –

Php - FAQ