GD Warning imagettfbbox() Could not find/open font

清疚 2022-03-17 10:22 368阅读 0赞

在运行pChart的example的时候出现了这个问题,字体路径是没问题的,代码应该也无问题。网上找了一下,发现官网有相应的提示:http://php.net/manual/en/function.imagettftext.php。

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 appendedto 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’;
?>

遇到这个问题可能的原因是字体路径中出现了“.”,不巧的是,我的脚本就出现了。

这个问题的解决方案有两个:

第一种:就是按官网所说的,在使用imagettfbbox()函数前设置GDFONTPATH环境变量;

第二种:我发现使用字体的绝对路径,也是能够加载的。

网上有说法是linux不能用相对路径,这是错误的,算是GD2.0.18以下版本的小缺陷吧。

转载自:https://blog.csdn.net/zycamym/article/details/11913407

发表评论

表情:
评论列表 (有 0 条评论,368人围观)

还没有评论,来说两句吧...

相关阅读