Invalid font filename

trying to learn php extension “GD”. Running local (http://localhost/imageoverlay/helloworld.php) under Apache Version 2.0; Firefox 21.0; Internet Explorer 8.0.

My code generates an unhelpful firefox message

The image “http://localhost/imageoverlay/helloworld.php” cannot be displayed because it contains errors"

and, running under IE, a more helpful message

<br />
<b>Warning</b>: imagettftext() [<a href=‘function.imagettftext’>function.imagettftext</a>]: Invalid font filename in <b>C:\xampp\htdocs\imageoverlay\helloworld.php</b> on line <b>29</b><br />
‰PNG
"

Line 29 is the

imagettftext($image, 50, 0, 10, 160, $white, $font_path, $string);

which I conclude from the IE message, and a google search on “Invalid font filename” complains about the $font_path which I’ve tried a number of different ways – to wit:

//Set the path to our true type font
// $font_path = 'advent_light';
// $font_path = 'arial';
// $font_path = '/arial.tff';
// $font_path = dirname(__FILE__) . '/arial.ttf';
$font_path = dirname(__FILE__) . '/fonts/arial.ttf';

(the ‘advent_light’ is from the tutorial I’m using.)

Guessing that my local address for the font is not the correct one. If so, how do I determine what path to use? If not, what might the issue be?

Here is the full script

<?php
// source: http://blog.themeforest.net/tutorials/fun-with-the-php-gd-library-part-1/

//Report any errors
ini_set("display_errors", "1");
error_reporting(E_ALL);

//Set the content type
header('content-type: image/png');

//Create our basic image stream 300x300 pixels
$image = imagecreate(300, 300);

//Set up some colors, use a dark gray as the background color
$dark_grey = imagecolorallocate($image, 102, 102, 102);
$white = imagecolorallocate($image, 255, 255, 255);

//Set the path to our true type font
// $font_path = 'advent_light';
// $font_path = 'arial';
// $font_path = '/arial.tff';
// $font_path = dirname(__FILE__) . '/arial.ttf';
$font_path = dirname(__FILE__) . '/fonts/arial.ttf';

//Set our text string
$string = 'Hello World!';

//Write our text to the existing image.
imagettftext($image, 50, 0, 10, 160, $white, $font_path, $string);

//Create our final image
imagepng($image);

//Clear up memory
imagedestroy($image);
?>

Regards,
grNadpa

Hi Grnadpa,

Where is the file arial.ttf in relation to your script that is trying to use it?

I guess that’s what I’m asking as my tutorial (http://blog.themeforest.net/tutorials/fun-with-the-php-gd-library-part-1/) offers no suggestion and the internet google searches resulted in my 5 alternative attempts:

// $font_path = 'advent_light';
// $font_path = 'arial';
// $font_path = '/arial.tff';
// $font_path = dirname(__FILE__) . '/arial.ttf';
$font_path = dirname(__FILE__) . '/fonts/arial.ttf';

Doing a search on my C: drive (operating system: windows/xp) shows arial.ttf in folders
C:/WINDOWS\$NtServicePackUninstall$
C:/WINDOWS\Fonts
C:/WINDOWS\$NtServicePackFiles\i386

For testing I would upload the arial.ttf font file to the same folder as your code and use $font_path = ‘arial.tff’;

Once it is working you can change location etc.

Bear in mind that the font file must be somewhere on your server and NOT on your machine!
As Rubble suggests, upload or copy the file to your localhost server into the same folder.

That worked.

Following your advice I moved the arial.ttf file into the folder that contains helloworld.php. Then using

$font_path = dirname(__FILE__) . '/arial.ttf';

the error disappeared and a portion of the string appeared. (No doubt there is a font size argument I can use to reduce the text to 300px wide).

I gather that I will have to load my fonts onto my own folder on my host’s server as well? Seems like a giant step backward if I do. Will worry about that if and when I move it to production.

Thanks again folks – I doubt that I would have arrived at that solution on my own.

regards,

grNadpa

I gather that I will have to load my fonts onto my own folder on my host’s server as well? Seems like a giant step backward if I do.

There are fonts installed on servers and Imagemagick regesters them when it is installed and so you do not need the path; GD may be the same. I always upload a copy of the font as most of the fonts I use are not ones that are installed by default onto the server.

You could try running your code on the server and see what happens.