Repository navigation
Expand file tree
/
Copy pathcaptcha.php
More file actions
48 lines (42 loc) · 1.46 KB
/
Copy pathcaptcha.php
File metadata and controls
48 lines (42 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
function generateCaptcha() {
// Generate the captcha code and image
// ...
$width = 200;
$height = 50;
$fontFile = __DIR__ . '/arial.ttf';
$fontSize = 24;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);
// Generate random noise
$noiseColor = imagecolorallocate($image, 0, 0, 0);
for ($i = 0; $i < $width; $i++) {
for ($j = 0; $j < $height; $j++) {
if (rand(0, 100) < 25) { // 10% chance of adding noise
imagesetpixel($image, $i, $j, $noiseColor);
}
}
}
// Generate random string
$captchaLength = 6;
$captchaChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$captchaString = '';
for ($i = 0; $i < $captchaLength; $i++) {
$captchaString .= $captchaChars[rand(0, strlen($captchaChars) - 1)];
}
// Add text to image
$textColor = imagecolorallocate($image, 0, 0, 0);
$x = $width / $captchaLength;
for ($i = 0; $i < $captchaLength; $i++) {
$angle = rand(-15, 15);
imagettftext($image, $fontSize, $angle, $x * $i + rand(5, 10), rand($fontSize, $height - 10), $textColor, $fontFile, $captchaString[$i]);
}
ob_start();
imagepng($image);
$imageString = ob_get_clean();
return [
'code' => $captchaString,
'image' => base64_encode($imageString),
];
}