PHP验证码找不到TTF文件


PHP Captcha can't find the TTF file

我需要添加验证码到我的网站,但由于某种原因,我得到这个错误(从日志文件):

Warning: imagettfbbox(): Could not find/open font in /home/eric/www/captcha.php on line 24
Warning: imagettftext(): Could not find/open font in /home/eric/www/captcha.php on line 27

下面是我的代码:

function generate($width,$height,$characters='6') {
    $code = '';
    $availableChar = '23456789bcdfghjkmnpqrstvwxyz';
    $i = 0;
    while ($i < $characters) { 
        $code .= substr($availableChar, mt_rand(0, strlen($availableChar)-1), 1);
        $i++;
    }
    $image = imagecreate($width, $height);
    $text = imagecolorallocate($image, 20, 40, 100);
    $things = imagecolorallocate($image, 100, 120, 180);
    for( $i=0; $i<($width*$height)/3; $i++ ) {
        imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $things);
    }
    for( $i=0; $i<($width*$height)/150; $i++ ) {
        imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $things);
    }
    $textbox = imagettfbbox($height, 0, 'monofont.ttf', $code);
    imagettftext($image, $height, 0, (($width - $textbox[4])/2), (($height - $textbox[5])/2), $text, 'monofont.ttf' , $code);
    imagejpeg($image);
    imagedestroy($image);
    return $code;
}
header('Content-Type: image/jpeg');
$code = generate(100, 40, 5);
echo $_SESSION['security_code'] = $code;

和我文件夹中的文件:

-rwxr-xr-x 1 tech4wilco tech4wilco 41036 1999-07-13 23:30 monofont.ttf
-rw-r--r-- 1 tech4wilco tech4wilco  1030 2011-10-17 15:43 captcha.php

我做了一些挖掘,我发现的是字体需要在那里,正如你所看到的,它是在同一个文件夹的PHP文件,我没有理解什么吗?

它可能在错误的目录中查找。尝试将monofont.ttf更改为./monofont.ttf

它与PHP文件不在同一个文件夹中,因为您运行的是Test.php而不是capcha . PHP。试着将它与test.php放在同一个文件夹中,或者使用正确的路径来访问它。

目录中没有monofont.ttf文件