zoukankan      html  css  js  c++  java
  • ThinkPHP

    首先,在项目目录下创建Class文件夹,用于存储个人类文件。

    之后建立Data目录存放所需字体文件,其他的数据也可以放在这个文件夹下。

    然后再Conf文件夹下创建verify.php配置文件。

    在config.php文件中引用verify.php配置文件。

    代码入下:

        //加载验证码配置
        'LOAD_EXT_CONFIG' => 'verify',

    要使用Class文件夹下的类,要这么引用:

            //引入自定义验证码类
            import('Class.ValidateCode', APP_PATH);
            ValidateCode::doVerify();

    效果:

    使用的验证码类,以及配置文件:

    配置文件:

    <?php
    return array(
        //验证配置
        'charset'    => 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789',//随机因子
        'codelen'    => 4,                    //验证码长度
        'width'      => 130,                //宽度
        'height'     => 50,                //高度
        'font'       => './App/Data/QDB.otf',        //指定的字体
        'fontsize'   => 30,                //指定字体大小
        'verifyName' => 'verify',            //session中验证码键值
    );


    验证码类:

    <?php
    //验证码类
    class ValidateCode {
    
        /**
         * 生成验证码
         * @return [type] [description]
         */
        static function doVerify() {
            //属性
            $img;
            $charset = C('charset');//随机因子
            $codelen = C('codelen');//验证码长度
            $width = C('width');//宽度
            $height = C('height');//高度
            $font = C('font');//指定的字体
            $fontsize = C('fontsize');//指定字体大小 
            $verifyName = C('verifyName');//session中验证码键值
    
    
            
            //生成随机码
            $_len = strlen($charset)-1;
            for ($i=0;$i<$codelen;$i++) {
                $code .= $charset[mt_rand(0,$_len)];
            }
    
    
            //生成背景
            $img = imagecreatetruecolor($width, $height);
            $color = imagecolorallocate($img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
            imagefilledrectangle($img,0,$height,$width,0,$color);
    
            //生成文字
            $_x = $width / $codelen;
            for ($i=0;$i<$codelen;$i++) {
                $fontcolor = imagecolorallocate($img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
                imagettftext($img,$fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$height / 1.4,$fontcolor,$font,$code[$i]);
            }
    
            //生成线条、雪花
            //线条
            for ($i=0;$i<6;$i++) {
                $linecolor = imagecolorallocate($img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
                imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$linecolor);
            }
            //雪花
            for ($i=0;$i<30;$i++) {
                $snowcolor = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
                imagestring($img,mt_rand(1,5),mt_rand(0,$width),mt_rand(0,$height),'.',$snowcolor);
            }
    
            //创建session
            //生成小写验证码进行md5加密,并存于session
            $_SESSION[$verifyName] = md5(strtolower($code));
    
            //输出
            header('Content-type:image/png');
            imagepng($img);
            imagedestroy($img);
        }
    }
  • 相关阅读:
    swift制作framework过程
    Redis学习
    Mysql vs NoSql vs NewSql
    Mysql实践
    跨库分页解决方案[转]
    二分查找解题套路框架 [转]
    Service Mesh [转]
    Golang Case
    [转]分布式服务限流
    保险
  • 原文地址:https://www.cnblogs.com/KTblog/p/5189506.html
Copyright © 2011-2022 走看看