看到你,梁松远的博客

位置 » 主页 » 代码分享 » 简单的php实现的验证码生成类

简单的php实现的验证码生成类

 class lib_captcha
 {
 const NUMBER = 1;
 const CHAR = 2;
 const STRING = 3;
 const CHINESE = 4;//暂时不支持中文
 private $words;0
 private $length = 5;//验证码长度
 private $width = 130;//宽度
 private $height = 40;//高度
 private $img;//图形资源句柄
 private $font;//指定的字体
 private $fontsize = 22;//指定字体大小
 private $fontcolor;
 private $timeout = 600;//超时
 private $session;
 private $img_dir;
 private $img_name;
 
 public function __contruct(){
 
 $this->img_dir = ROOT_PATH . 'cache/captcha/';
 $this->img_name = md5(microtime(TRUE)).'.png';
 $this->font = $this->get_font();
 $this->words = $this->get_words(self::CHAR,$this->length);
 if (!is_dir($this->img_dir))
 {
 @mkdir($this->img_dir);
 }
 //删除过期的验证码
 $this->unlinkimg();
 }
 
 public function get_font()
 {
 if (!$captcha_fonts =FIRM::cache()->get('captcha_fonts'))
 {
 $captcha_fonts = fetch_file_lists(SYS_PATH . 'lib/fonts/');
 
 FIRM::cache()->set('captcha_fonts', $captcha_fonts, get_setting('cache_level_normal'));
 }
 return array_random($captcha_fonts);
 }
 
 public function get_words($type = self::STRING,$length = 4){
 $words = random($length, $type);
 return $words;
 }
 
 public function unlinkimg(){
 $files = @dir($this->img_dir);
 while (($file = $files->read()) !== false){
 if($file != '.' &&$file != '..' ){
 if((time()-filemtime($this->img_dir.$file))>$this->timeout){
 @unlink($this->img_dir . $file);
 }
 }
 }
 $files->close();
 }
 
 private function createbg() {
 $this->img = imagecreatetruecolor($this->width, $this->height);
 $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
 imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
 }
 
 private function createfont() {
 $_x = $this->width / $this->length;
 for ($i=0;$i<$this->length;$i++) {
 $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
 imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->words[$i]);
 }
 }
 
 private function createline() {
 //线条
 for ($i=0;$i<6;$i++) {
 $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
 imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
 }
 //雪花
 for ($i=0;$i<8;$i++) {
 $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
 imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
 }
 }
 
 public function get_random(){
 return '?'.random(4,1);
 }
 
 public function generate()
 {
 HTTP::no_cache_header();
 FIRM::session()->captcha = strtolower($this->words);
 $this->createbg();
 $this->createline();
 $this->createfont();
 imagepng($this->img,$this->img_dir . $this->img_name);
 //header('Content-type:image/png');
 readfile($this->img_dir.$this->img_name);
 }
 
 public function is_validate($seccode)
 {
 if($seccode == FIRM::session()->captcha)
 {
 unset(FIRM::session()->captcha);
 return true;
 }
 else
 {
 unset(FIRM::session()->captcha);
 return false;
 }
 }
 }