-
// +------------------------------------------------------------------------
- // 验证码类,该类的对象能动态获取验证码图片,验证码字符保存在SESSION['code']中
- // +------------------------------------------------------------------------
- // 支持4种格式 数字 字母 汉字 混合
- // +------------------------------------------------------------------------
- // @Author: HelloChina(sanzi0930@163.com)
- // +------------------------------------------------------------------------
- // @Date: 2012年6月7日11:03:00
- // +------------------------------------------------------------------------
- // @version 1.0
- // +------------------------------------------------------------------------
-
- class Vcode{
- protected $width; //验证码宽度
- protected $height; //验证码长度
- protected $codeNum; //验证码字符个数
- protected $codeType; //验证码类型
- protected $fontSize; //字符大小
- protected $fontType; //字体类型
- protected $codeStr; //中文内容
- protected $strNum; //中文个数
- protected $imageType; //输出图片类型
- protected $image; //图片资源
- protected $checkCode; //验证码内容
- /**
- +--------------------------------------------------------------------------------
- * 取得验证码信息
- +--------------------------------------------------------------------------------
- * @param integer $width 验证码宽度
- * @param integer $height 验证码高度
- * @param integer $codeNum 验证码字符个数
- * @param integer $codeType 验证码字符类型 1为数字 2为字母 3为汉字 4为混编
- * @param integer $fontSize 验证码字体的大小
- * @param string $fontType 验证码字体类型
- * @param string $imageType 验证码输出图片类型
- * @param string $codestr 中文验证码内容
- +--------------------------------------------------------------------------------
- */
- public function __construct($width=100, $height=50, $codeNum=4, $codeType=4, $fontSize=12, $fontType='heiti.ttf' ,$imageType='jpeg', $codeStr='去我饿人他一哦平啊是的飞个好就看了在想才吧你吗'){
- $this->width = $width;
- $this->height = $height;
- $this->codeNum = $codeNum;
- $this->codeType = $codeType;
- $this->fontSize = $fontSize;
- $this->fontType = $fontType;
- $this->codeStr = $codeStr;
- $this->strNum = strlen($this->codeStr)/3-1;
- $this->imageType = $imageType;
- $this->checkCode = $this->getCheckCode();
- }
-
- //+--------------------------------------------------------------------------------
- //* 生成验证码字符
- //+--------------------------------------------------------------------------------
- //* @return string
- //+--------------------------------------------------------------------------------
- public function __toString(){
- $string = implode('', $this->getCheckCode());
- $_SESSION["code"]=$string; //加到session中
- $this->getImage(); //输出验证码
- return '';
- }
- protected function getCheckCode(){
- $string = array();
- switch($this->codeType){
- case 1:
- //数字字符串
- $string = array_rand(range(0,9), $this->codeNum);
- break;
- case 2:
- //大字母字符串
- $string = array_rand(array_flip(range('A', 'Z')), $this->codeNum);
- break;
- case 3:
- //汉字字符串
-
- for($i=0; $icodeNum); $i++){
- $start = mt_rand(0, $this->strNum);
- $string[$i]= self::msubstr($this->codeStr,$start);
- }
- break;
- case 4:
- //混合字符串
- for($i=0; $icodeNum); $i++){
- $rand=mt_rand(0,2);
- switch($rand){
- case 0:
- $ascii = mt_rand(48,57);
- $string[$i] = sprintf('%c',$ascii);
- break;
复制代码
|