Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Captcha can't find the TTF file

Tags:

php

captcha

I need to add a captcha to my site but for some reason I get this error (from the log 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

Here's my code:

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;

and the file in my folder:

-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

I did some digging and what I found is the font needs to be there and as you can see it's in the same folder as the PHP file, Am I not understanding something?

like image 397
Tech4Wilco Avatar asked Oct 17 '11 19:10

Tech4Wilco


1 Answers

It's probably looking in the wrong directory. Try changing monofont.ttf to ./monofont.ttf.

like image 147
MVS Avatar answered Sep 20 '22 13:09

MVS