captcha_class.php
9.69 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
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* 验证码图片生成
*
* @author Tiny
* @class Captcha
*/
class Captcha
{
/** Width of the image */
public $width = 200;
/** Height of the image */
public $height = 70;
/**
* Path for resource files (fonts, words, etc.)
* "resources" by default. For security reasons, is better move this
* directory to another location outise the web server
*/
/** Min word length (for non-dictionary random text generation) */
public $minWordLength = 4;
/**
* Max word length (for non-dictionary random text generation)
* Used for dictionary words indicating the word-length
* for font-size modification purposes
*/
public $maxWordLength = 5;
/** Sessionname to store the original text */
public $session_var = 'captcha';
/** Background color in RGB-array */
public $backgroundColor = array(255, 255, 255);
/** Foreground colors in RGB-array */
public $colors = array(
array(27,78,181), // blue
array(22,163,35), // green
array(214,36,7), // red
);
public $color = null;
/** Shadow color in RGB-array or null */
public $shadowColor = null; //array(0, 0, 0);
public $fontSize = 25;
/**
* Font configuration
* - font: TTF file
* - spacing: relative pixel space between character
* - minSize: min font size
* - maxSize: max font size
*/
public $fonts = array(
'Time' => array('spacing' => -1, 'minSize' => 28, 'maxSize' => 42, 'font' => 'font.ttf'),
);
/** Wave configuracion in X and Y axes */
public $Yperiod = 7;
public $Yamplitude = 9;
public $Xperiod = 11;
public $Xamplitude = 5;
/** letter rotation clockwise */
public $maxRotation = 8;
/**
* Internal image size factor (for better image quality)
* 1: low, 2: medium, 3: high
*/
public $scale = 3;
/**
* Blur effect for better image quality (but slower image processing).
* Better image results with scale=3
*/
public $blur = false;
/** Debug? */
public $debug = false;
/** Image format: jpeg or png */
public $imageFormat = 'jpeg';
/** GD image */
public $im;
public function __construct($width=220,$height=70,$num=5,$bgcolor=array(255,255,255),$color=null)
{
$this->width =$width;
$this->height = $height;
$this->setBackColor($bgcolor);
$this->setColor($color);
$this->maxWordLength = $this->minWordLength = $num;
}
public function setBackColor($color)
{
if(is_array($color)) $this->backgroundColor = $color;
else if(preg_match('/#[0-9A-Fa-f]{6}/',$color))
{
$this->backgroundColor =array(hexdec(substr($color,1,2)),hexdec(substr($color,3,2)),hexdec(substr($color,5,2)));
}
}
public function setColor($color)
{
if(is_array($color)) $this->color = $color;
else if(preg_match('/#[0-9A-Fa-f]{6}/',$color))
{
$this->color =array(hexdec(substr($color,1,2)),hexdec(substr($color,3,2)),hexdec(substr($color,5,2)));
}
else $this->color = $this->colors[mt_rand(0, sizeof($this->colors)-1)];
}
public function createImage(&$text='') {
$ini = microtime(true);
/** Initialization */
$this->ImageAllocate();
/** Text insertion */
$text = $this->GetCaptchaText();
$fontcfg = $this->fonts[array_rand($this->fonts)];
$this->WriteText($text, $fontcfg);
/** Transformations */
$this->WaveImage();
if ($this->blur && function_exists('imagefilter'))
{
imagefilter($this->im, IMG_FILTER_GAUSSIAN_BLUR);
}
$this->ReduceImage();
if ($this->debug)
{
imagestring($this->im, 1, 1, $this->height-8,
"$text {$fontcfg['font']} ".round((microtime(true)-$ini)*1000)."ms",
$this->GdFgColor
);
}
/** Output */
$this->WriteImage();
$this->Cleanup();
}
/**
* Creates the image resources
*/
protected function ImageAllocate()
{
// Cleanup
if (!empty($this->im))
{
imagedestroy($this->im);
}
$this->im = imagecreatetruecolor($this->width*$this->scale, $this->height*$this->scale);
// Background color
$this->GdBgColor = imagecolorallocate($this->im,
$this->backgroundColor[0],
$this->backgroundColor[1],
$this->backgroundColor[2]
);
imagefilledrectangle($this->im, 0, 0, $this->width*$this->scale, $this->height*$this->scale, $this->GdBgColor);
// Foreground color
$color = $this->color;//s[mt_rand(0, sizeof($this->colors)-1)];
$this->GdFgColor = imagecolorallocate($this->im, $color[0], $color[1], $color[2]);
// Shadow color
if (!empty($this->shadowColor) && is_array($this->shadowColor) && sizeof($this->shadowColor) >= 3)
{
$this->GdShadowColor = imagecolorallocate($this->im,
$this->shadowColor[0],
$this->shadowColor[1],
$this->shadowColor[2]
);
}
}
/**
* Text generation
*
* @return string Text
*/
protected function GetCaptchaText()
{
$text = $this->GetRandomCaptchaText();
return $text;
}
/**
* Random text generation
* @return string Text
*/
protected function GetRandomCaptchaText($length = null)
{
if (empty($length))
{
$length = rand($this->minWordLength, $this->maxWordLength);
}
$words = "bcdfghjklmnpqrstuvwxyz";
$vocals = "aeiou";
$text = "";
$vocal = mt_rand(0, 1);
for ($i=0; $i<$length; $i++)
{
if ($vocal)
{
$text .= substr($vocals, mt_rand(0, 4), 1);
}
else
{
$text .= substr($words, mt_rand(0, 21), 1);
}
$vocal = !$vocal;
}
return $text;
}
/**
* Text insertion
*/
protected function WriteText($text, $fontcfg = array())
{
if (empty($fontcfg))
{
// Select the font configuration
$fontcfg = $this->fonts[array_rand($this->fonts)];
}
// Full path of font file
$fontfile = dirname(__FILE__).'/'.$fontcfg['font'];
/** Increase font-size for shortest words: 9% for each glyp missing */
$lettersMissing = $this->maxWordLength-strlen($text);
$fontSizefactor = 1+($lettersMissing*0.09);
//$fontspace = $this->width/strlen($text)-2;
//$minSize = $fontspace;
//$maxSize= $fontspace;
// Text generation (char by char)
$x = 20*$this->scale;
$y = round(($this->height*0.78)*$this->scale);
$length = strlen($text);
for ($i=0; $i<$length; $i++)
{
$degree = rand($this->maxRotation*-1, $this->maxRotation);
$fontsize = rand($this->fontSize+1, $this->fontSize-1)*$this->scale*$fontSizefactor;
//$fontsize = $maxSize*$this->scale*$fontSizefactor;
$letter = substr($text, $i, 1);
if ($this->shadowColor)
{
$coords = imagettftext($this->im, $fontsize, $degree,
$x+$this->scale, $y+$this->scale,
$this->GdShadowColor, $fontfile, $letter);
}
$coords = imagettftext($this->im, $fontsize, $degree,
$x, $y,
$this->GdFgColor, $fontfile, $letter);
$x += ($coords[2]-$x) + ($fontcfg['spacing']*$this->scale);
}
}
/**
* Wave filter
*/
protected function WaveImage()
{
// X-axis wave generation
$xp = $this->scale*$this->Xperiod*rand(1,3);
$k = rand(0, 100);
for ($i = 0; $i < ($this->width*$this->scale); $i++)
{
imagecopy($this->im, $this->im,
$i-1, sin($k+$i/$xp) * ($this->scale*$this->Xamplitude),
$i, 0, 1, $this->height*$this->scale);
}
// Y-axis wave generation
$k = rand(0, 100);
$yp = $this->scale*$this->Yperiod*rand(1,2);
for ($i = 0; $i < ($this->height*$this->scale); $i++)
{
imagecopy($this->im, $this->im,
sin($k+$i/$yp) * ($this->scale*$this->Yamplitude), $i-1,
0, $i, $this->width*$this->scale, 1);
}
}
/**
* Reduce the image to the final size
*/
protected function ReduceImage()
{
$imResampled = imagecreatetruecolor($this->width, $this->height);
imagecopyresampled($imResampled, $this->im,
0, 0, 0, 0,
$this->width, $this->height,
$this->width*$this->scale, $this->height*$this->scale
);
imagedestroy($this->im);
$this->im = $imResampled;
}
/**
* File generation
*/
protected function WriteImage()
{
if ($this->imageFormat == 'png' && function_exists('imagepng'))
{
header("Content-type: image/png");
imagepng($this->im);
}
else
{
header("Content-type: image/jpeg");
imagejpeg($this->im, null, 90);
}
}
/**
* Cleanup
*/
protected function Cleanup()
{
imagedestroy($this->im);
}
}
?>