iranwang 发表于 2011-8-6 16:22:24

ci图片验证码类

本帖最后由 iranwang 于 2011-8-6 16:24 编辑

写了一个验证码类 大家提提意见

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* @version 0.1
* @author iranw<wang_wenguan@yeah.net>
*/
class Icaptcha{
    public $width = 100;    //图片宽度
    public $height = 30;    //图片高度
    public $line_num = 4;    //干扰线数量
    public $dot_num = 30;    //干扰点数量
    public $fontsize = 5;    //字体大小
    public $mar_left = 25;    //字体居左宽度
    public $pad    = 15;      //字体间间距
    public $lenght = 4;      //设置验证码长度
    public $randstr = "";
    public $bgcolor = array(255,255,255);//设置背景颜色如果未设定 则默认为白色数组形式array("255","255","255")
   
    private $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';   
    private $im = "";
   
    function __construct(){
    }
   
    public function createimg($width=100,$height=30,$length=4){
      $this->width = $width;
      $this->height = $height;
      $this->lenght = $length;
      $this->im = imagecreate($this->width, $this->height);   

      $this->SetBgColor();      //设置背景色
      $this->SetDot();            //设置干扰点
      $this->SetLine();            //设置干扰线            
      $this->GetRandStr();      //获取随机字符   
      
      for($i=0;$i<$this->lenght;$i++){
            $c_position = !$i?$this->mar_left:$c_position+$this->pad;
            $color = ImageColorAllocate ( $this->im, mt_rand ( 0, 100 ), mt_rand ( 0, 100 ), mt_rand ( 0, 100 ) ); //字符随即颜色
            ImageChar ( $this->im, $this->fontsize, $c_position, 10, $this->randstr[$i], $color ); //绘字符
      }
      Imagegif ( $this->im );
      ImageDestroy ( $this->im );
    }
   
   
    private function SetBgColor(){
      $bgcolor = imagecolorallocate($this->im, $this->bgcolor,$this->bgcolor,$this->bgcolor);//设计背景颜色
      imagefill($this->im, 0, 0, $bgcolor);      //填充背景色
    }
   
    private function SetDot(){
      for($i=0;$i<$this->dot_num;$i++){
            $color = ImageColorAllocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) ); //干扰点颜色
            ImageSetPixel ( $this->im, mt_rand ( 0, $this->width ), mt_rand ( 0, $this->height ), $color ); //干扰点
      }
    }
   
    private function SetLine(){
      for($i=0;$i<$this->line_num;$i++){
            $color = ImageColorAllocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) ); //干扰线颜色
            ImageArc ( $this->im, mt_rand ( - 5, $this->width ), mt_rand ( - 5, $this->height ), mt_rand ( 20, 300 ), mt_rand ( 20, 200 ), 55, 44, $color ); //干扰线
      }
    }
   
    private function GetRandStr(){
      $this->randstr = "";
      while (strlen($this->randstr)<$this->lenght){
            $this->randstr .= substr($this->chars, (rand()/strlen($this->chars)),1);
      }      
    }   
}
/* End of file icaptcha.php */
/* Location: ./application/libraries/icaptcha.php */


controllers调用

    /**
   * 验证码生成图片
   * Enter description here ...
   */
    public function captcha(){      
      Header ( "Content-type: image/gif" );
      $this->icaptcha->dot_num = 100;      
      $this->icaptcha->line_num = 4;
      $this->icaptcha->bgcolor= array(255,255,0);
      $this->icaptcha->createimg(100,30,4);   
      $this->session->set_userdata('icaptcha',$this->icaptcha->randstr);
    }


gs129090 发表于 2011-8-6 19:57:11

支持一下

dragonjahi 发表于 2011-8-7 19:43:32

{:1_1:}是吗?挺好的!

小凡 发表于 2011-8-8 00:46:25

支持下 {:1_1:}

如是如是 发表于 2012-2-1 14:12:01

输出的是乱码?何解?

wangcaiwen 发表于 2012-2-2 16:12:38

rand()/strlen($this->chars)里的rand()改成rand(0,strlen($this->chars))
private function GetRandStr(){
      $this->randstr = "";
      while (strlen($this->randstr)<$this->lenght){
            $this->randstr .= substr($this->chars, (rand()/strlen($this->chars)),1);
      }      
    }   


深深的呼吸 发表于 2012-10-24 15:34:01

我一用就感到震惊了

图片和显示的session不对!!!!

深深的呼吸 发表于 2012-10-24 15:34:51

wangcaiwen 发表于 2012-2-2 16:12 static/image/common/back.gif
**** 作者被禁止或删除 内容自动屏蔽 ****

改了之后图片总是显示一样的了

smallhe 发表于 2013-4-8 09:03:17

标记一下,有用

︶ㄣ挖婆婆丁的 发表于 2013-11-21 10:56:03

为什么是乱码????
页: [1]
查看完整版本: ci图片验证码类