PHP Watermark

Adding watermark in PHP is very simple. It can be done using PHP image libraries like GD. Using GD functions, we can use text or image as watermark onto a target layer/document.

PHP / GD Text Watermarking

Text watermarking functions are,

  • imagestring() – Adding text string into images
  • imagettftext() – Adding text into images using True Type Fonts


imagestring($image, $font, $x, $y, $watermarkText, $watermarkColor);
imagettftext($image, $fontSize, $watermarkAngle, $x, $y, $watermarkColor, $fontFilePath, $watermarkText);

The imagestring() function is sufficient for adding simple watermark without more gimmicks. To add effects like angled text as watermark, imagettftext() function can be used. It provides a rich set of effects.


There are more GD functions to add text watermark to images using other font types Free Type and Post Script Type. For example, imagefttext(), imagepstext().

PHP / GD Text Watermarking Example

$imageURL = "bg.png";
list($width,$height) = getimagesize($imageURL);
$imageProperties = imagecreatetruecolor($width, $height);
$targetLayer = imagecreatefrompng($imageURL);
imagecopyresampled($imageProperties, $targetLayer, 0, 0, 0, 0, $width, $height, $width, $height);
$WaterMarkText = 'CONFIDENTIAL';
$watermarkColor = imagecolorallocate($imageProperties, 191,191,191);
imagestring($imageProperties, 5, 130, 117, $WaterMarkText, $watermarkColor);
header('Content-type: image/jpeg');
imagepng ($imageProperties);

The output of this PHP script will be,


PHP / GD Image Watermarking

In PHP, adding an image onto another image layer as a watermark can be done using imagecopy() function.

imagecopy($destinationImage, $srcImage, $destinatioX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight);

This function copies source image onto destination image by overwriting destination image pixels.

There is another similar GD function imagecopymerge() which is to copy and merge source onto the destination.

imagecopymerge($destinationImage, $srcImage, $destinatioX, $destinationY, $sourceX, $sourceY, $sourceWidth, $sourceHeight,$pct);

imagecopymerge() function has an extra parameter $pct. It is used for image merging.

While merging png images with transparent background as a watermark, imagecopymerge() function will not preserve transparency onto the destination. So, imagecopy() is preferable for image watermarking.

PHP / GD Image Watermarking Example

$watermark = imagecreatefrompng('watermark.png');
$imageURL = imagecreatefrompng('bg.png');
$watermarkX = imagesx($watermark);
$watermarkY = imagesy($watermark);
imagecopy($imageURL, $watermark, imagesx($imageURL)/5, imagesy($imageURL)/5, 0, 0, $watermarkX, $watermarkY);
header('Content-type: image/png');

Output of this image watermarking script will be,



Download PHP Watermark Source Code

This PHP code tutorial was published on January 30, 2014.

↑ Back to Top