时间:2021-07-01 10:21:17 帮助过:18人阅读
ob_clean();
header("content-type: image/jpeg");
//生成验证码
$char = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$len = 4;
$schar = '';
$charlen = strlen($char);
for ($i=0; $i < $len; $i++) {
$schar .= $char[rand(0,$charlen)];
}
//将结果保存到 session中
@session_start();
$_SESSION['captcha_code'] = $schar;
//读取图片
$bg_file = './captcha/captcha_bg' . mt_rand(1,5) . '.jpg';
//根据图片创建画布
$img = imagecreatefromjpeg($bg_file);
if (rand(1,2) == 1) {
$color = imagecolorallocate($img, 0, 0, 0 );
}else {
$color = imagecolorallocate($img, 255, 255, 255);
}
$imgsize = getimagesize($bg_file);
imagestring($img, 5, 30, 0, $schar,$color );
imagepng($img);
imagedestroy($img);
$data = ob_get_contents();
ob_clean();
var_dump($data);