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?
It's probably looking in the wrong directory. Try changing monofont.ttf
to ./monofont.ttf
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With