[SOLVED] Warning: imagettftext() [function.imagettftext]: Invalid font filename in path\to\php\file.php on line NN

I am running XAMPP 1.7.4 with PHP 5.3.5 on Windows 7. When using any text related GD library functions such as imagettftext(), I get the following error:

[SOLVED] Warning: imagettftext() [function.imagettftext]: Invalid font filename in path/to/php/file.php on line NN

Normally, this happens when the font is missing in GDFONTPATH and can usually be resolved by using correct font folder, using correct font file and naming it properly in the PHP file. However, in this particular case, I’ve figured that this is the problem with GD Library or PHP because I am still getting the error even though I have done everything right. I tried WAMP but still in vain. When I tried the same in my Linux machine, everything was fine.

Here is what I did as a workaround. I removed the putenv line and referred to fonts by relative path. For example, the following sample code is the one that does not work

putenv('GDFONTPATH=' . realPath('fonts'));
$font="ariali";
imagettftext($image, $size, $angle, $xcordinate, $ycordinate, $text_color, $font, $text);

I have assumed that the fonts folder contains a file ariali.ttf and in that case, the code must have worked. However, it does not, so the workaround is to do the following:

//putenv('GDFONTPATH=' . realPath('fonts')); remove this line
$font="fonts/ariali.ttf"; //use relative path here instead
imagettftext($image, $size, $angle, $xcordinate, $ycordinate, $text_color, $font, $text);

The above code works and I guess this is how I will have to use fonts in PHP from now on.

If you enjoyed this post, make sure you subscribe to my RSS feed!
  • Nice brief and this post helped me alot in my college assignement. Say thank you you as your information.

  • Mahasakthi49

    what is the error in this line? i searched so many times but i didn’t got it

    Notice: Undefined index: rand_code in C:wampwwwtestrandom.php on line  

  • Mahasakthi49

    what is the error in this line? i searched so many times but i didn’t got it

    Notice: Undefined index: rand_code in C:wampwwwtestrandom.php on line