generateCode(4); Session::put('captcha', $code); $width = 120; $height = 44; $img = imagecreatetruecolor($width, $height); // 背景 $bg = imagecolorallocate($img, 244, 247, 250); imagefilledrectangle($img, 0, 0, $width, $height, $bg); // 干扰线 for ($i = 0; $i < 6; $i++) { $c = imagecolorallocate($img, rand(160, 210), rand(160, 210), rand(160, 210)); imageline($img, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $c); } // 字符(随机色 + 轻微纵向抖动) $palette = [[74, 138, 244], [110, 140, 176], [43, 182, 164], [123, 126, 240], [200, 120, 60]]; $len = strlen($code); $step = (int) ($width / ($len + 1)); for ($i = 0; $i < $len; $i++) { $col = $palette[array_rand($palette)]; $c = imagecolorallocate($img, $col[0], $col[1], $col[2]); $x = $step * ($i + 1) - 10 + rand(-3, 3); $y = rand(8, 18); imagestring($img, 5, $x, $y, $code[$i], $c); } // 噪点 for ($i = 0; $i < 50; $i++) { $c = imagecolorallocate($img, rand(180, 225), rand(180, 225), rand(180, 225)); imagesetpixel($img, rand(0, $width), rand(0, $height), $c); } ob_start(); imagepng($img); $data = ob_get_clean(); imagedestroy($img); return response($data, 200) ->header('Content-Type', 'image/png') ->header('Cache-Control', 'no-store, no-cache, must-revalidate, private') ->header('Pragma', 'no-cache'); } /** * 生成验证码字符(去除易混淆的 0/O/1/I/L)。 */ protected function generateCode(int $len): string { $pool = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; $out = ''; for ($i = 0; $i < $len; $i++) { $out .= $pool[random_int(0, strlen($pool) - 1)]; } return $out; } }