Overview

Packages

  • Auth
  • Config
  • Controller
  • Date
  • Db
  • Feed
    • Abstract
    • Writers
  • File
    • Decorator
  • Form
    • Element
  • Image
  • Log
    • Writer
  • Net
    • Exception
  • None
  • PHP
  • PHPMailer
  • Session
  • Util
  • Validate
    • Validator
  • Zend
    • Registry

Classes

  • Image_Captcha
  • Image_Converter
  • Image_DegradeCouleur
  • Image_Font
  • Image_Gauge
  • Image_Image
  • Image_Traitement

Exceptions

  • Image_Exception
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Pry Framework
  4:  *
  5:  * LICENSE
  6:  *
  7:  * This source file is subject to the new BSD license that is bundled
  8:  * with this package in the file LICENSE.txt.
  9:  * 
 10:  * @version $Revision: 276 $
 11:  */
 12: 
 13: /**
 14:  * Génère une image de vérification (CAPTCHA).
 15:  * <code>
 16:  * $captcha = new Image_Captcha(300,80);
 17:  * $captcha->setLength(4);
 18:  * $captcha->setType(Image_Captcha::IMG_JPG);
 19:  * $captcha->setBorderSize(2);
 20:  * $captcha->addBorder('#000000');
 21:  * $captcha->createCaptcha();
 22:  * $captcha->display();
 23:  * $_SESSION['captcha'] = $captcha->getRandomString();
 24:  * </code> 
 25:  * @category Pry
 26:  * @package Image
 27:  * @version 1.2.0
 28:  * @author Olivier ROGER <oroger.fr>
 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:      * Cosntructeur
 46:      *
 47:      * @param int $width
 48:      * @param int $height
 49:      * @access public
 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:      * Défini la longeur de la chaine. 4 ou 5 semble un bon compromis
 74:      * 
 75:      * @access public
 76:      * @param int $length Longeur de la chaine
 77:      */
 78:     public function setLength($length)
 79:     {
 80:         $this->randStringLength = $length;
 81:     }
 82:     
 83:     /**
 84:      * Défini la taille de la bordure de l'image
 85:      *
 86:      * @access public
 87:      * @param int $size Taille en pixels
 88:      */
 89:     public function setBorderSize($size)
 90:     {
 91:         $this->borderWidth = $size;
 92:     }
 93:     
 94:     /**
 95:      * Renvoi la chaine générée pour enregistrment en session
 96:      *
 97:      * @access public
 98:      * @return string
 99:      */
100:     public function getRandomString()
101:     {
102:         return implode('',$this->randString);
103:     }
104:     
105:     /**
106:      * Génère la chaine aléatoire.
107:      * 
108:      * @access private
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:      * Défini des couleurs de base pour les lettres
120:      * 
121:      * @access private
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:      * Ajoute du "bruit" à l'image pour la rendre difficile à lire par les robots
152:      *
153:      * @access private
154:      */
155:     private function moreNoise()
156:     {
157:         // Ajout d'ellipse et de courbe
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:      * Ajoute une bordure à l'image
177:      * 
178:      * @access public
179:      * @param string $color Couleur en hexa (#000000 , 0xFFFFFF)
180:      */
181:     public function addBorder($color)
182:     {
183:         $this->setBorder($this->borderWidth,$color);
184:     }
185:     
186:     /**
187:      * Active le bruit sur l'image
188:      * 
189:      * @access public
190:      */
191:     public function setTricky()
192:     {
193:         $this->tricky=true; 
194:     }
195:     
196:     /**
197:      * Génère le captcha
198:      *
199:      * @access public
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:  ?>
Pry Framework API documentation generated by ApiGen 2.6.1