PHP GD gives error
Update: The TTF file seems to exist after testing. Update: changed to a relative path for the font file. Still not working.
I am getting the following error when I try to render an image with GD via PHP.
[Tue Sep 01 19:44:15 2009] [error] [IP ADDRESS client] 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 gave me the same error. I added the font to the server by dropping the file into a 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);
}
source to share
This is probably a font file in a format unknown to your libgd version.
The gd section of the phpinfo () output should contain the FreeType / T1Lib version of the library. Which one? And what does
echo '<pre>Debug: '; passthru('file '.$font); echo "</pre>\n";
// imagettftext($this->im, $fsize, $X, $Y, $fsize, $text_color, $font, $text);
type?
Edit: oops, forget about the font file type. There will be an error for this Could not read font
. Could not find/open font
really means that it says either there is no such file or it cannot be accessed. Is the
output signal passthru('file '.$font);
"only" CENTURY.TTF: TrueType font data
? Then you used a relative path. Try passing the 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);
source to share
You may be calling a 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 that the font path is absolute and does not start with a forward slash, the man page for imagettftext states
Depending on which version of the GD PHP library is using, when the fontfile does not start with a leading / then .ttf will be appended to the filename. and the library will try to search for that filename along the library-specified font path.
source to share