imagettftext() - Cannot find font location

2019-06-07 22:45发布

问题:

I am using the imagettftext() function to create a custom captcha script.

I have ran and tested the code successfully on my PC, running PHP (Version 5.3.8) but when I upload to my shared hosting account running PHP (Version 5.2.17) it comes up with the following error

Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/pheelco/public_html/captcha.php on line 17

This is line 17;

imagettftext($img, 25, 0, 0, 25, $text, "fonts/alger.ttf", $num);

I definitely have the folder "fonts" uploaded with that font, its in the public_html folder.

Any ideas?

Note: I don't have access to change any PHP configurations, as I'm using a shared hosting account.

回答1:

The imagettftext documentation says:

fontfile

The path to the TrueType font you wish to use.

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.

When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.

In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.

<?php // Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont'; ?>

Did you try this? You can use gd_info to check which version of GD you are working with.



回答2:

Solved. It turned out the file name was "alger.TTF" with a capitalised file extension and in my code I was referring to it with a lower-case extension. For some reason this still worked in PHP 5.3.8 but not in PHP 5.2.17 I hope this helps anybody else that comes across this issue