牛骨文教育服务平台(让学习变的简单)
博文笔记

GD Warning: imagettfbbox(): Could not find/open font

创建时间:2013-09-23 投稿人: 浏览次数:2179

在运行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以下版本的小缺陷吧。

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。