* @version 2.0 * GNU General Public License (Version 2, October 2nd, 2005) * * based on Hadar Porat's Captcha Numbers class v.1.5 * * This program is free software; you can redistribute * it and/or modify it under the terms of the GNU * General Public License as published by the Free * Software Foundation; either version 2 of the License, * or (at your option) any later version. * * This program is distributed in the hope that it will * be useful, but WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License * for more details. */ /** * CaptchaNumbersV2 Class * @access public * @author Albert Demeter * @version 2.0 */ class CaptchaNumbersV2 { var $length = 6; var $font = 'arial'; var $size = 15; var $type = 'png'; var $height = 40; var $width = 80; var $grid = 7; var $string = ''; var $captchaType = 'mixed'; /** * @return void * @param int $length string length * @param int $size font size * @param String $type image type * @param String $captchaType text contain digits, chars or mixed * @desc generate the main image */ function CaptchaNumbersV2($length = '', $size = '', $type = '', $captchaType = '') { if ($length!='') $this -> length = $length; if ($size!='') $this -> size = $size; if ($type!='') $this -> type = $type; if ($captchaType!='') $this -> captchaType = $captchaType; $this -> width = $this -> length * $this -> size + $this -> grid + $this -> grid; $this -> height = $this -> size + (2 * $this -> grid); $this -> generateString(); } /** * @return void * @desc display captcha image */ function display() { $this -> sendHeader(); $image = $this -> generate(); switch ($this-> type) { case 'jpeg': imagejpeg($image); break; case 'png': imagepng($image); break; case 'gif': imagegif($image); break; default: imagepng($image); break; } } /** * @return Image * @desc generate the image */ function generate() { $image = ImageCreate($this -> width, $this -> height) or die("Cannot Initialize new GD image stream"); // colors $background_color = ImageColorAllocate($image, 240, 240, 240); $net_color_1 = ImageColorAllocate($image, 10, 200, 10); $net_color_2 = ImageColorAllocate($image, 200, 10, 10); $stringcolor_1 = ImageColorAllocate($image, 150, 150, 150); $stringcolor_2 = ImageColorAllocate($image, 120, 120, 250); for ($i = $this -> grid; $i < $this -> height; $i+=$this -> grid) if ($i%2) ImageLine($image, 0, $i, $this -> width, $i, $net_color_1); else ImageLine($image, 0, $i, $this -> width, $i, $net_color_2); // make the text $str = $this -> getString(); for($i = 0, $n = strlen($str); $i < $n; $i++) { if ($i%2) ImageTTFText($image, $this -> size, 0, $this -> grid + $this -> size * $i, $this -> size + $this -> grid - rand(0, 5), $stringcolor_1, $this -> font, $str{$i} ); else ImageTTFText($image, $this -> size, 0, $this -> grid + $this -> size * $i, $this -> size + $this -> grid + rand(0, 5), $stringcolor_2, $this -> font, $str{$i} ); } // grid for ($i = $this -> grid; $i < $this -> width; $i+=$this -> grid) if ($i%2) ImageLine($image, $i, 0, $i, $this -> height, $net_color_1); else ImageLine($image, $i, 0, $i, $this -> height, $net_color_2); return $image; } /** * @return String * @desc generate the string */ function generateString() { $string = $this->create_random_value($this -> length, $this -> captchaType); $this -> string = $string; return true; } /** * @return void * @desc send image header */ function sendHeader() { header('Content-type: image/' . $this -> type); } /** * @return String * @desc return the string */ function getString() { return $this -> string; } function create_random_value($length, $type = 'mixed') { if ( ($type != 'mixed') && ($type != 'chars') && ($type != 'digits')) return false; $rand_value = ''; while (strlen($rand_value) < $length) { if ($type == 'digits') { $char = rand(0,9); } else { $char = chr(rand(0,255)); } if ($type == 'mixed') { if (eregi('^[a-z0-9]$', $char)) $rand_value .= $char; } elseif ($type == 'chars') { if (eregi('^[a-z]$', $char)) $rand_value .= $char; } elseif ($type == 'digits') { if (ereg('^[0-9]$', $char)) $rand_value .= $char; } } return $rand_value; } }