Files
Fuxsto-V3/user/cap.php
2025-10-18 14:46:52 +08:00

70 lines
2.0 KiB
PHP

<?php
session_start();
// 获取用户代理
$user_agent = $_SERVER['HTTP_USER_AGENT'];
// 设置图像宽度和高度
if (preg_match('/Mobile|Android|iPhone/i', $user_agent)) {
// 适合手机的尺寸
$width = 120; // 手机端使用默认宽度
$height = 50; // 手机端使用较小高度
} else {
// 适合PC的尺寸
$width = 120; // PC端保持宽度
$height = 50; // PC端设置为正方形高度
}
// 创建图像
$image = imagecreatetruecolor($width, $height);
// 分配背景颜色
$background_color = imagecolorallocate($image, 255, 255, 255); // 白色背景
imagefilledrectangle($image, 0, 0, $width, $height, $background_color);
// 生成随机验证码
$captcha_code = '';
for ($i = 0; $i < 5; $i++) {
$char = chr(rand(97, 122)); // 生成小写字母
$captcha_code .= $char;
}
// 将验证码存储到会话中
$_SESSION['captcha'] = $captcha_code;
// 在图像上添加随机线条
$line_color = imagecolorallocate($image, 200, 200, 200); // 灰色线条
for ($i = 0; $i < 5; $i++) {
imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $line_color);
}
// 绘制每个字符,并设置不同的随机颜色
$font_size = 5; // 字体大小
$text_length = strlen($captcha_code);
$padding = 10; // 边距
// 根据当前的宽度计算字符间隔
$x_interval = ($width - $padding * 2) / $text_length;
for ($i = 0; $i < $text_length; $i++) {
// 随机颜色
$text_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
// 字符的位置
$x_position = $padding + ($i * $x_interval); // 调整字符之间的间距
$y_position = rand($padding, $height - $padding - 15); // 随机垂直位置
// 添加字符到图像
imagestring($image, $font_size, $x_position, $y_position, $captcha_code[$i], $text_color);
}
// 设置内容类型为图像
header('Content-Type: image/png');
// 输出图像
imagepng($image);
// 释放内存
imagedestroy($image);
?>