php 实现简单的验证码

<?php
class Verify
{
    // 图片宽
    private $width;
    // 图片高
    private $height;
    // 图片类型
    private $imgType;
    // 字体类型
    private $type;
    // 字体个数
    private $num;
    // 资源
    private $img;
    // 画布上的字符串
    private $getCode;
    // 初始化一批成员属性
    public function __construct($width=100, $height=40, $imgType='jpeg', $num=4, $type=3 )
    {
        $this->width = $width;
        $this->height = $height;
        $this->imgType = $imgType;
        $this->num = $num;
        $this->type = $type;
        $this->getCode = $this->getCode();
        // echo $this->getCode;
    }
    // 获取字符串
    private function getCode()
    {
        // $$sthing  = '';
        switch($this->type){
            case 1:
                // $sthing = join('',array_rand(range(0,9),$this->num));
                $string = implode('',array_rand(range(0,9),$this->num));
            break;
            case 2:
                $string = implode('', array_rand(array_flip(range('a' , 'z')),$this->num)); //array_flip 键值对调 implode 把数组元素组合为字符串:
            break;
            case 3:
               // $str='abcdefghijklmnpqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM123456789';
               //$string = substr(str_shuffle($str),0,$this->num); //str_shuffle 随机打乱所有字符; substr  函数返回字符串的一部分。
                for($i = 0; $i<$this->num; $i++){
                    $rand = mt_rand(0 , 2);
                    switch($rand){
                        case 0;
                            $char = mt_rand(48,57);
                            break;
                        case 1;
                            $char = mt_rand(65,90);
                            break;
                        case 2;
                            $char = mt_rand(97,122);
                            break;
                    }
                    $string.=sprintf('%c',$char);
                    
                }
            break;
            

        }
        return $string;
    }
    // 创建画布 
    private function createImg()
    {
        $this->img = imagecreatetruecolor($this->width, $this->height);
    }
    // 背景颜色
    private function bgColor()
    {
        return imagecolorallocate($this->img,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
        
    }
    // 准备字体颜色
    private function fontColor()
    {
        return imagecolorallocate($this->img,mt_rand(0,123),mt_rand(0,123),mt_rand(0,130));
    }
    // t填充背景色
    private function fill()
    {
        return imagefilledrectangle($this->img,0,0,$this->width,$this->height,$this->bgColor());
         
    }
    // 画点
    private function pixed()
    {
        for($i = 0; $i<50; $i++)
        {
            imagesetpixel($this->img,mt_rand(0,$this->width), mt_rand(0,$this->height),$this->fontColor());
        }
 
    }
    //画线
    private function arc()
    {
        for($i = 0; $i<5; $i++)
        {
            imageline($this->img,mt_rand(0,120),mt_rand(0,40),mt_rand(0,120),mt_rand(0,40),$this->bgColor());
            // imagearc($this->img,mt_rand(10,$this->width), mt_rand(10,$this->height),80 , 20, 20 , 200, $this->fontColor());
        }
    }
    //写字
    private function write()
    {
        for ($i = 0; $i < $this->num; $i++)
        {
            $x = ceil($this->width/$this->num)*$i + mt_rand(5,10); //ceil 取整
            $y = mt_rand(5,$this->height - 20);

            imagechar($this->img,10,$x,$y,$this->getCode[$i],$this->fontColor());
            
        }
    }

    //输出图片
    private function out()
    {
        $func = 'image'.$this->imgType;
        $heaber = 'Content-type:image'.$this->imgType;
        // 判断这个函数是否存在
        if(function_exists($func)){
            $func($this->img);
            header($heaber);

        }else{
            exit('不支持该图片类型');
        }
    }
    //得到图片
    public function getImg()
    {
        $this->createImg();
        $this->fill();
        $this->arc();
        $this->pixed();
        $this->write();
        $this->out();

    }

    //销毁资源
    public function __destruct()
    {
        imagedestroy($this->img);
    }

}

$a = new Verify();
$a->getImg();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值