php生成验证码

<?php
namespace service;
class VerifyCode {
    /**
     * 存放图片,字体的根目录
     * @var string
     */
    private $resourceRoot = __DIR__;
    /**
     * 验证码的背景图
     * @var string
     */
    private $jamImg = "/jam_1.png";
    /**
     * 生成验证码所用的字体
     * @var string
     */
    private $font = '/LTYPEB.TTF';
    /**
     * 验证码图片宽
     * @var int
     */
    private $sizeWidth = 96;
    /**
     * 验证码图片高
     * @var type
     */
    private $sizeHeight = 30;
    /**
     * 验证码长度
     * @var int
     */
    private $codeLength = 4;
    /**
     * 验证码所包含的字符类型
     * @var int
     */
    private $chrType = 1;//1 数字,2 大写字母, 4 小写字母, 8 中文 混合类型为对应类型之和
    /**
     * 本次验证码内容
     * @var string
     */
    private $code = array();
    /**
     * 保存生成验证码将要用到的字符
     * @var array
     */
    private $chrs;
    /**
     * 生成的图片
     * @var resource
     */
    private $img;
    /**
     * 构造函数
     */
    public function __construct($chrType = null, $codeLength = null) {
        if($chrType){
            $this->setChrType($chrType);
        }
        if($codeLength){
            $this->setCodeLength($codeLength);
        }
        $this->jamImg = $this->resourceRoot.$this->jamImg;
        $this->font = $this->resourceRoot.$this->font;
    }
    /**
     * 设置生成验证码要用的字符类型
     * @param int $chrType 1 数字,2 大写字母, 4 小写字母, 8 中文 混合类型为对应类型之和
     */
    public function setChrType($chrType){
        $this->chrType = $chrType;
    }
    /**
     * 设置验证码的长度
     * @param int $codeLength 验证码长度
     */
    public function setCodeLength($codeLength){
        $this->codeLength = $codeLength;
    }
    /**
     * 初始化生成验证码将要用到的字符集
     */
    private function initChrs(){
        $this->chrs = array(
            'length' => 0,
            'code' => array()
        );
        if($this->chrType & 1){//数字
            $this->addAscii(48, 10);
        }
        if($this->chrType & 2){//大写字母
            $this->addAscii(65, 26);
        }
        if($this->chrType & 4){//小写字母
            $this->addAscii(97, 26);
        }
        if($this->chrType & 8){//一级中文字
            $this->addAscii(0, 3755);
        }
    }
    /**
     * 添加生成验证码要用到的字符集
     * @param int $start 字符集起始Ascii码
     * @param int $length 字符集长度
     */
    private function addAscii($start, $length){
        $this->chrs['code'][] = array($start, $length);
        $this->chrs['length'] += $length;
    }
    /**
     * 找出数值对应的中文字符
     * @param int $code 数值
     */
    private function codeToUTF8($code){
        $y = $code % 94;
        $x = ($code - $y) / 94;
        $gb2312 = dechex(176 + $x).dechex(160 + $y + 1);
        return iconv('gb2312', 'utf-8', pack('H*', $gb2312));
    }
    /**
     * 将随机生成的数值转换成对应的字符
     * @param int $code 随机数
     * @return string
     */
    private function transferCode($code){
        foreach($this->chrs['code'] as $item){
            if($code < $item[1]){
                $code += $item[0];
                break;
            }else{
                $code -= $item[1];
            }
        }
        if($item[0]){
            return chr($code);
        }else{
            return $this->codeToUTF8($code);
        }
    }
    /**
     * 生成验证码
     */
    public function generateCode(){
        $this->initChrs();
        for($i = 0; $i < $this->codeLength; $i++){
            $this->code[] = $this->transferCode(mt_rand(0, $this->chrs['length'] - 1));
        }
        return $this->getCode();
    }
    /**
     * 给制验证码
     */
    protected function createImage(){
        $jamImg = imagecreatefrompng($this->jamImg);
        $imageInfo = getimagesize($this->jamImg);
        $this->img = imagecreatetruecolor($this->sizeWidth ,$this->sizeHeight);
        imagefill($this->img, 0, 0, imagecolorallocate($this->img, 255, 255, 255));
        foreach($this->code as $i => $chr){
            $color = imagecolorallocate($this->img, rand(0,100), rand(0,100), rand(0,100));
            imagettftext($this->img, rand(20, 25), rand(350, 370), $i*18+rand(0,5) + 10, 24+rand(0,5), $color, $this->font, $chr);
        }
        imagecopy($this->img, $jamImg, 0, 0, rand(0,$imageInfo[0]-$this->sizeWidth), rand(0,$imageInfo[1]-$this->sizeHeight), $this->sizeWidth ,$this->sizeHeight);
    }
    /**
     * 输出验证码
     */
    protected function printImage(){
        header("Content-type: image/jpeg");
        imagejpeg($this->img, null, 80);
        imagedestroy($this->img);
    }
    /**
     * 生成验证码
     */
    public function draw(){
        $this->createImage();
        $this->printImage();
    }
    /**
     * 获取本次验证码内容
     * @return string
     */
    public function getCode(){
        return implode('', $this->code);
    }
}
?>
 public function picVerifyCode()
    {
        Session::start(true);
        $verifyCode = new VerifyCode(1,4);
        $_SESSION['verifyCode'] = $verifyCode->generateCode();
        $verifyCode->draw();
    }
原文地址:https://www.cnblogs.com/huangguojin/p/6656731.html