60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
|
<?php
|
||
|
session_start();
|
||
|
|
||
|
// 生成验证码
|
||
|
$code = '';
|
||
|
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||
|
for ($i = 0; $i < 4; $i++) {
|
||
|
$code .= $chars[rand(0, strlen($chars) - 1)];
|
||
|
}
|
||
|
|
||
|
$_SESSION['captcha'] = $code;
|
||
|
|
||
|
// 创建图片
|
||
|
$width = 100;
|
||
|
$height = 40;
|
||
|
$image = imagecreate($width, $height);
|
||
|
|
||
|
// 颜色定义
|
||
|
$bg_color = imagecolorallocate($image, 245, 245, 245);
|
||
|
$text_color = imagecolorallocate($image, 50, 50, 50);
|
||
|
$line_color = imagecolorallocate($image, 200, 200, 200);
|
||
|
$noise_color = imagecolorallocate($image, 180, 180, 180);
|
||
|
|
||
|
// 填充背景
|
||
|
imagefill($image, 0, 0, $bg_color);
|
||
|
|
||
|
// 添加干扰线
|
||
|
for ($i = 0; $i < 5; $i++) {
|
||
|
imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $line_color);
|
||
|
}
|
||
|
|
||
|
// 添加噪点
|
||
|
for ($i = 0; $i < 50; $i++) {
|
||
|
imagesetpixel($image, rand(0, $width), rand(0, $height), $noise_color);
|
||
|
}
|
||
|
|
||
|
// 添加验证码文字
|
||
|
for ($i = 0; $i < 4; $i++) {
|
||
|
$x = 15 + $i * 18;
|
||
|
$y = rand(8, 15);
|
||
|
$angle = rand(-15, 15);
|
||
|
|
||
|
if (function_exists('imagettftext')) {
|
||
|
// 如果支持TTF字体
|
||
|
imagettftext($image, 16, $angle, $x, 25, $text_color, __DIR__ . '/arial.ttf', $code[$i]);
|
||
|
} else {
|
||
|
// 使用内置字体
|
||
|
imagestring($image, 5, $x, $y, $code[$i], $text_color);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 输出图片
|
||
|
header('Content-Type: image/png');
|
||
|
header('Cache-Control: no-cache, no-store, must-revalidate');
|
||
|
header('Pragma: no-cache');
|
||
|
header('Expires: 0');
|
||
|
|
||
|
imagepng($image);
|
||
|
imagedestroy($image);
|
||
|
?>
|