Using a dafont font with imagettftext in PHP

If you want to spice up your imagettftext use and use custom fonts from dafont here is how to do it:

Choose your font from dafont in this example I choose Raitons font. Click download on the right of the page

Dafont raitons font

Open up the downloaded zip file and drag the .ttf file into your image create php file directory (note i changed the font name from Raitons Demo.ttf to Raitons.ttf)

raitons zip ttf file


image php file and raitons

image.php is this:

function create_image($width, $height, $text)
$image = ImageCreate($width, $height);
$white = ImageColorAllocate($image, 255, 255, 255);//white
$grey = imagecolorallocate($image, 128, 128, 128);//grey
$black = imagecolorallocate($image, 0, 0, 0);//black
ImageFill($image, 0, 0, $white);
$font = 'Raitons.ttf';//make sure chosen font is in the directory!!
imagettftext($image, 36, 0, 96, 46, $grey, $font, $text);//text shadow
imagettftext($image, 36, 0, 95, 45, $black, $font, $text);//text
$save = "phpimage.png";//image will be save in this files directory as "phpimage.png"
imagepng($image, $save);
return "<img src='$save'>";
ImageDestroy($image);//Free memory
echo create_image(600, 200, 'Sample text for image');

This will generate an image with the custom text using the font Raitons. The image file will be called phpimage.png in your working directory that looks like this:

That is how to use custom or dafont fonts with imagettftext as you can see its simple and custom fonts look a lot better than the default ones!