Add Text Watermark to Image Using PHP

In this tutorial, we will introduce how to add text watermark to an image using php.

1. Open an image

<?php
$image = imagecreatefromjpeg('image.jpg');
?>

2. Set the font type and color of text that will be added to image

<?php
$textcolor = imagecolorallocate($image, 255, 255, 255);
  
$font_file = 'myfont.ttf';
  
$custom_text = "Watermark Text";
?>

3. Add text to an image

We will use php imagettftext() function to add text to an image.

imagettftext() is defined as:

imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

We can add like this:

<?php
imagettftext($image, 225, 0, 3450, 3000, $textcolor, $font_file, $custom_text);
?>

4. Save image

<?php
imagejpeg($image);
imagedestroy($image); // for clearing memory
?>