1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\Image;
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33: class Captcha extends Image
34: {
35:
36: private $randString;
37: private $randStringLength;
38: private $borderWidth;
39: private $border;
40: private $shadow;
41: private $tricky;
42: private $char;
43: private $maxFontSize;
44: private $minFontSize;
45: public $arrayOfColor;
46:
47: 48: 49: 50: 51: 52: 53:
54: public function __construct($width, $height)
55: {
56: parent::__construct(null, $width, $height);
57: $this->width = $width;
58: $this->height = $height;
59: if ($width <= 0 || $height <= 0)
60: throw new \RangeException('Une image doit avoir des dimension valides');
61:
62: $this->randStringLength = 10;
63: $this->borderWidth = 3;
64: $this->maxFontSize = 30;
65: $this->minFontSize = 20;
66: $this->tricky = false;
67: $this->char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ12346789';
68: $this->randString = array();
69:
70: $this->setbgColor(255, 255, 255);
71: $this->setFont('Acidic');
72: $this->initTextColor();
73: }
74:
75: 76: 77: 78: 79: 80:
81: public function setLength($length)
82: {
83: $this->randStringLength = $length;
84: }
85:
86: 87: 88: 89: 90: 91:
92: public function setBorderSize($size)
93: {
94: $this->borderWidth = $size;
95: }
96:
97: 98: 99: 100: 101: 102:
103: public function getRandomString()
104: {
105: return implode('', $this->randString);
106: }
107:
108: 109: 110: 111: 112:
113: private function randomize()
114: {
115: for ($i = 0; $i < $this->randStringLength; $i++) {
116: $this->randString[$i] = $this->char[mt_rand(0, 33)];
117: }
118: }
119:
120: 121: 122: 123: 124:
125: private function initTextColor()
126: {
127: $this->arrayOfColor[0]['r'] = 215;
128: $this->arrayOfColor[0]['v'] = 0;
129: $this->arrayOfColor[0]['b'] = 0;
130:
131: $this->arrayOfColor[1]['r'] = 0;
132: $this->arrayOfColor[1]['v'] = 128;
133: $this->arrayOfColor[1]['b'] = 255;
134:
135: $this->arrayOfColor[2]['r'] = 0;
136: $this->arrayOfColor[2]['v'] = 128;
137: $this->arrayOfColor[2]['b'] = 0;
138:
139: $this->arrayOfColor[3]['r'] = 255;
140: $this->arrayOfColor[3]['v'] = 0;
141: $this->arrayOfColor[3]['b'] = 128;
142:
143: $this->arrayOfColor[4]['r'] = 128;
144: $this->arrayOfColor[4]['v'] = 0;
145: $this->arrayOfColor[4]['b'] = 128;
146:
147: $this->arrayOfColor[5]['r'] = 0;
148: $this->arrayOfColor[5]['v'] = 255;
149: $this->arrayOfColor[5]['b'] = 128;
150: }
151:
152: 153: 154: 155: 156:
157: private function moreNoise()
158: {
159:
160: $max = ceil($this->height / 20);
161: if ($max < 1)
162: $max = 2;
163: $maxElement = mt_rand(1, $max);
164: for ($i = 0; $i < $maxElement; $i++) {
165: $cx = mt_rand(0, $this->width);
166: $cy = mt_rand(0, $this->height);
167:
168: $largeur = mt_rand(5, $this->width);
169: $hauteur = mt_rand(5, $this->height);
170:
171: imageellipse($this->source, $cx, $cy, $largeur, $hauteur, $this->couleur);
172: imageline($this->source, $cx, $cy, $largeur, $hauteur, $this->couleur);
173: }
174: }
175:
176: 177: 178: 179: 180: 181:
182: public function addBorder($color)
183: {
184: $this->setBorder($this->borderWidth, $color);
185: }
186:
187: 188: 189: 190: 191:
192: public function setTricky()
193: {
194: $this->tricky = true;
195: }
196:
197: 198: 199: 200: 201:
202: public function createCaptcha()
203: {
204: $this->randomize();
205: $totalCol = count($this->arrayOfColor);
206: $posX = $this->width / 4;
207: $posY = ($this->height / 2) + ($this->maxFontSize - $this->minFontSize) + 5;
208: for ($i = 0; $i < $this->randStringLength; $i++) {
209: $rand = mt_rand(0, $totalCol - 1);
210: $randFontsize = mt_rand($this->minFontSize, $this->maxFontSize);
211: $randAngle = mt_rand(-40, 40);
212: $this->setColor($this->arrayOfColor[$rand]['r'], $this->arrayOfColor[$rand]['v'], $this->arrayOfColor[$rand]['b']);
213: $this->setText($this->randString[$i], $randFontsize, $posX, $posY, $randAngle);
214: $posX += $randFontsize - 3;
215: if ($this->tricky)
216: $this->moreNoise();
217: }
218: }
219:
220: }
221:
222: ?>