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