//

Information for details

php常用类分享之验证码图片类

验证码生成类:

功能:生成验证码。

本文实例讲述了PHP实现的简单验证码类及其详细的使用方法。分享给大家供大家参考,具体如下

<?php

/**
 * 验证码图片类
*/

class verify{

    //声明成员属性          
    private $width; //宽
    private $height; //高
    private $imagetype; //图片类型
    private $num; //显示字符的个数
    private $checkcode; //返回给session 的字符串
    private $img; //定义一个图片资源
    private $type; //验证码的类型 ,1 数字 2 字母 3 组合型
    
    //初始化 默认的参数
    public function __construct($width='100',$height='30',$num='4',$type='3',$imagetype='jpeg'){
        $this->width=$width;
        $this->height=$height;
        $this->num=$num;
        $this->type=$type;
        $this->imagetype=$imagetype;
        $this->checkcode=$this->getcheckcode();
    }
    
    private function getcheckcode(){ //产生字符串的方法
        switch($this->type){
            case 1://数字类型的
                $string=join('',array_rand(range(0,9),$this->num));
            break;
            case 2://字母类型的
                $string=implode('',array_rand(array_flip(range('a','z')),$this->num));
            break;
            case 3://字母与数字混合性
                $str='23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
                $s=str_shuffle($str);
                $string=substr($s,0,$this->num);
            break;
        }
        return $string;
    }

    //定义产生画布的方法
    private function createimage(){
        $this->img=imagecreatetruecolor($this->width,$this->height);
    }
    
    //产生背景颜色,颜色要比字体的颜色浅
    private function bgcolorrand(){
        return imagecolorallocate($this->img,mt_rand(230,255),mt_rand(230,255),mt_rand(230,255));
    }
    
    //产生字体的颜色,颜色要比背景颜色深
    private function textcolorrand(){
        return imagecolorallocate($this->img,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
    }
    
    //填充背景的颜色
    private function filledbg(){
        imagefilledrectangle($this->img,0,0,$this->width,$this->height,$this->bgcolorrand());
    }
    
    //添加干扰点
    private function filledpix(){
        for($i;$i<70;$i++){
            $x=mt_rand(2,$this->width-5);
            $y=mt_rand(2,$this->height-5);
            imagesetpixel($this->img,$x,$y,$this->textcolorrand());
        }
    }
    
    //循环写上字符
    private function writetext(){
        for($i=0;$i<$this->num;$i++){
            $x=ceil($this->width/$this->num)*$i+5;
            $y=mt_rand(5,$this->height-20);
            imagechar($this->img,5,$x,$y,$this->checkcode[$i],$this->textcolorrand());
        }
    }
    
    //输出图片
    private function output(){
        $func='image'.$this->imagetype;
        $header='content-type:image/'.$this->imagetype;
        /*----判断我们链接的这个字符串的名字是不是函数----*/
        if(function_exists($func)){
            header($header);
            $func($this->img);
        }else{
            echo '当前系统不支持这种图片的格式';
            return false;
        }
    }
    
    //获得图片的方法
    public function getimg(){
        $this->createimage();//创建画布方法
        $this->filledbg();//填充背景方法
        $this->filledpix();//填充干扰点方法
        $this->writetext();//写上随机字符方法
        $this->output();//输出图片方法
    }

    function __get($proname){
        if($proname=='checkcode'){
            return $this->$proname;
        }else{
            return false;
        }
    }

    //销毁图片资源,自动执行
    public function __destruct(){
        imagedestroy($this->img);
    }
}

$verify=new verify();
$verify->getimg();
?>


然成科技是一家本土的云南软件开发公司,2017年获得云南省科技厅、省财政厅、省税务局等机构认定的高新技术企业。定位于高端网站设计、系统开发、APP开发、微信公众号、微信小程序开发。拥有多个软著权,公司先后为多家事业单位提供服务,比如会泽文化馆、宣威文化馆、中科院昆明植物研究所等。其中中国科学院昆明植物研究所已经进行了长达4年的合作,并且还持续合作中!

  • 滇公网安备 53010202001388号