Add a text watermark using a True Type Font to an image with PHP

July 05, 2014 | Last tested: May 2015 | 1922 views | Comments

Use a True Type Font to watermark an image in PHP.

For example.

Reference Tutorial: Write text to an image using TrueType fonts in PHP

//the image to be watermarked
$image = 'images/create-a-lonely-child-scene-photo-manipulation.jpg';

//get the size of the image
list($width, $height) = getimagesize($image);

//set the font size - pixel size for (GD1) or point size for (GD2)
$fontSize = 20;

//set the text angle
$angle = 0;

//set the watermark text
$text = "";

//set the x and y positions of the text
$xPosition = 10; //ten pixels from the left
$yPosition = 30; //font size plus ten pixels from the top (20+10)

//create a new image
$newImg = imagecreatefromjpeg($image);

//set the True Type Font to be used
$font = '1942.ttf';

//set the font color to red
$fontColor_red = imagecolorallocate($newImg, 255, 0, 0);

//write the text on the created image

//output the new image with a watermark to a file
 * PNG file appeared to have
 * a better quality than the JPG

 * destroy the image to free
 * any memory associated with it



Please mouse over here to view the image.


JPG (100% quality)

Please mouse over here to view the image.

Other options:

Center the watermark on the image.

Put the watermark on the lower right corner of the image.

To place the watermark on the upper right corner of the image.


The sample image was used with permission, and is from CienelDotNet : Create A Lonely Child Scene Photo Manipulation.