@@ -10,24 +10,27 @@ class Captcha
10
10
{
11
11
private $ str = "ABCDEFGHIJKLMNOPQRSTUVWXYZaabcdefghijklmnopqrstuvwxyz1234567890 " ;
12
12
private $ font ;
13
- private $ im ;
13
+ private $ imageResource ;
14
14
private $ level ;
15
15
private $ code ;
16
+ private $ caseSensitive ;
16
17
17
18
/**
18
19
* Captcha constructor.
19
20
* @param int $level
21
+ * @param boolean $caseSensitive
20
22
*/
21
- public function __construct ($ level = 2 )
23
+ public function __construct ($ level = 2 , $ caseSensitive = false )
22
24
{
23
25
$ this ->level = $ level ;
26
+ $ this ->caseSensitive = $ caseSensitive ;
24
27
25
- // load the truetype font
26
- $ this ->font = realpath ( ' . /arial.ttf ') ;
28
+ // load the TrueType font
29
+ $ this ->font = __DIR__ . ' /arial.ttf ' ;
27
30
28
31
// create image
29
- $ this ->im = imagecreate (200 , 77 );
30
- imagecolorallocate ($ this ->im , 255 , 255 , 255 );
32
+ $ this ->imageResource = imagecreate (200 , 77 );
33
+ imagecolorallocate ($ this ->imageResource , 255 , 255 , 255 );
31
34
32
35
// generate
33
36
$ this ->generate ();
@@ -43,7 +46,7 @@ public function __construct($level = 2)
43
46
*/
44
47
public function getImageResource ()
45
48
{
46
- return $ this ->im ;
49
+ return $ this ->imageResource ;
47
50
}
48
51
49
52
/**
@@ -61,8 +64,8 @@ public function render()
61
64
{
62
65
header ('Content-Type: image/png ' );
63
66
64
- imagepng ($ this ->im );
65
- imagedestroy ($ this ->im );
67
+ imagepng ($ this ->imageResource );
68
+ imagedestroy ($ this ->imageResource );
66
69
}
67
70
68
71
/**
@@ -81,10 +84,24 @@ private function generate()
81
84
82
85
// get font size
83
86
$ fontSize = ($ this ->level > 1 ) ? rand (20 , 48 ) : 28 ;
84
- imagettftext ($ this ->im , $ fontSize , rand (-35 , 35 ), 35 * $ i , 55 , imagecolorallocate ($ this ->im , rand (0 , 240 ), rand (0 , 240 ), rand (0 , 240 )), $ this ->font , $ char );
87
+ imagettftext ($ this ->imageResource , $ fontSize , rand (-35 , 35 ), 35 * $ i , 55 , imagecolorallocate ($ this ->imageResource , rand (0 , 240 ), rand (0 , 240 ), rand (0 , 240 )), $ this ->font , $ char );
85
88
}
86
89
87
- $ this ->code = $ output ;
90
+ $ this ->code = ($ this ->caseSensitive ) ? $ output : strtolower ($ output );
91
+ }
92
+
93
+ /**
94
+ * @param $str
95
+ * @return bool
96
+ * check the code
97
+ */
98
+ public function check ($ str ){
99
+ if (!$ this ->caseSensitive ){
100
+ return strtolower ($ str ) == strtolower ($ this ->code );
101
+ }
102
+ else {
103
+ return $ str == $ this ->code ;
104
+ }
88
105
}
89
106
90
107
/**
@@ -95,11 +112,11 @@ private function addLines()
95
112
96
113
$ lines = rand (1 , 3 );
97
114
for ($ i = 0 ; $ i < $ lines ; $ i ++) {
98
- imageline ($ this ->im , rand (0 , 200 ), rand (0 , -77 ), rand (0 , 200 ), rand (77 , 144 ), imagecolorallocate ($ this ->im , rand (0 , 240 ), rand (0 , 240 ), rand (0 , 240 )));
115
+ imageline ($ this ->imageResource , rand (0 , 200 ), rand (0 , -77 ), rand (0 , 200 ), rand (77 , 144 ), imagecolorallocate ($ this ->imageResource , rand (0 , 240 ), rand (0 , 240 ), rand (0 , 240 )));
99
116
}
100
117
101
118
for ($ i = 0 ; $ i < 5 - $ lines ; $ i ++) {
102
- imageline ($ this ->im , rand (0 , -200 ), rand (0 , 77 ), rand (200 , 400 ), rand (0 , 77 ), imagecolorallocate ($ this ->im , rand (0 , 240 ), rand (0 , 240 ), rand (0 , 240 )));
119
+ imageline ($ this ->imageResource , rand (0 , -200 ), rand (0 , 77 ), rand (200 , 400 ), rand (0 , 77 ), imagecolorallocate ($ this ->imageResource , rand (0 , 240 ), rand (0 , 240 ), rand (0 , 240 )));
103
120
}
104
121
}
105
122
}
0 commit comments