0
votes

I am trying to generate images containing data from my database, and I want to use a custom font for it, so I looked up the imagettftext. I am following its syntax, but it's not printing anything. What am I doing wrong?

When I use imagestring it works fine. But that does not use any custom font.

I have placed my font.tff file in the same folder as my script. Here is my code:

header('Content-Type: image/png');

$image = @imagecreate(400, 110);
$backgrund = imagecolorallocate($image, 70, 130, 180);
$textcolor = imagecolorallocate($image, 255, 255, 255);
$font = 'font.ttf';

// TTF
imagettftext($image, 10, 0, 15, 15, $textcolor, $font, 'Test TTF');

// Normal text
imagestring($image, 2, 15, 30, 'Test Normal Font', $textcolor);

imagepng($image, 'images/test.png');
imagedestroy($image);

Am I missing something? The first line is completely blank. I also tried adjusting the font size (it maybe is different than regular imagestring?) but still nothing. The font is a .ttf font file.

What characters are in your custom font? For that text to display, it needs space, capital F, N and T, and a couple of lowercase characters. - Jongware
I've tried many different fonts. I even tested just the letter "a" and it still does not work. I am doing this on my local computer, using XAMPP and Apache. I read something about enabling FreeType and some GD Library? Is that maybe what I am missing? And how do I enable that in XAMPP - Lee Cheung
I also tried fonts like consolas, arial, and other in ttf and none of them work - Lee Cheung