Captcha.php
10.8 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
namespace Plugin;
use Hood\Session;
class Captcha {
private static $V = array(
"a",
"e",
"i",
"o",
"u",
"y"
);
private static $VN = array(
"a",
"e",
"i",
"o",
"u",
"y",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
);
private static $C = array(
"b",
"c",
"d",
"f",
"g",
"h",
"j",
"k",
"m",
"n",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"z"
);
private static $CN = array(
"b",
"c",
"d",
"f",
"g",
"h",
"j",
"k",
"m",
"n",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"z",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
);
private $_width = 160;
private $_height = 60;
private $_fs = 28;
private $_dotNoiseLevel = 50;
private $_lineNoiseLevel = 5;
private $_wordLen = 6;
private $_useNumbers = true;
/**
* Generate new random word
*
* @return string
*/
protected function _generateWord() {
$word = '';
$wordLen = $this->getWordLen ();
$vowels = $this->_useNumbers ? self::$VN : self::$V;
$consonants = $this->_useNumbers ? self::$CN : self::$C;
for($i = 0; $i < $wordLen; $i = $i + 2) {
$consonant = $consonants [array_rand ( $consonants )];
$vowel = $vowels [array_rand ( $vowels )];
$word .= $consonant . $vowel;
}
if (strlen ( $word ) > $wordLen) {
$word = substr ( $word, 0, $wordLen );
}
return $word;
}
public function getWordLen() {
return $this->_wordLen;
}
/**
* 设置验证码长度
*
* @param string $wordLen
* @return \Plugin\Captcha
*/
public function setWordLen($wordLen) {
$this->_wordLen = $wordLen;
return $this;
}
/**
* 获取图像宽度
*
* @return int
*/
public function getWidth() {
return $this->_width;
}
/**
* 获取图像高度
*
* @return int
*/
public function getHeight() {
return $this->_height;
}
/**
* 设置图像宽度
*
* @param int $width
* @return \Plugin\Captcha
*/
public function setWidth($width) {
$this->_width = $width;
return $this;
}
/**
* 设置图像高度
*
* @param int $height
* @return \Plugin\Captcha
*/
public function setHeight($height) {
$this->_height = $height;
return $this;
}
/**
*
*/
public function getFontSize() {
return $this->_fs;
}
public function setFontSize($fs) {
$this->_fs = $fs;
return $this;
}
/**
* 设置干扰点
*
* @param string $noise
* @return \Plugin\Captcha
*/
public function setDotNoiseLevel($noise = 100) {
$this->_dotNoiseLevel = $noise;
return $this;
}
/**
* 设置干扰线
*
* @param string $noise
* @return \Plugin\Captcha
*/
public function setLineNoiseLevel($noise = 5) {
$this->_lineNoiseLevel = $noise;
return $this;
}
/**
* 获取干扰点
*
* @return string
*/
public function getDotNoiseLevel() {
return $this->_dotNoiseLevel;
}
/**
* 获取干扰线
*
* @return string
*/
public function getLineNoiseLevel() {
return $this->_lineNoiseLevel;
}
/**
* Generate random frequency
*
* @return float
*/
protected function _randomFreq() {
return mt_rand ( 700000, 1000000 ) / 15000000;
}
/**
* Generate random phase
*
* @return float
*/
protected function _randomPhase() {
// random phase from 0 to pi
return mt_rand ( 0, 3141592 ) / 1000000;
}
/**
* Generate random character size
*
* @return int
*/
protected function _randomSize() {
return 0;
return mt_rand ( 300, 700 ) / 100;
}
public function getFont() {
return dirname ( __FILE__ ) . '/ttf/2.ttf';
}
/**
* 将验证码存入Sessioin
*
* @param string $namespace
* @param string $code
* @return true
*/
public static function setToSession($namespace, $code) {
self::getSessionObject($namespace)->captcha = $code;
return true;
}
/**
* 从session中取出已经生成的验证码
*
* @param string $namespace
* @return string|false
*/
public static function getFromSession($namespace) {
$code = self::getSessionObject($namespace)->captcha;
if (!empty( $code )) {
return $code;
} else {
return false;
}
}
/**
* Constructor
*
* @param array $options
* @return void
*/
public function __construct($options = null) {
if (isset ( $options )) {
foreach ( $options as $k => $v ) {
call_user_func ( array ($this, 'set' . ucfirst ( $k ) ), $v );
}
}
}
public function generate($namespace) {
$word = $this->_generateWord ();
self::setToSession ( $namespace, $word );
header ( "Content-type: image/png" );
$this->_generateImage ( $word );
return $word;
}
/**
* 生成验证码
*
* @param string $word
*/
protected function _generateImage($word) {
if (! extension_loaded("gd")) {
exit("Image CAPTCHA requires GD extension");
}
if (! function_exists("imagepng")) {
exit("Image CAPTCHA requires PNG support");
}
if (! function_exists("imageftbbox")) {
exit("Image CAPTCHA requires FT fonts support");
}
$font = $this->getFont();
if (empty($font)) {
exit("Image CAPTCHA requires font");
}
$w = $this->getWidth ();
$h = $this->getHeight ();
$fsize = $this->getFontSize ();
$img = imagecreatetruecolor ( $w, $h );
$text_color = imagecolorallocate ( $img, 0, 0, 0 );
$bg_color = imagecolorallocate ( $img, 255, 255, 255 );
imagefilledrectangle ( $img, 0, 0, $w - 1, $h - 1, $bg_color );
$textbox = imageftbbox ( $fsize, 0, $font, $word );
$x = ($w - ($textbox [2] - $textbox [0])) / 2;
$y = ($h - ($textbox [7] - $textbox [1])) / 2;
imagefttext ( $img, $fsize, 0, $x, $y, $text_color, $font, $word );
// generate noise
for($i = 0; $i < $this->_dotNoiseLevel; $i ++) {
imagefilledellipse ( $img, mt_rand ( 0, $w ), mt_rand ( 0, $h ), 2, 2, $text_color );
}
for($i = 0; $i < $this->_lineNoiseLevel; $i ++) {
imageline ( $img, mt_rand ( 0, $w ), mt_rand ( 0, $h ), mt_rand ( 0, $w ), mt_rand ( 0, $h ), $text_color );
}
// transformed image
$img2 = imagecreatetruecolor ( $w, $h );
$bg_color = imagecolorallocate ( $img2, 255, 255, 255 );
imagefilledrectangle ( $img2, 0, 0, $w - 1, $h - 1, $bg_color );
// apply wave transforms
$freq1 = $this->_randomFreq ();
$freq2 = $this->_randomFreq ();
$freq3 = $this->_randomFreq ();
$freq4 = $this->_randomFreq ();
$ph1 = $this->_randomPhase ();
$ph2 = $this->_randomPhase ();
$ph3 = $this->_randomPhase ();
$ph4 = $this->_randomPhase ();
$szx = $this->_randomSize ();
$szy = $this->_randomSize ();
for($x = 0; $x < $w; $x ++) {
for($y = 0; $y < $h; $y ++) {
$sx = $x + (sin ( $x * $freq1 + $ph1 ) + sin ( $y * $freq3 + $ph3 )) * $szx;
$sy = $y + (sin ( $x * $freq2 + $ph2 ) + sin ( $y * $freq4 + $ph4 )) * $szy;
if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {
continue;
} else {
$color = (imagecolorat ( $img, $sx, $sy ) >> 16) & 0xFF;
$color_x = (imagecolorat ( $img, $sx + 1, $sy ) >> 16) & 0xFF;
$color_y = (imagecolorat ( $img, $sx, $sy + 1 ) >> 16) & 0xFF;
$color_xy = (imagecolorat ( $img, $sx + 1, $sy + 1 ) >> 16) & 0xFF;
}
if ($color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) {
// ignore background
continue;
} elseif ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) {
// transfer inside of the image as-is
$newcolor = 0;
} else {
// do antialiasing for border items
$frac_x = $sx - floor ( $sx );
$frac_y = $sy - floor ( $sy );
$frac_x1 = 1 - $frac_x;
$frac_y1 = 1 - $frac_y;
$newcolor = $color * $frac_x1 * $frac_y1 + $color_x * $frac_x * $frac_y1 + $color_y * $frac_x1 * $frac_y + $color_xy * $frac_x * $frac_y;
}
imagesetpixel ( $img2, $x, $y, imagecolorallocate ( $img2, $newcolor, $newcolor, $newcolor ) );
}
}
// generate noise
for($i = 0; $i < $this->_dotNoiseLevel; $i ++) {
imagefilledellipse ( $img2, mt_rand ( 0, $w ), mt_rand ( 0, $h ), 2, 2, $text_color );
}
for($i = 0; $i < $this->_lineNoiseLevel; $i ++) {
imageline ( $img2, mt_rand ( 0, $w ), mt_rand ( 0, $h ), mt_rand ( 0, $w ), mt_rand ( 0, $h ), $text_color );
}
imagepng ( $img2 );
imagedestroy ( $img );
imagedestroy ( $img2 );
}
/**
* 获取session对象
*
* @param string $namespace
* @return object
*/
public static function getSessionObject($namespace)
{
if(strtoupper(substr(PHP_OS,0,3)) === 'WIN')
{
return Session::fileStart($namespace);
}
else
{
return Session::start($namespace, null, 'yohobuy.com');
}
}
}