ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP Class - 验证码

2021-05-29 16:00:38  阅读:146  来源: 互联网

标签:function rand width 验证码 mt protected str 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();
    }
}

标签:function,rand,width,验证码,mt,protected,str,PHP,Class
来源: https://blog.csdn.net/weixin_42412237/article/details/117386601

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有