Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Captcha
  • Converter
  • DegradeCouleur
  • Font
  • Gauge
  • Image
  • Traitement

Exceptions

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