Search code examples
phpfontsimagettftext

Why could imagettfbbox() not read font in x on line y?


Test:

<?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;
}
?>

Result:

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

As you can see I tested all the answers from these questions but they did not help:

Maybe an interesting info regarding GDFONTPATH. If I set it to:

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

Then the error message says Could not find/open instead of Could not read :

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

Because of that I do not think setting GDFONTPATH solves the issue.

P.S. The server is using PHP Version 5.5.38 and LiteSpeed V6.10


Solution

  • The problem was solved by re-uploading the font files. They were uploaded through Filezilla in ASCII mode instead of the correct Binary mode. By that the font files became corrupt.

    P.S. I did not set GDFONTPATH and it works with all those paths without problems:

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