php 封装验证码类

<?php

 

//封装生成验证码的类

 

class codes{

    private $imgX; //验证码长度

    private $imgY;//验证码高

    private $codeNumber; //验证码个数

    private $code;//验证码

    private $str="abcedefghijkmnpqrstuvwxyz0123456789QWERTYUIOPASDFGHJKLZXCVBNM"; //验证码来源

    private $img; //储存图像


 

    //调用方法

    public function outIMG(){

        $this->background();

        $this->getcode();

        $this->setbg();

        $this->iron_tor();

        $this->iron_dir();

        $this->PrintfImg();

    }




 

    public function __construct($imgX=100,$imgY=40,$codeNumber=4) //默认值,如果有其他值则将默认值改为设定的值

    {

        $this->imgX=$imgX;

        $this->imgY=$imgY;

        $this->codeNumber=$codeNumber;

    }

 

    //生成验证码

    private function getcode(){

        for($i=0;$i<$this->codeNumber;$i++){

            $key=rand(1,strlen($this->str)-1);

            $this->code.=$this->str[$key]; //连接字符

        }

        $this->Setsession();

    }

 

        //生成背景图片

        private function background(){

            $this->img=imagecreate($this->imgX,$this->imgY);

            imagecolorallocate($this->img,255,255,255);

    

        }

 

    //验证码写入背景

    private function setbg(){

        for($i=0;$i<=$this->codeNumber;$i++){

            $font_size=rand(5,7);//字体大小

            imagecolorallocate($this->img,255,255,255);//给图片一个颜色

            $color=imagecolorallocate($this->img,rand(1,255),rand(1,255),rand(1,255));//给字体添加颜色 属性:1.在那个图片里面 2.文字颜色

            $height=imagefontheight($font_size); //获取字符在图片的高度 属性:字体大小

            $x=($this->imgX/$this->codeNumber)*$i+rand(1,10); //在图片中给x一个位置

            $y=rand(1,$this->imgY-$height); //在图片中给y一个位置 属性:y的位置不能超过图片的高度 所以是1-40为了防止贴在顶部所以减去一个字体的高度

            imagechar($this->img,$font_size,$x,$y,$this->code[$i],$color);  //给图片添加字符串 属性:字符大小,x,y,输出字符,字体颜色

            }

    }

 

    //设置干扰元素点

    private function iron_tor(){

        for($i=0;$i<30;$i++){

            $dot_color=imagecolorallocate($this->img,rand(1,255),rand(1,255),rand(1,255));

            imagesetpixel($this->img,rand(1,99),rand(1,39),$dot_color);//在图片中随机生成一个1像素的小点

        }

    }

 

    //生成干扰曲线

    private function iron_dir(){

        for($i=0;$i<3;$i++){

            $setxd=imagecolorallocate($this->img,rand(1,255),rand(1,255),rand(1,255));

            imagearc($this->img,rand(1,99),rand(1,39),100,2,30,10,$setxd);//画一条曲线(实际是椭圆) 属性:图像,x,y,宽,高,起始点,结束点,颜色

            }

    }



 

    //输出图片

 

    private function PrintfImg(){

        header('Content-type:image/jpeg');

        imagejpeg($this->img);

    }

 

    //保存验证码

    private function Setsession(){

       session_start();

       $_SESSION['code']=strtolower($this->code);

    }

}

 

$code =new codes();

//清除缓存区

$code->outIMG();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值