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