php面向对象画验证码

一、关于画验证码的重要的函数:

imagecreatetruecolor()--->创建画布

imagechar()---->画单个的字符

imagesetpixel()--->画干扰点

imagearc()---->画干扰弧线

imagecolorallocate()---->创建颜色,为画验证码做准备

imagefill()----->颜色填充

imagepng()---->以PNG格式将图片输出到浏览器或保存到文件

类的设计:成员属性

​ (1)要保存有关这个对象的成员信息。

​ 一般使用protected属性

​ (2)在这个类中很多成员方法都使用这个变量,$this->name 在类中类似于全局变量。

、成员方法

 
<?php
class Code 
{
    //图像的宽度
    protected $width;
    //图像的高度
    protected $height;
    //验证码的个数
    protected $number;
    //验证码的类型
    protected $type;
    //图像资源
    protected $image;
    //验证码字符串
    protected $code;
    function __construct($width = 100,$height = 50,$number = 4,$type = 2)
    {
        $this->width = $width;
        $this->height = $height;
        $this->number = $number;
        $this->type = $type;
        //只要创建对象,就将验证码字符串生成
        $this->code = $this->getCode();
    }
    //生成验证码字符串函数
    protected function getCode()
    {
        switch ($this->type) {
            case 0://纯数字的情况
                $code = $this->getNumberCode();
                break;
            case 1://纯字母的情况
                $code = $this->getCharCode();
                break;
            case 2://混合的情况
                $code = $this->getMixCode();
                break;
            default:
                die('发生错误,没有这种类型的验证码');
        }
        return $code;
    }
    //得到纯数字验证码
    protected function getNumberCode()
    {
        for($i = 0;$i < $this->number;$i++){
            $arr[] = mt_rand(0,9);
        }
        return join('',$arr);
    }
​//得到纯字母验证码
    protected function getCharCode()
    {
        $arr1 = range('a', 'z');
        $arr2 = range('A', 'Z');
        $arr  = array_merge($arr1,$arr2);
        $str = join('',$arr);
        return substr(str_shuffle($str),0,$this->number);
    }
//得到混合类型的验证码
    protected function getMixCode()
    {
        $arr1 = range('a', 'z');
        $arr2 = range('A', 'Z');
        $arr3 = range(0,9);
        $arr  = array_merge($arr1,$arr2,$arr3);
        $str = join('',$arr);
        return substr(str_shuffle($str),0,$this->number);
    }
    //外部调用这个方法画出验证码
    public function outImage()
    {
        //创建画布
        $this->image = $this->createImage();
        //创建颜色,深色 浅色
        //填充背景颜色
        imagefill($this->image, 0, 0, $this->lightCorlor());
        //化验证码
        $this->drawCode();
        //画干扰元素
        $this->trouble();
        //输出图像
        header('content-type:image/png');
        imagepng($this->image);
    }
    protected function trouble()
    {
        //计算干扰点的总个数
        $n = ($this->width * $this->height) / 20;
        for($i = 0; $i < $n; $i++){
            $x = mt_rand(0,$this->width);
            $y = mt_rand(0,$this->height);
            imagesetpixel($this->image, $x, $y, $this->darkColor());
        }
        //画干扰弧线
            $m = 5;
            for($j = 0;$j < $m; $j ++){
                $cx = mt_rand(0,$this->width);
                $cy = mt_rand(0,$this->height);
                $width = mt_rand(0,100);
                $height = mt_rand(0,50);
                $s = mt_rand(0,90);
                $e = mt_rand(90,180);
                imagearc($this->image,$cx,$cy,$width,$height,$s,$e,$this->darkColor());
            }
        }
         function drawCode()
        {
            //得到一个格子的宽度
            $width = $this->width / $this->number;
            for($i = 0; $i < $this->number;$i++){
                $x = mt_rand($i*$width + 5,($i + 1)*$width - 10);
                $y = mt_rand(0,$this->height - 15);
                //画验证码
                imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor());
            }
        }
         function createImage()
        {
            return imagecreatetruecolor($this->width, $this->height);
        }
         function lightCorlor()
        {
            return imagecolorallocate($this->image, mt_rand(150,255), mt_rand(150,255), mt_rand(150,255));
        }
         function darkColor()
        {
            return imagecolorallocate($this->image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));
        }
        //写这个魔术方法让外部也可以获取code的值
        function __get($name)
        {
            if ($name == 'code') {
                return $this->$name;
            }
            return false;
        }
        function __destruct()
        {
            imagedestroy($this->image);
        }
    
}
$code = new Code();
$code->outImage();

4.分页类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值