PHP Class - 验证码

测试文件

<!DOCTYPE html>
<html>
<head>
    <title>验证码测试</title>
</head>
<body>
    <img src="./Code.php">
</body>
</html>

类文件

<?php

$code = new Code();
$code->outImage();

/*
 * @Purpose 验证码
*/
class Code
{
    // 验证码个数
    protected $number;
    // 类型0,1,2
    protected $codeType;
    // 图像宽度
    protected $width;
    // 图像高度
    protected $height;
    // 图像
    protected $image;
    // 验证码字符串
    protected $code;

    // 构造函数
    public function __construct($number=4, $codeType=2, $width=100, $height=50)
    {
        $this->number = $number;
        $this->codeType = $codeType;
        $this->width = $width;
        $this->height = $height;

        $this->code = $this->createCode();
    }

    // 析构函数 图像销毁
    public function __destruct()
    {
        imagedestroy($this->image);
    }

    // 获取属性 获取验证码字符串
    public function __get($name)
    {
        if($name == 'code') {
            return $this->code;
        }
        return false;
    }

    // 创建字符串
    protected function createCode()
    {
        switch ($this->codeType) {
            case 0: // 纯数字
                $code = $this->getNumberCode();
                break;
            case 1: //纯字母
                $code = $this->getCharCode();
                break;
            case 2: // 数字字幕混合
                $code = $this->getNumCharCode();
                break;
            default:
                die('不支持这种验证码类型');
                break;
        }

        return $code;
    }

    // 得到纯数字字符串
    protected function getNumberCode()
    {
        $str = join('', range(0, 9));

        return substr(str_shuffle($str), 0, $this->number);
    }

    // 得到纯字母字符串
    protected function getCharCode()
    {
        $str = join('', range('a', 'z'));
        $str = $str.strtoupper($str);

        return substr(str_shuffle($str), 0, $this->number);
    }

    // 得到数字字母混合字符串
    protected function getNumCharCode()
    {
        $numStr = join('', range(0, 9));
        $str = join('', range('a', 'z'));
        $str = $numStr.$str.strtoupper($str);

        return substr(str_shuffle($str), 0, $this->number);
    }

    // 新建一个真彩色图像
    protected function createImage()
    {
        $this->image = imagecreatetruecolor($this->width, $this->height);
    }

    // 填充色彩
    protected function fillBack()
    {
        imagefill($this->image, 0, 0, $this->lightColor());
    }

    // 亮色
    protected function lightColor()
    {
        return imagecolorallocate($this->image, mt_rand(130,255), mt_rand(130,255), mt_rand(130,255));
    }

    // 暗色
    protected function darkColor()
    {
        return imagecolorallocate($this->image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));
    }

    // 将验证码字符串画到画布中
    protected function drawChar()
    {
        $width = ceil($this->width / $this->number);
        for ($i=0; $i < $this->number; $i++) {
            $x = mt_rand($width * $i + 5, $width * ($i+1) - 10);
            $y = mt_rand(0, $this->height -15);
            imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor());
        }
    }

    // 输出干扰元素
    protected function drawDisturb()
    {
        for ($i = 0; $i < 150; $i++) {
            $x = mt_rand(0, $this->width);
            $y = mt_rand(0, $this->height);
            
            imagesetpixel($this->image, $x, $y, $this->lightColor());
            
        }
        for ($i=0; $i < 5; $i++) { 
            $x1 = mt_rand(0, $this->width / 2);
            $x2 = mt_rand(0, $this->width);
            $y1 = mt_rand(0, $this->height);
            $y2 = mt_rand(0, $this->height);
            imageline($this->image, $x1, $y1, $x2, $y2, $this->lightColor());
        }
        
    }

    // 输出并显示
    protected function show()
    {
        header('Content-Type:image/png');
        imagepng($this->image);
    }

    // 输出图像
    public function outImage()
    {
        // 创建画布
        $this->createImage();
        // 填充背景色
        $this->fillBack();
        // 将验证码字符串画到画布中
        $this->drawChar();
        // 输出干扰元素
        $this->drawDisturb();
        // 输出并显示
        $this->show();
    }
}

上一篇:Python - 面向对象(三)公共变量,受保护变量,私有变量


下一篇:Py public protected private