<?php
session_start();
// 生成随机验证码
function generate_captcha($length = 4) {
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $chars[mt_rand(0, strlen($chars) - 1)];
}
return $code;
}
// 生成图形验证码
function generate_captcha_image($captcha) {
$width = 100;
$height = 40;
$font_size = 20;
// 创建画布
$image = imagecreatetruecolor($width, $height);
// 生成背景色
$bg_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bg_color);
// 生成干扰线
for ($i = 0; $i < 5; $i++) {
$line_color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $line_color);
}
// 生成验证码文字
for ($i = 0; $i < strlen($captcha); $i++) {
$text_color = imagecolorallocate($image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
imagettftext($image, $font_size, mt_rand(-15, 15), 20 + $i * ($font_size + 10), mt_rand($height / 2, $height), $text_color, './arial.ttf', $captcha[$i]);
}
// 输出图像
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
}
// 生成验证码
$captcha = generate_captcha();
// 将验证码保存到SESSION中
$_SESSION['captcha'] = $captcha;
// 生成图形验证码图片并输出
generate_captcha_image($captcha);
?>
这个是chatgpt生成的实例
@兲蛋,你有
./arial.ttf
这个字体吗