Files

133 lines
4.1 KiB
PHP

<?php
namespace App\Services;
/**
* 图形数字验证码(SVG 输出,无 GD 扩展依赖)
* 一次性使用:生成即写入 Session,校验后立即失效
*/
class CaptchaService
{
/** 验证码位数 */
public const LENGTH = 4;
/** 有效期(秒) */
public const TTL = 300;
/**
* 生成验证码:写入 Session 并返回 [code, svg]
* @return array{0:string,1:string}
*/
public static function generate(): array
{
$code = '';
for ($i = 0; $i < self::LENGTH; $i++) {
$code .= (string) random_int(0, 9);
}
if (session_status() === PHP_SESSION_ACTIVE) {
$_SESSION['captcha_code'] = $code;
$_SESSION['captcha_time'] = time();
}
return [$code, self::render($code)];
}
/**
* 校验验证码(比对后立即失效)
*/
public static function verify(?string $input): bool
{
$code = (string) ($_SESSION['captcha_code'] ?? '');
$time = (int) ($_SESSION['captcha_time'] ?? 0);
unset($_SESSION['captcha_code'], $_SESSION['captcha_time']);
if ($code === '' || $time <= 0) {
return false;
}
if (time() - $time > self::TTL) {
return false;
}
$input = trim((string) $input);
if ($input === '') {
return false;
}
return hash_equals($code, $input);
}
/**
* 渲染 SVG 图形
*/
private static function render(string $code): string
{
$w = 132;
$h = 40;
$textColors = ['#1f2937', '#374151', '#1e3a8a', '#3f3f46', '#4c1d95', '#7c2d12', '#065f46'];
$bgColors = ['#f8fafc', '#f1f5f9', '#fdf4ff', '#ecfeff', '#f7fee7'];
$lineColors = ['#c7d2fe', '#fbcfe8', '#a5f3fc', '#bbf7d0'];
$bg = $bgColors[random_int(0, count($bgColors) - 1)];
$line = $lineColors[random_int(0, count($lineColors) - 1)];
$svg = [];
$svg[] = sprintf(
'<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" viewBox="0 0 %d %d">',
$w, $h, $w, $h
);
$svg[] = sprintf('<rect width="%d" height="%d" fill="%s"/>', $w, $h, $bg);
// 干扰线
for ($i = 0; $i < 4; $i++) {
$svg[] = sprintf(
'<line x1="%d" y1="%d" x2="%d" y2="%d" stroke="%s" stroke-width="1.6" stroke-opacity="0.75"/>',
random_int(-8, $w), random_int(0, $h),
random_int(-8, $w), random_int(0, $h),
$line
);
}
// 干扰曲线
$svg[] = sprintf(
'<path d="M0 %d Q %d %d %d %d T %d %d" fill="none" stroke="%s" stroke-width="1.6" stroke-opacity="0.6"/>',
random_int(8, 40),
(int) ($w / 4), random_int(0, $h),
(int) ($w / 2), random_int(0, $h),
$w, random_int(8, 40),
$line
);
// 噪点
for ($i = 0; $i < 26; $i++) {
$svg[] = sprintf(
'<circle cx="%d" cy="%d" r="%d" fill="#64748b" fill-opacity="0.3"/>',
random_int(0, $w), random_int(0, $h), random_int(1, 2)
);
}
// 数字(逐位随机大小/旋转/位置)
$len = strlen($code);
$cell = (int) (($w - 24) / $len);
for ($i = 0; $i < $len; $i++) {
$x = 16 + $i * $cell + random_int(-3, 3);
$y = random_int(28, 33);
$size = random_int(22, 27);
$rotate = random_int(-22, 22);
$cx = $x + 8;
$cy = $y - 7;
$color = $textColors[random_int(0, count($textColors) - 1)];
$svg[] = sprintf(
'<text x="%d" y="%d" font-size="%d" font-family="Consolas, Monaco, \'Courier New\', monospace" font-weight="bold" fill="%s" transform="rotate(%d %d %d)">%s</text>',
$x, $y, $size, htmlspecialchars($code[$i], ENT_QUOTES),
$rotate, $cx, $cy, htmlspecialchars($code[$i], ENT_QUOTES)
);
}
$svg[] = '</svg>';
return implode('', $svg);
}
}