How to add watermark Text over Image in PHP

In this tutorial, We will discuss, How to add watermark Text over Image in PHP. These days, Every company using their Text or Logo as a watermark over images that they want to share over social media or other websites Like Ecommerce, Google, Youtube, etc.

Here we are talking about PHP code through which we can add watermark text over the image. We can use these Scripts in Any Platform like Core PHP, WordPress, Laravel, and other PHP Platforms.

Please check the Basic Script to add text over the image given below:


$SourceFile = ‘./images/xyz.jpg’;
$DestinationFile = ‘./images/image1-watermark.jpg’;
$WaterMarkText = ‘Copyright phpJabbers.com’;

function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {

list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$font = dirname(__FILE__) . ‘/font/arial.ttf’;
$font_size = 10;
imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
if ($DestinationFile<>”) {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header(‘Content-Type: image/jpeg’);
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);

I hope this code will work at your end. If you have any issues then let me know. I will surely help you with the same.

If you need other Informational Articles then you can visit my Website at https://summyifi.com

Good Luck 🙂

Leave a Reply

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