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( '', $w, $h, $w, $h ); $svg[] = sprintf('', $w, $h, $bg); // 干扰线 for ($i = 0; $i < 4; $i++) { $svg[] = sprintf( '', random_int(-8, $w), random_int(0, $h), random_int(-8, $w), random_int(0, $h), $line ); } // 干扰曲线 $svg[] = sprintf( '', 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( '', 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( '%s', $x, $y, $size, htmlspecialchars($code[$i], ENT_QUOTES), $rotate, $cx, $cy, htmlspecialchars($code[$i], ENT_QUOTES) ); } $svg[] = ''; return implode('', $svg); } }