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 –
- imagecreatetruecolor– Returns an image identifier representing a black image of the specified size.
- imagecopy– Used to copy image.
- imagettftext– This is used to write the texts to the image using TrueType fonts.
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-
- $imageResource– This is obtained from the function imagecreatetruecolor().
- $fontsize– This is used to define the font size of the watermark text.
- $waterMarkAngle– This is used to define the watermark angle.
- $x, $y– $x is used to define the distance of the watermark from X axis. $y is used to define the distance from Y axis.
- $fontFilePath– This is used to add the font file path. If font file is not there then download the font file – Download Arial Font
- $watermarkText– Watermark text to be added.
- $color– This is used to add the watermark font color.
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); ??> |
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); ??> |
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 –
- $dstImage– The main image on which you want to add the watermark.
- $srcImage– The image used for watermark.
- $dstX, $dstY– The x, y co-ordinates of destination image.
- $srcX , $srcY– The x, y co-ordinates of the source image.
- $srcW , $srcH– The height & width of the image to be copied. The image will be copied with a height of $srcW & Height of $srcH from the co-ordinates $srcX, $srcW And will be added onto the destination image’s co-ordinate $x, $y.
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); ??> |
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 –