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 โ€“

  • 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);
?>

Try it ยป

If you run the above example it will produce the output something like this โ€“

Add Watermark On Image in PHP

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 Watermark On Image diagonal php

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);
?>

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 โ€“


Advertisements

Add Comment

๐Ÿ“– Read More