封装PHP验证码类

封装PHP验证码类

写了一个PHP验证码类封装,用的话,直接拿过去就行

Captcha.class.php

<?php

class Captcha
{
    //字体文件
    private $_fontfile = '';
    //画布宽度
    private $_width = 120;
    //画布高度
    private $_height = 40;
    //字体大小
    private $_fontsize = 16;
    //验证码长度
    private $_length = 4;
    //画布资源
    private $_img = null;
    //干扰元素
    private $_snow = 0;
    private $_pixel = 0;
    private $_line = 0;


    /**
     * 初始化数据
     * @p8aram = $config
     */
    public function __construct($config = array())
    {
        if (is_array($config) && (count($config) > 0)) {
            //检测字体文件是否存在并且刻度
            if (isset($config['fontfile']) && is_file($config['fontfile']) && is_readable($config['fontfile'])) {
                $this->_fontfile = $config['fontfile'];
            } else {
                return false;
            }
            //检测是否设置验证码长度
            if (isset($config['length']) && $config['length'] > 0) {
                $this->_length = (int)$config['length'];
            }
            //检测是否存在字体大小
            if (isset($config['fontsize']) && $config['fontsize'] > 0) {
                $this->_fontsize = (int)$config['fontsize'];
            }

            //检测是否存在宽和高
            if (isset($config['width']) && $config['width'] > 0) {
                $this->_width = (int)$config['width'];
            }
            if (isset($config['height']) && $config['height'] > 0) {
                $this->_height = (int)$config['height'];
            }
            //配置干扰元素
            if (isset($config['snow']) && $config['snow'] > 0) {
                $this->_snow = (int)$config['snow'];
            }
            if (isset($config['pixel']) && $config['pixel'] > 0) {
                $this->_pixel = (int)$config['pixel'];
            }
            if (isset($config['line']) && $config['line'] > 0) {
                $this->_line = (int)$config['line'];
            }

            //创建画布
            $this->_img = imagecreatetruecolor($this->_width, $this->_height);
            return $this->_img;
        } else {
            return false;
        }
    }

    //生成验证码
    public  function getCaptcha()
    {
        $white = imagecolorallocate($this->img, 255, 255, 255);
        //填充背景颜色
        imagefilledrectangle($this->_img, 0, 0, $this->_width, $this->_height, $white);
        //生成随机验证码字符串
        $str = $this->_generate($this->_length);
        if ($str === false) {
            return false;
        }

        //绘制验证码
        for ($i = 0; $i < $this->_length; $i++) {
            $size = $this->_fontsize;
            $angle = mt_rand(-25, 25);
            $x = ceil(($this->_width / $this->_length)) * $i + mt_rand(5, 10);
            $y = mt_rand(ceil($this->_height / 3), ($this->_height) - 5);
            $color = $this->_getcolor();
            $fontfile = $this->_fontfile;
            // $text = mb_substr($str, $i ,1);
            $text = $str[$i];
            imagettftext($this->_img, $size, $angle, $x, $y, $color, $fontfile, $text);
        }

        //绘制验证码
        if ($this->_snow) {
            //雪花
            $this->_getSnow();
        } else {
            if ($this->_pixel) {
                //像素
                $this->_getPixsel();
            }
            if ($this->_line) {
                //线段
                $this->_getLine();
            }
        }

        //输出图像
        ob_clean();
        // echo $str;
        header('Content-type:image/png');
        imagepng($this->_img);
        imagedestroy($this->_img);
        // return strtolower($str);
    }

    /**
     * 生成雪花
     * 
     * */
    private  function _getSnow()
    {
        for ($i = 1; $i <= $this->_snow; $i++) {
            imagestring($this->_img, mt_rand(1, 5), mt_rand(0, $this->_width), mt_rand(0, $this->_height), '*', $this->_getcolor());
        }
    }

    /**
     * 生成像素
     */
    private  function _getPixsel()
    {
        for ($i = 0; $i <= $this->_pixel; $i++) {
            imagesetpixel($this->_img, mt_rand(0, $this->_width), mt_rand(0, $this->_height), $this->_getcolor());
        }
    }

    /**
     * 生成线
     */
    private function _getLine()
    {
        for ($i = 1; $i <= $this->_line; $i++) {
            imageline($this->_img, mt_rand(0, $this->_width), mt_rand(0, $this->_height), mt_rand(0, $this->_width), mt_rand(0, $this->_height), $this->_getcolor());
        }
    }
    /**
     * 随机生成字符串
     * string
     */
    private function _generate($length = 4)
    {
        if ($length < 1 || $length > 30) {
            return false;
        }
        $chars = array_merge(range(0, 9), range('a', 'z'), range("A", "Z"));
        $str = join('', array_rand(array_flip($chars), $length));
        return $str;
    }




    /**
     * 生成颜色
     * 
     */
    private function _getcolor()
    {
        return imagecolorallocate($this->_img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    }
}

代码调用

<?php

require_once 'Captcha.class.php';

$config = array(
    'fontfile'=>'1.ttf',
    'length'=>4,
    'fontsize'=>'',
    'width'=>'',
    'height'=>'',
    'snow'=>'',
    'pixel'=>'',
    'line'=>''
);

$cap = new Captcha($config);
$cap->getCaptcha();

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值