|
本帖最后由 iranwang 于 2011-8-6 16:24 编辑
写了一个验证码类 大家提提意见
PHP复制代码
<?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[0], $this->bgcolor[1], $this->bgcolor[2]);//设计背景颜色
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调用
PHP复制代码
/**
* 验证码生成图片
* 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);
}
复制代码
|
评分
-
查看全部评分
|