tags:

views:

248

answers:

3
+1  Q: 

PHP GD gives error

Update: TTF file seems to be there after testing. Update: changed to a relative path for the font file. Still doesn't work.

I get the following error when I try to make an image using GD through PHP.

[Tue Sep 01 19:44:15 2009] [error] [client IP ADDRESS] PHP Warning: imagettftext() [function.imagettftext]: Could not find/open font in /www/vhosts/website.com/htdocs/trial/TextToImage.class.php on line 38

I changed the path for the font as it was giving me the same error. I added the font to the server by dropping the file into the folder. What am I missing?

/**
 * @name                    : makeImageF
 *
 * Function for create image from text with selected font.
 *
 * @param String $text     : String to convert into the Image.
 * @param String $font     : Font name of the text.
 * @param int    $W        : Width of the Image.
 * @param int    $H        : Hight of the Image.
 * @param int     $X        : x-coordinate of the text into the image.
 * @param int    $Y        : y-coordinate of the text into the image.
 * @param int    $fsize    : Font size of text.
 * @param array  $color       : RGB color array for text color.
 * @param array  $bgcolor  : RGB color array for background.
 *
 */
public function makeImageF($text, $font="/www/vhosts/website.com/htdocs/trial/CENTURY.TTF", $W=200, $H=20, $X=0, $Y=0, $fsize=18, $color=array(0x0,0x0,0x0), $bgcolor=array(0xFF,0xFF,0xFF)){

    $this->im = @imagecreate($W, $H)
        or die("Cannot Initialize new GD image stream");

    $background_color = imagecolorallocate($this->im, $bgcolor[0], $bgcolor[1], $bgcolor[2]);        //RGB color background.
    $text_color = imagecolorallocate($this->im, $color[0], $color[1], $color[2]);            //RGB color text.

    imagettftext($this->im, $fsize, $X, $Y, $fsize, $text_color, $font, $text);
}
+1  A: 

It may be the way you are calling the function - add this to the function to help you further

if (!file_exists($font))
    die("Font not found ($font)");
if (!is_readable($font))
    die("Font exists but not readable ($font)");

Also check the font path is absolute and doesn't start with a slash, the manual page for imagettftext states

Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

Paul Dixon
+1 for the process of elimination.
David Thomas
Tried this, it passes both these. No error is displayed.
Joe
A: 

Does the web server (the user that runs the web server) have read access to that folder/file?

Ólafur Waage
Is there a good way of testing for this? I can browse to the font file online and download it from my server. I would assume that would make your answer yes.
Joe
The answer by Paul Dixon, file_exists + is_readable, should test for existence of the file and its readability.
The Wicked Flea
+2  A: 

It's probably a font file in a format unknown to your version of libgd.
The gd section of the output of phpinfo() should include version of the FreeType/T1Lib library. Which one is it?
And what does

echo '<pre>Debug: '; passthru('file '.$font); echo "</pre>\n";
// imagettftext($this->im, $fsize, $X, $Y, $fsize, $text_color, $font, $text);

print?

Edit: oops, forget about the the type of the font file. The error for that would be Could not read font. Could not find/open font really means what it says: either there is no such file or it cannot be accessed.
The output of passthru('file '.$font); is "only" CENTURY.TTF: TrueType font data? Then you've used a relative path. Try passing an absolute path to imagettftext()

$font_realpath = realpath($font);
if ( !$font_realpath || !is_file($font_realpath) ) {
  die 'no such font file';
}
else if ( !is_readable($font_realpath) ) {
  die 'cannot read font file';
}
imagettftext($this->im, $fsize, $X, $Y, $fsize, $text_color, $font_realpath, $text);
VolkerK
Below is everything from the GD portion of the phpinfo file. So Freetype is 2.2.1. There is no t1lib on the phpinfo page at all.GD Support enabledGD Version bundled (2.0.28 compatible)FreeType Support enabledFreeType Linkage with freetypeFreeType Version 2.2.1GIF Read Support enabledGIF Create Support enabledJPG Support enabledPNG Support enabledWBMP Support enabledXBM Support enabled
Joe
Ok, now let's check the type of the font file.
VolkerK
Debug: CENTURY.TTF: TrueType font data I had to comment out the rest of the img.php file to get it to work. If that matters.
Joe