Changeset 2535
- Timestamp:
- 04/20/2008 04:04:33 AM (7 months ago)
- Location:
- trunk/system
- Files:
-
- 2 modified
-
config/captcha.php (modified) (1 diff)
-
libraries/Captcha.php (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/system/config/captcha.php
r2206 r2535 6 6 * Custom styles can be added by extending the Captcha class 7 7 */ 8 8 9 /** 9 * Width of captcha image, ignored if using a background 10 * Width and height of the Captcha image. 11 * These settings are ignored if using a background image. 10 12 */ 11 $config['width'] = 150; 12 /** 13 * Height of captcha image, ignored if using a background 14 */ 13 $config['width'] = 150; 15 14 $config['height'] = 40; 15 16 16 /** 17 17 * Captcha style to use. Default is 'basic' and is only for testing as it 18 * does not require any TrueType fonts installed.18 * does not require any truetype fonts installed. 19 19 * 'standard' is the recommended style. A font must be supplied. A background 20 20 * image is optional. 21 21 * 'math' is a 'solve the question' style. 22 22 * A font must be supplied. A background image is optional. 23 * Custom styles can be added easily by extending the library 23 * Custom styles can be added easily by extending the library. 24 24 */ 25 25 $config['style'] = 'basic'; 26 26 27 /** 27 * Number of characters to use for the captcha, ignored if using style 'math'28 * Four or five seems optimal.28 * Number of characters to use for the Captcha (4 or 5 recommended). 29 * This setting is ignored if using style 'math'. 29 30 */ 30 31 $config['num_chars'] = 4; 32 31 33 /** 32 * Path to font files. Default is none, ''. Example 'application/fonts/'34 * Path to font files. Example: 'application/fonts/'. 33 35 * If using 'standard' style, you must supply a valid truetype font file. 34 36 */ 35 37 $config['font_path'] = ''; 38 36 39 /** 37 * Name of the font, Case sensitive, with the file extension, default is ''40 * Name of the font, with the file extension. Case sensitive. 38 41 */ 39 42 $config['font_name'] = ''; 43 40 44 /** 41 * Background image. Default ''. Example 'application/images/pattern.jpg'45 * Background image. Example: 'application/images/pattern.jpg'. 42 46 * The dimensions of this image will be used. 43 47 */ -
trunk/system/libraries/Captcha.php
r2534 r2535 11 11 */ 12 12 class Captcha_Core { 13 //config 14 protected $font_path = ''; 15 protected $font_name = ''; 16 protected $width = 150; 17 protected $height = 50; 13 14 // Config 15 protected $font_path = ''; 16 protected $font_name = ''; 17 protected $width = 150; 18 protected $height = 50; 18 19 protected $background_image = ''; 19 protected $style = 'basic';20 protected $num_chars = 4;21 22 // class internal variables20 protected $style = 'basic'; 21 protected $num_chars = 4; 22 23 // Class internal variables 23 24 protected $image; 24 25 protected $color_black; … … 28 29 protected $numerals = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'); 29 30 30 31 31 /** 32 32 * Creates a new Captcha instance. 33 * 33 34 * @throws Kohana_Exception 34 35 * @param array configuration … … 50 51 $this->initialize($config); 51 52 52 // If using a background image, check i t exists.53 // If using a background image, check if it exists. 53 54 if ($this->background_image) 54 55 { … … 56 57 throw new Kohana_Exception('captcha.file_not_found', $this->background_image); 57 58 } 58 // If using a font, check it exists. 59 60 // If using a font, check if it exists. 59 61 if ($this->font_name) 60 62 { … … 85 87 86 88 /** 87 * Sets the captcha code to use88 * 89 * @param string captcha code generated in captcha controller89 * Sets the Captcha code to use. 90 * 91 * @param string captcha code generated in captcha controller 90 92 * @return void 91 93 */ 92 94 public function set_code($str) 93 95 { 94 $this->captcha_code = $str; 95 } 96 97 /** 98 * Generates the Captcha Image 99 * 100 * @param none 96 $this->captcha_code = (string) $str; 97 } 98 99 /** 100 * Generates the Captcha image. 101 * 101 102 * @return void 102 103 */ 103 104 public function render() 104 105 { 105 // if extending the class with a custom captcha function, name it 'xyz_captcha' 106 // style 'xyz' must be added to config 107 // Call the method that implements the captcha 106 // If extending the class with a custom Captcha function, name it 'xyz_captcha'. 107 // Style 'xyz' must be added to config. Now call the method that implements the Captcha. 108 108 $this->{$this->style.'_captcha'}(); 109 109 110 // Tell browser what to expect111 // todomake this automatic112 // header("Content-Type: image/jpeg");113 header( "Content-Type: image/png");114 115 // Output the captcha image116 // imagejpeg($this->image);110 // Tell browser what to expect 111 // TODO: make this automatic 112 // header('Content-Type: image/jpeg'); 113 header('Content-Type: image/png'); 114 115 // Output the captcha image 116 // imagejpeg($this->image); 117 117 imagepng($this->image); 118 118 119 // Free up resources119 // Free up resources 120 120 imagedestroy($this->image); 121 121 } 122 /** 123 * Validates the captcha code against session captcha code 124 * 125 * @param string captcha code text 122 123 /** 124 * Validates the Captcha code against session Captcha code 125 * 126 * @param string captcha code text 126 127 * @return boolean 127 128 */ 128 129 public static function valid_captcha($str) 129 130 { 130 return ( $str == $_SESSION['captcha_code']) ? TRUE : FALSE;131 } 132 133 /** 134 * Creates image resource and allocates some basic colors 131 return ((string) $str === Session::instance()->get('captcha_code')); 132 } 133 134 /** 135 * Creates image resource and allocates some basic colors. 135 136 * If a background image is supplied, the image dimensions are used. 136 * @param none137 * 137 138 * @return void 138 139 */ … … 141 142 if ($this->background_image) 142 143 { 143 // todocreate from any valid image144 // TODO: create from any valid image 144 145 $this->image = imagecreatefromjpeg($this->background_image); 145 146 $this->color_white = imagecolorallocate($this->image, 255, 255, 255); 146 // get the background image dimensions 147 $this->width = imagesx($this->image); 147 148 // Get the background image dimensions 149 $this->width = imagesx($this->image); 148 150 $this->height = imagesy($this->image); 149 151 } … … 153 155 $this->color_white = imagecolorallocate($this->image, 255, 255, 255); 154 156 155 // Fill the image with a colored gradient 156 // Use random colors, but try not to obscure the text 157 $left_color = array(mt_rand(100,255), 0, 255); 157 // Fill the image with a colored gradient (use random colors, but try not to obscure text) 158 $left_color = array(mt_rand(100,255), 0, 255); 158 159 $right_color = array(100, 100, mt_rand(100,0)); 159 160 $this->img_color_gradient($this->image, 0, 0, $this->height, $this->width, $left_color, $right_color); … … 162 163 163 164 /** 164 * Allocates a background color to image 165 * 166 * @param array GD image color identifier165 * Allocates a background color to image. 166 * 167 * @param array GD image color identifier 167 168 * @return void 168 169 */ … … 173 174 174 175 /** 175 * Draws a very basic captcha image:176 * Draws a very basic Captcha image. 176 177 * Requires only GD. Useful for testing or if you can't use truetype fonts. 177 178 * 178 * @param none179 179 * @return void 180 180 */ 181 181 protected function basic_captcha() 182 182 { 183 $this->image = imagecreate($this->width, $this->height);183 $this->image = imagecreate($this->width, $this->height); 184 184 $this->color_white = imagecolorallocate($this->image, 255, 255, 255); 185 185 $this->color_black = imagecolorallocate($this->image, 0, 0, 0); … … 189 189 190 190 /** 191 * Draws the standard captcha image:192 * Requires GD with freetype and available true type compatible font files.191 * Draws the standard Captcha image: 192 * Requires GD with freetype and available truetype compatible font files. 193 193 * 194 194 * @param none … … 202 202 $this->calculate_spacing(); 203 203 204 // draw each captcha character with varying attributes205 for ($i = 0 ; $i < strlen($this->captcha_code); $i++)206 { 207 // allocate random color, size, rotation attributes to text.204 // Draw each Captcha character with varying attributes 205 for ($i = 0, $strlen = strlen($this->captcha_code); $i < $strlen; $i++) 206 { 207 // Allocate random color, size and rotation attributes to text 208 208 $text_color = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); 209 209 $angle = mt_rand(-40, 40); 210 // make first char angle inward 211 if ($i == 0) 210 211 // Make first char angle inward 212 if ($i === 0) 212 213 { 213 214 $angle = -abs($angle); 214 215 } 215 // make last char angle inward216 if ($i == ($this->num_chars -1))216 // Make last char angle inward 217 if ($i === ($this->num_chars - 1)) 217 218 { 218 219 $angle = abs($angle); 219 220 } 220 // Scale the charcter size on image height 221 $font_size = mt_rand(($this->height -20), ($this->height -12));222 221 222 // Scale the character size on image height 223 $font_size = mt_rand($this->height - 20, $this->height - 12); 223 224 $char_details = imageftbbox($font_size, $angle, $font, $this->captcha_code[$i], array()); 224 225 225 // calculate character starting coordinates 226 $iX = $this->spacing / 4 + $i * $this->spacing; 227 $char_height = $char_details[2] - $char_details[5]; 228 $iY = $this->height / 2 + $char_height / 4; 229 230 // write text character to image 231 imagefttext($this->image, $font_size, $angle, $iX, $iY, $text_color, $font, $this->captcha_code[$i], array()); 232 } 233 } 234 235 /** 236 * Draws the math riddle captcha image: 237 * Requires GD with freetype and available true type compatible font files. 238 * 239 * @param none 226 // Calculate character starting coordinates 227 $iX = $this->spacing / 4 + $i * $this->spacing; 228 $char_height = $char_details[2] - $char_details[5]; 229 $iY = $this->height / 2 + $char_height / 4; 230 231 // Write text character to image 232 imagefttext($this->image, $font_size, $angle, $iX, $iY, $text_color, $font, $this->captcha_code[$i], array()); 233 } 234 } 235 236 /** 237 * Draws the math riddle Captcha image. 238 * Requires GD with freetype and available truetype compatible font files. 239 * 240 240 * @return void 241 241 */ 242 242 protected function math_captcha() 243 243 { 244 $answer = $_SESSION['captcha_code'];245 //get the last digit 246 $last_digit = substr($answer, -1);247 // convert to numeral248 $numeral = $this->numerals[$last_digit]; 249 // subtract last digit from answer244 $answer = Session::instance()->get('captcha_code'); 245 246 // Convert to numeral 247 $numeral = $this->numerals[substr($answer, -1)]; 248 249 // Subtract last digit from answer 250 250 $number = substr($answer, 0, 2).'0'; 251 251 252 // $number plus $numeral equals $answer 252 253 $text = $number.' + '.$numeral.' = '; 253 254 $this->img_create(); 254 255 $font = $this->font_path.$this->font_name; 255 // scale the font size to image height 256 257 // Scale the font size to image height 256 258 $font_size = $this->height / 3; 257 259 $text_details = imageftbbox($font_size, 0, $font, $text, array()); 258 260 $iX = 5; 259 261 $iY = ($this->height / 2) + 5; 262 260 263 imagefttext($this->image, $font_size, 0, $iX, $iY, $this->color_white, $font, $text, array()); 261 264 } 262 /** 263 * Calculates letter spacing for true type font characters264 * 265 * @param none265 266 /** 267 * Calculates letter spacing for truetype font characters. 268 * 266 269 * @return integer 267 270 */ 268 271 protected function calculate_spacing() 269 272 { 270 $this->spacing = (int)($this->width / $this->num_chars);273 return $this->spacing = (int) $this->width / $this->num_chars; 271 274 } 272 275 … … 274 277 * Fills the image with a colored gradient. 275 278 * 276 * @param resource gd image resource identifier277 * @param integer start X position278 * @param integer start Y position279 * @param integer height of fill in pixels280 * @param integer width of fill in pixels281 * @param resource gd image color identifier for left of image282 * @param resource gd image color identifier for right of image279 * @param resource gd image resource identifier 280 * @param integer start X position 281 * @param integer start Y position 282 * @param integer height of fill in pixels 283 * @param integer width of fill in pixels 284 * @param resource gd image color identifier for left of image 285 * @param resource gd image color identifier for right of image 283 286 * @return void 284 287 */ … … 288 291 $color1 = ($left_color[1] - $right_color[1]) / $width; 289 292 $color2 = ($left_color[2] - $right_color[2]) / $width; 290 for ($i=0; $i <= $width; $i++) 291 { 292 $red = $left_color[0] -floor($i*$color0); 293 294 for ($i = 0; $i <= $width; $i++) 295 { 296 $red = $left_color[0] -floor($i*$color0); 293 297 $green = $left_color[1] -floor($i*$color1); 294 $blue = $left_color[2] -floor($i*$color2); 295 $col = imagecolorallocate($this->image, $red, $green, $blue); 298 $blue = $left_color[2] -floor($i*$color2); 299 $col = imagecolorallocate($this->image, $red, $green, $blue); 300 296 301 imageline($this->image, $x1 + $i, $y1, $x1 + $i, $y1 + $height, $col); 297 302 }
