我的PHP验证码无法正常工作。如何解决?

时间:2019-05-30 09:41:13

标签: php captcha

我已经为验证码编写了php代码,但是仍然无法正常工作,而且我看不到任何错误,只是无法正常工作。谁能告诉我问题出在哪里?

这是我的代码:

<?php

session_start();

$code = rand(1000,9999);

$backgrounds = array(
    'background1.png',
    'background2.png',
    'background3.png',
    'background4.png',
    'background5.png',
    'background6.png'
);
$background = $backgrounds[mt_rand(0, count($backgrounds) -1)];
list($bg_width, $bg_height, $bg_type, $bg_attr) = getimagesize($background);

$hex = "#666666";
list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
$color = imagecolorallocate($captcha, $r, $g, $b);

$fonts = array(
    'font1.ttf',
    'font2.ttf',
    'font3.ttf',
    'font4.ttf'
);
$font = $fonts[mt_rand(0, count($fonts) - 1)];
$font_size = rand(11,14);

$captcha = imagecreatefrompng($background);

$text_box = imagettfbbox($font_size,0,$font,$code);

$x = ($bg_width - $text_box[4])/2;
$y = ($bg_height - $text_box[5])/2;
imagettftext($captcha,$font_size,0,$x,$y,$color,$font,$code);

header("Content-type: image/png");
imagepng($captcha);

?>

0 个答案:

没有答案