PHP Class - 验证码

PHP Class - 验证码,第1张

概述测试文件<!DOCTYPEhtml><html><head><title>验证码测试<itle></head><body><imgsrc="./Code.php"></body></html>类文件<?php$code=newCode();$code->outImage();/**@Pu

测试文件

<!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();    }}
总结

以上是内存溢出为你收集整理的PHP Class - 验证码全部内容,希望文章能够帮你解决PHP Class - 验证码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1001756.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存