PHP Watermark

by Vincy. Last modified on July 15th, 2022.

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().

Output of this image watermarking script will be,


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

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

Download PHP Watermark Source Code

Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “PHP Watermark”

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page