50 lines
1.5 KiB
PHP
Executable File
50 lines
1.5 KiB
PHP
Executable File
<?php
|
||
session_start();
|
||
|
||
$width = 120;
|
||
$height = 42;
|
||
$fontSize = 20;
|
||
|
||
$image = imagecreatetruecolor($width, $height);
|
||
|
||
$bgColor = imagecolorallocate($image, 255, 255, 255);
|
||
imagefill($image, 0, 0, $bgColor);
|
||
|
||
// 干扰线
|
||
for ($i = 0; $i < 5; $i++) {
|
||
$lineColor = imagecolorallocatealpha($image, rand(100, 200), rand(100, 200), rand(100, 200), 50);
|
||
imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $lineColor);
|
||
}
|
||
|
||
// 干扰点
|
||
for ($i = 0; $i < 80; $i++) {
|
||
$pixelColor = imagecolorallocate($image, rand(150, 200), rand(150, 200), rand(150, 200));
|
||
imagesetpixel($image, rand(0, $width), rand(0, $height), $pixelColor);
|
||
}
|
||
|
||
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
|
||
$code = '';
|
||
for ($i = 0; $i < 4; $i++) {
|
||
$code .= $chars[rand(0, strlen($chars) - 1)];
|
||
}
|
||
$_SESSION['captcha'] = $code;
|
||
|
||
// 尝试使用TTF字体,不存在则用内置字体
|
||
$fontFile = __DIR__ . '/assets/arial.ttf';
|
||
if (file_exists($fontFile)) {
|
||
for ($i = 0; $i < 4; $i++) {
|
||
$angle = rand(-15, 15);
|
||
$x = 8 + $i * 28;
|
||
$textColor = imagecolorallocate($image, rand(30, 100), rand(30, 100), rand(30, 100));
|
||
imagettftext($image, $fontSize, $angle, $x, 34, $textColor, $fontFile, $code[$i]);
|
||
}
|
||
} else {
|
||
$textColor = imagecolorallocate($image, 50, 50, 50);
|
||
imagestring($image, 5, 10, 12, $code, $textColor);
|
||
}
|
||
|
||
header('Content-Type: image/png');
|
||
header('Cache-Control: no-cache, no-store, must-revalidate');
|
||
imagepng($image);
|
||
imagedestroy($image);
|