验证码类 数字 字母 汉字

<?php
// +------------------------------------------------------------------------
// 验证码类,该类的对象能动态获取验证码图片,验证码字符保存在SESSION['code']中
// +------------------------------------------------------------------------
// 支持4种格式 数字 字母 汉字 混合
// +------------------------------------------------------------------------


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; $i < ($this->codeNum); $i++) {
          $start = mt_rand(0, $this->strNum);
          $string[$i] = self::msubstr($this->codeStr, $start);
        }
        break;
      case 4:
        //混合字符串
        for ($i = 0; $i < ($this->codeNum); $i++) {
          $rand = mt_rand(0, 2);
          switch ($rand) {
            case 0:
              $ascii = mt_rand(48, 57);
              $string[$i] = sprintf('%c', $ascii);
              break;
          }
        }
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值