PHP

How To Add Text Watermark To Image In PHP?

This simple article demonstrates of PHP add watermark text to image. you will learn how to add watermark text to image in PHP. I would like to show you PHP add text on image as watermark. I explained simply about add text watermark to image PHP. Alright, let’s dive into the steps.

  • 4.5/5.0
  • Last updated 05 September, 2022
  • By Admin

Sometimes we want to add a website domain name as a watermark on each image of your website. Then I will give you a very simple example of how to add a text watermark to image in PHP. so let's simply follow below code and make it done.

Here, You need to put one image call "watermark-php-text.png". Then you have to download font from google fonts and put in folder. You can download from here:
Download Font

Let's copy below code and create index.php file.

index.php

<?php
$sourceImage = "watermark-php-text.png";
  
$imagetobewatermark = imagecreatefrompng($sourceImage);
$watermarktext="CODEWALE.COM";
$font= "fonts/Roboto/Roboto-Bold.ttf";
$fontsize="22";
$white = imagecolorallocate($imagetobewatermark, 51, 102, 0);
  
$image = imagecreatefrompng($sourceImage);
$imageSize = getimagesize($sourceImage);
  
$wmX = $imageSize[0] - 380;
$wmY = $imageSize[1] - 20;
  
imagettftext($imagetobewatermark, $fontsize, 0, $wmX, $wmY, $white, $font, $watermarktext);
header("Content-type:image/png");

/*
For Save Image
imagepng($imagetobewatermark, $sourceImage);
*/
  
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);
?>

Output