简单实用的PHP验证码类

最近在学习php。实现了一个简单的验证码类,仅供参考。记得自己的PHP环境得有GD库。

先上效果图:

    图一

    图二

<?php

/** 
 * 验证码类
 */
class Code
{
    // 验证码个数
    protected $num;
    // 验证码类型(纯数字 纯字母 组合)
    protected $codeType;
    // 验证码宽度
    protected $width;
    // 验证码高度
    protected $height;
    // 验证码$code
    protected $code;
    // 图像资源$image
    protected $image;

    // 初始化成员变量,给默认值
    public function __construct($num = 4, $codeType = 2, $width = 100, $height = 50)
    {
        $this->num = $num;
        $this->codeType = $codeType;
        $this->width = $width;
        $this->height = $height;
        $this->code = $this->createCode();
        $this->image = $this->createImage();
        // echo $this->code;
    }

    // 创建验证码
    protected function createCode()
    {
        // 根据验证码类型调用不同方法
        switch ($this->codeType) {
            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()
    {
        // range(0, 9)返回数组 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        // join('', range(0,9))返回字符串'0123456789'
        $num = join('', range(0, 9));
        // str_shuffle($num)打乱'0123456789', 再用substr()截取打乱后的前四位字符串
        return substr(str_shuffle($num), 0, 4);
    }

    // 纯字母验证码
    protected function getCharCode()
    {
        $char = join('', range('a', 'z'));
        $char = $char . strtoupper($char);

        return substr(str_shuffle($char), 0, 4);
    }

    // 字母数字组合验证码
    protected function getMixCode()
    {
        $num = join('', range(0, 9));
        $char = join('', range('a', 'z'));
        $mix = $num . $char . strtoupper($char);

        return substr(str_shuffle($mix), 0, 4);
    }

    // 创建图像
    protected function createImage()
    {
        // 创建画布
        $image = imagecreatetruecolor($this->width, $this->height);
        // 填充颜色
        imagefill($image, 0, 0, $this->lightColor($image));
        // 画字符 根据传递的字符数量$this->num,这里默认值为4
        for ($i = 0; $i < $this->num; $i++) {
            // 分别取x坐标(0, 25) (25, 50)(50, 75)(75, 100),  +10 和-15 是为了是字符距离左右有点空隙,可以自己微调
            $x = mt_rand(floor($this->width / $this->num) * $i + 10, floor($this->width / $this->num) * ($i + 1) - 15);
            $y = mt_rand(10, $this->height - 20);
            imagechar($image, 5, $x, $y, $this->code[$i], $this->darkColor($image));
        }
        // 干扰点 150个
        for ($i = 0; $i < 150; $i++) {
            imagesetpixel($image, mt_rand(0, $this->width), mt_rand(0, $this->height), $this->darkColor($image));
        }
        // 干扰线 5条
        for ($i = 0; $i < 5; $i++) {
            imageline($image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $this->darkColor($image));
        }
        // 设置mime
        header('Content-type:image/png');
        // 输出图像
        imagepng($image);
        // 释放资源
        imagedestroy($image);
    }

    // 浅色
    protected function lightColor($image)
    {
        // 浅色的rgb值范围大概是130~255之间
        return imagecolorallocate($image, mt_rand(130, 255), mt_rand(130, 255), mt_rand(130, 255));
    }
    // 深色
    protected function darkColor($image)
    {
        // 深色的rgb值范围大概是0~120之间
        return imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
    }

}

在一个新文件里实例化就可以使用。

猜你喜欢

转载自www.cnblogs.com/rancelotus/p/10006795.html