【问题标题】:Why could imagettfbbox() not read font in x on line y?为什么 imagettfbbox() 不能在第 y 行读取 x 中的字体?
【发布时间】:2017-03-30 09:50:06
【问题描述】:

测试:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
?>
<pre>
<?php
//putenv('GDFONTPATH=' . realpath('.'));
print_r(gd_info());
echo 'phpuser: ' . get_current_user() . PHP_EOL;
echo 'scriptuser: ' . getmyuid() . PHP_EOL;
echo 'scriptgroup: ' . getmygid() . PHP_EOL;
$files = glob('captcha/fonts/*.ttf');
foreach ($files as $file) {
    echo 'file: ' . $file . PHP_EOL;
    echo 'fullpath: ' . __DIR__ . '/' . $file . PHP_EOL;
    echo 'chmod: ' . decoct(fileperms($file) & 0777) . PHP_EOL;
    echo 'fileowner: ' . fileowner($file) . PHP_EOL;
    echo 'user: ' . print_r(posix_getpwuid(fileowner($file)), true);
    echo 'filegroup: ' . filegroup($file) . PHP_EOL;
    echo 'group: ' . print_r(posix_getgrgid(filegroup($file)), true);
    echo 'isfile: ' . (is_file($file) ? 'yes' : 'no') . PHP_EOL;
    echo 'readable: ' . (is_readable($file) ? 'yes' : 'no') . PHP_EOL;
    echo 'writeable: ' . (is_writeable($file) ? 'yes' : 'no') . PHP_EOL;
    $size = mt_rand(10, 20);
    $angle = mt_rand(-35, 30);
    echo 'imagettfbbox with ' . $file . ':';
    imagettfbbox($size, $angle, $file, 'a');
    echo 'imagettfbbox with ' . str_replace('.ttf', '', $file) . ':';
    imagettfbbox($size, $angle, str_replace('.ttf', '', $file), 'a');
    echo 'imagettfbbox with ' . __DIR__ . '/' . $file . ':';
    imagettfbbox($size, $angle, __DIR__ . '/' . $file, 'b');
    echo PHP_EOL;
}
?>

结果:

Array
(
    [GD Version] => bundled (2.1.0 compatible)
    [FreeType Support] => 1
    [FreeType Linkage] => with freetype
    [T1Lib Support] => 1
    [GIF Read Support] => 1
    [GIF Create Support] => 1
    [JPEG Support] => 1
    [PNG Support] => 1
    [WBMP Support] => 1
    [XPM Support] => 1
    [XBM Support] => 1
    [JIS-mapped Japanese Font Support] => 
)
phpuser: user1
scriptuser: 825
scriptgroup: 820
file: captcha/fonts/assimila.ttf
fullpath: /home/.../captcha/fonts/assimila.ttf
chmod: 644
fileowner: 825
user: Array
(
    [name] => user1
    [passwd] => x
    [uid] => 825
    [gid] => 820
    [gecos] => 
    [dir] => /home/user1
    [shell] => /bin/bash
)
filegroup: 820
group: Array
(
    [name] => user1
    [passwd] => x
    [members] => Array
        (
            [0] => user1
        )

    [gid] => 820
)
isfile: yes
readable: yes
writeable: yes
imagettfbbox with captcha/fonts/assimila.ttf:
Warning:  imagettfbbox(): Could not read font in /home/.../test.php on line 27

imagettfbbox with captcha/fonts/assimila:
Warning:  imagettfbbox(): Could not read font in /home/.../test.php on line 29

imagettfbbox with /home/.../captcha/fonts/assimila.ttf:
Warning:  imagettfbbox(): Could not read font in /home/.../test.php on line 31

如您所见,我测试了这些问题的所有答案,但没有帮助:

也许是关于GDFONTPATH 的有趣信息。如果我将其设置为:

putenv('GDFONTPATH=' . realpath('.') . '/captcha/fonts/');

然后错误消息显示Could not find/open 而不是Could not read

Warning:  imagettfbbox(): Could not find/open font in /home/.../test.php on line 27

因此,我认为设置 GDFONTPATH 不能解决问题。

附:服务器使用 PHP 版本 5.5.38 和 LiteSpeed V6.10

【问题讨论】:

  • 听起来您将putenv('GDFONTPATH=' . realpath('.') . '/captcha/fonts/') 设置为不存在的路径。
  • 你没有阅读我的剧本。我也用正确的路径对其进行了测试(第 7 行)。
  • 第 7 行是 //putenv('GDFONTPATH=' . realpath('.'));,这并没有真正做太多,但即使未注释也不会给我任何迹象表明您正在使用正确的路径。您能否更新您的帖子以解释文件系统布局(哪些文件在哪些目录中),以便我们了解您的路径实际上会做什么? (理想情况下,你不要手动写出来,而是从 dir 结构读数中获取它,因为你的手指可以输入你认为布局应该是什么,丢失路径名中多余或更少的字母等内容)

标签: php fonts imagettftext


【解决方案1】:

通过重新上传字体文件解决了问题。它们是通过 Filezilla 以 ASCII 模式而不是正确的二进制模式上传的。这样字体文件就会损坏。

附:我没有设置GDFONTPATH,它适用于所有这些路径而没有问题:

captcha/fonts/assimila.ttf
captcha/fonts/assimila
/home/{Userpath}/captcha/fonts/assimila.ttf

【讨论】:

  • 这也解决了我的问题。我的代码多年来一直运行良好,然后突然停止了。是字体文件损坏了。我重新上传了字体(二进制模式),现在它可以工作了!谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多