Write Text to Image using TrueType Font in PHP

//if(youLikeIt || itHelpsYou)clickShare();

PHP has several libraries for Image Processing. The most extensively used is GD (Graphics Library).

GD is included by default as of PHP 4.3 wikipedia. So, GD should be used without any additional configuration.


GD has two functions to write text to image : 

  1. imagestring  (standard function)

  2. imagettftext  (using TrueType Font, like .ttf or .otf extension).

This codes show you how to write text to image using TrueType Font :

	//load image
	$imgResource = imagecreatefromjpeg($imagePath);
	//white text color
	$textcolor = imagecolorallocate($imgResource, 255,255, 255);
	$rotation="0";//counter-clockwise rotation
	//write text to img
	imagettftext($imgResource, $fontSize, $rotation, 
					$x, $y, $textcolor,$fontPath, 
	//save image to file
	//output image to browser
	header("Content-Type: image/jpeg");

The result is :


//if(youLikeIt || itHelpsYou)clickShare();
Take a Break //non-technical articles
Hot Topics //most used tags
Email Subscriptions //We won't spam you. Honest!

Enter your email address:

Delivered by FeedBurner