00001 <?php
00003
00004
00005
00006
00007
00008
00010
00053 class FLEA_Helper_ImgCode
00054 {
00060 var $_code;
00061
00067 var $_expired;
00068
00074 var $imagetype = 'jpeg';
00075
00084 var $keepCode = false;
00085
00089 function FLEA_Helper_ImgCode()
00090 {
00091 @session_start();
00092
00093 $this->_code = isset($_SESSION['IMGCODE']) ?
00094 $_SESSION['IMGCODE'] : '';
00095 $this->_expired = isset($_SESSION['IMGCODE_EXPIRED']) ?
00096 $_SESSION['IMGCODE_EXPIRED'] : 0;
00097 }
00098
00106 function check($code)
00107 {
00108 $time = time();
00109 if ($time >= $this->_expired || strtoupper($code) != strtoupper($this->_code)) {
00110 return false;
00111 }
00112 return true;
00113 }
00114
00122 function checkCaseSensitive($code)
00123 {
00124 $time = time();
00125 if ($time >= $this->_expired || $code != $this->_code) {
00126 return false;
00127 }
00128 return true;
00129 }
00130
00134 function clear()
00135 {
00136 unset($_SESSION['IMGCODE']);
00137 unset($_SESSION['IMGCODE_EXPIRED']);
00138 }
00139
00160 function image($type = 0, $length = 4, $lefttime = 900, $options = null)
00161 {
00162 if ($this->keepCode && $this->_code != '') {
00163 $code = $this->_code;
00164 } else {
00165
00166 switch ($type) {
00167 case 0:
00168 $seed = '0123456789';
00169 break;
00170 case 1:
00171 $seed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
00172 break;
00173 default:
00174 $seed = '346789ABCDEFGHJKLMNPQRTUVWXYabcdefghjklmnpqrtuvwxy';
00175 }
00176 if ($length <= 0) { $length = 4; }
00177 $code = '';
00178 list($usec, $sec) = explode(" ", microtime());
00179 srand($sec + $usec * 100000);
00180 $len = strlen($seed) - 1;
00181 for ($i = 0; $i < $length; $i++) {
00182 $code .= substr($seed, rand(0, $len), 1);
00183 }
00184 $_SESSION['IMGCODE'] = $code;
00185 }
00186 $_SESSION['IMGCODE_EXPIRED'] = time() + $lefttime;
00187
00188
00189 $paddingLeft = isset($options['paddingLeft']) ?
00190 (int)$options['paddingLeft'] : 3;
00191 $paddingRight = isset($options['paddingRight']) ?
00192 (int)$options['paddingRight'] : 3;
00193 $paddingTop = isset($options['paddingTop']) ?
00194 (int)$options['paddingTop'] : 2;
00195 $paddingBottom = isset($options['paddingBottom']) ?
00196 (int)$options['paddingBottom'] : 2;
00197 $color = isset($options['color']) ? $options['color'] : '0xffffff';
00198 $bgcolor = isset($options['bgcolor']) ? $options['bgcolor'] : '0x666666';
00199 $border = isset($options['border']) ? (int)$options['border'] : 1;
00200 $bdColor = isset($options['borderColor']) ? $options['borderColor'] : '0x000000';
00201
00202
00203 if (!isset($options['font'])) {
00204 $font = 5;
00205 } else if (is_int($options['font'])) {
00206 $font = (int)$options['font'];
00207 if ($font < 0 || $font > 5) { $font = 5; }
00208 } else {
00209 $font = imageloadfont($options['font']);
00210 }
00211
00212
00213 $fontWidth = imagefontwidth($font);
00214 $fontHeight = imagefontheight($font);
00215
00216
00217 $width = $fontWidth * strlen($code) + $paddingLeft + $paddingRight +
00218 $border * 2 + 1;
00219 $height = $fontHeight + $paddingTop + $paddingBottom + $border * 2 + 1;
00220
00221
00222 $img = imagecreate($width, $height);
00223
00224
00225 if ($border) {
00226 list($r, $g, $b) = $this->_hex2rgb($bdColor);
00227 $borderColor = imagecolorallocate($img, $r, $g, $b);
00228 imagefilledrectangle($img, 0, 0, $width, $height, $borderColor);
00229 }
00230
00231
00232 list($r, $g, $b) = $this->_hex2rgb($bgcolor);
00233 $backgroundColor = imagecolorallocate($img, $r, $g, $b);
00234 imagefilledrectangle($img, $border, $border,
00235 $width - $border - 1, $height - $border - 1, $backgroundColor);
00236
00237
00238 list($r, $g, $b) = $this->_hex2rgb($color);
00239 $textColor = imagecolorallocate($img, $r, $g, $b);
00240 imagestring($img, $font, $paddingLeft + $border, $paddingTop + $border,
00241 $code, $textColor);
00242
00243
00244 switch (strtolower($this->imagetype)) {
00245 case 'png':
00246 header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));
00247 imagepng($img);
00248 break;
00249 case 'gif':
00250 header("Content-type: " . image_type_to_mime_type(IMAGETYPE_GIF));
00251 imagegif($img);
00252 break;
00253 case 'jpg':
00254 default:
00255 header("Content-type: " . image_type_to_mime_type(IMAGETYPE_JPEG));
00256 imagejpeg($img);
00257 }
00258
00259 imagedestroy($img);
00260 unset($img);
00261 }
00262
00270 function _hex2rgb($color, $defualt = 'ffffff')
00271 {
00272 $color = strtolower($color);
00273 if (substr($color, 0, 2) == '0x') {
00274 $color = substr($color, 2);
00275 } elseif (substr($color, 0, 1) == '#') {
00276 $color = substr($color, 1);
00277 }
00278 $l = strlen($color);
00279 if ($l == 3) {
00280 $r = hexdec(substr($color, 0, 1));
00281 $g = hexdec(substr($color, 1, 1));
00282 $b = hexdec(substr($color, 2, 1));
00283 return array($r, $g, $b);
00284 } elseif ($l != 6) {
00285 $color = $defualt;
00286 }
00287
00288 $r = hexdec(substr($color, 0, 2));
00289 $g = hexdec(substr($color, 2, 2));
00290 $b = hexdec(substr($color, 4, 2));
00291 return array($r, $g, $b);
00292 }
00293 }