This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Captcha.class.php
70 lines (56 loc) · 2.02 KB
/
Captcha.class.php
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
class Captcha {
/**
* Creates a captcha consisting of random numbers.
*
* @param type $minNum lowest possible number
* @param type $maxNum highest possible number
* @param type $minAng minimum angle
* @param type $maxAng maximum angle
* @param type $size font size
* @param type $fontFolder font path
* @return type the random number for comparison
*/
function createCaptcha($minNum, $maxNum, $size, $fontFolder) {
$number = rand($minNum, $maxNum);
$angle = rand(-10, 10);
/* Calculating final image size */
$arSize = imagettfbbox($size, $angle, $fontFolder, $number);
$iWidth = abs($arSize[2] - $arSize[0]);
$iHeight = abs($arSize[7] - $arSize[1]);
$image = imagecreatetruecolor($iWidth * 1.25, $iHeight * 2);
/* Defining colors */
$red = imagecolorallocate($image, 255, 0, 0);
$lightgray = imagecolorallocate($image, 240, 240, 240);
imagefill($image, 0, 0, $lightgray);
ImageTTFText($image, $size, $angle, 10, $size * 1.25, $red, $fontFolder, $number);
$this->createRandomLines($image);
imagepng($image, 'output/captcha.png');
imagedestroy($image);
// $_SESSION['captcha'] = $number;
return $number;
}
/**
* Adds random lines for additional obfuscation.
*
* @param type $image
*/
function createRandomLines($image) {
$grey = imagecolorallocate($image, 150, 150, 150);
for ($i = 1; $i <= 200; $i += 10) {
$x1 = rand(-20, 0) + $i;
$y1 = rand(-20, 0) - $i;
$x2 = rand(100, 220) + $i;
$y2 = rand(100, 220) - $i;
imageline($image, $x1, $y1, $x2, $y2, $grey);
}
for ($i = 1; $i <= 200; $i = $i + 10) {
$x1 = rand(80, 95) - $i;
$y1 = rand(-10, 10) - $i;
$x2 = rand(-10, 1) - $i;
$y2 = rand(80, 250) - $i;
imageline($image, $x1, $y1, $x2, $y2, $grey);
}
}
}
?>