在php中显示验证码

时间:2012-10-01 06:37:37

标签: php captcha

当我显示验证码图像时,出现以下错误: -

警告(2):imagettftext()[function.imagettftext]:找不到/打开字体 我已经在root上保存了字体文件。

这是我的代码 -

$path = fullpath to site images folder
$imgname = 'noise.jpg';
$imgpath  = $path.'/'.$imgname;

$captchatext = md5(time());
$captchatext = substr($captchatext, 0, 5);
$_SESSION['captchatext'] = $captchatext;
$im = imagecreatefromjpeg($imgpath); 
$grey = imagecolorallocate($im, 128, 128, 128);
$font = site_root_directory_path/BIRTH_OF_A_HERO.ttf;
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = $_SESSION['captchatext'];
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);

1 个答案:

答案 0 :(得分:1)

您的字体路径必须是绝对的或相对于脚本。

例如:

$font = '/var/www/website/BIRTH_OF_A_HERO.ttf'; - absolute
$font = '../../BIRTH_OF_A_HERO.ttf'; - relative