How To Add text to image in PHP – Watermarking

Hello learners, here I am gonna show you how you can easily add text to image in PHP. This is like watermarking any image you want. You will be able to use your own text that will be placed over an image. The image will be watermarked with your desired text. Pretty cool right?

So, Here we gonna use the PHP GD library to add text to image in PHP.

We will use a font file to choose own desired font.

We will use our pre-stored image in our file directory.

Here, we gonna set the color of our text that.

We will choose the custom position of our text in the image.

After sending the image to the browser we will destroy the image to clear the memory.

You might be interested to Know How To Add Watermark in Images Using PHP

Add Text To Image in PHP – Easy Watermark in PHP

Create a PHP file and save the below code in that file.
Add an image to the same folder or directory with the file name image.jpg.
Put a TTF font file too in the same directory.
You are all set.

  header('Content-type: image/jpeg');

  $image = imagecreatefromjpeg('image.jpg');

  $textcolor = imagecolorallocate($image, 255, 255, 255);

  $font_file = 'myfont.ttf';

  $custom_text = "Watermark Text";

  imagettftext($image, 225, 0, 3450, 3000, $textcolor, $font_file, $custom_text);


  imagedestroy($image); // for clearing memory
 $image = imagecreatefromjpeg('image.jpg');

This creates an Image from an existing image file i.e, image.jpg

I used this image as image.jpg


 $textcolor = imagecolorallocate($image, 255, 255, 255);

This is for setting up our text color. I have used RGB color and the above value is for white text color. You can use any color.

 $font_file = 'myfont.ttf';

Thereafter, This is the font location.

I have used This font . You can download it myfont

[ This font file can be used for movie, poster, banner but you are strictly prohibited to use it on webpage, web template, app ]

After downloading this zip file. Extract the font file to your PHP file directory.

 $custom_text = "Watermark Text";

Here we can configure our text, displayed on the image as Watermark.

 imagettftext($image, 225, 0, 3450, 3000, $textcolor, $font_file, $custom_text);

So, this function is for setting up the position of out text and all the other things like, on which image we gonna put our text. Moreover, we can put the text color variable, font file path variable and our custom text variable here.


This is for sending the image to the browser.
Finally, the last function clears the memory after sending the final watermarked image to the browser.

The final output will look like this

PHP watermark

Watermarked Image in PHP

How to save the watermarked image created by PHP


Here you can take another parament filename in this way imagejpeg($image,”watermarked.jpg”);

if you use this line. Then in that same directory, the file will be saved as watermarked.jpg


2 responses to “How To Add text to image in PHP – Watermarking”

  1. M C says:

    if you are not getting your image, or the image is just one small squre, try this for your font file path, the realpath function is probably needed.

    $font_file = realpath(“.”).’\\fonts\arial.ttf’;

    • Tarazjusz says:

      still doesnt work 🙁 i get a white square…
      [ im working in info.php which is a sibling file to a folder ‘czcionki’ which contains font files (.ttf) ]

      header(‘Content-type: image/jpeg’);

      $fotka = imagecreatefromjpeg(‘image/czernowce.jpeg’);
      $kolor = imagecolorallocate($fotka, 255, 255, 255);
      $czcionka = realpath(“.”).’\\czcionki\RobotoCondensed-Regular.ttf’;
      //$czcionka = realpath(“czcionki/RobotoCondensed-Regular.ttf”);
      $znak = “cos”;

      imagettftext($fotka, 18, 0, 0, 24, $kolor, “czcionki/RobotoCondensed-Regular.ttf”, $znak);

      imagejpeg($fotka, ‘images/znakW.jpg’);


Leave a Reply

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