Online Forum To Support Coders With Technical Assistance - Read Write Execute

How to add watermark text on image in php

Ankit Agrawal
Ankit Agrawal
Published on February 5, 2019 · 1 min read

Watermark text can we added by using php image library such as GD and Imagemagick. Please follow below code.

<?php
$aImage = "Image path";
$aFont = "Font Path"; 
$aFontSize = 20; // watermark font size
$aNewImage = imagecreatefromjpeg($aImage);
$aImgWidth = imagesx($aNewImage);
$aImgHeight = imagesy($aNewImage);
$aTextColor = imagecolorallocatealpha($aNewImage, 0,0,0, 50); 
imagettftext($aNewImage, $aFontSize, 0, ($aImgWidth - 100)/2 , ($aImgHeight)/2, $aTextColor, $aFont, 'Text');
$aImgWatermark = "watermark_".$aImage;
imagejpeg($aNewImage, $aImgWatermark, 100);
imagedestroy($aNewImage);
?>
Was this article helpful?
Ankit Agrawal

Ankit Agrawal

Author & Senior Engineer at CodeExecute

Writing practical engineering guides, debugging real-world bottlenecks, and creating free tools for developer productivity.

← Previous Article How to download files using url in php Next Article → Add product programmatically in woocommerce