0

0

漂亮的验证码

php中文网

php中文网

发布时间:2016-06-07 11:44:06

|

1607人浏览过

|

来源于php中文网

原创

分享一款漂亮的验证码。
此代码来源于网络,haran只是进行了简单的改写与整合。
效果
1、漂亮的验证码
2、漂亮的验证码
3、漂亮的验证码
4、漂亮的验证码
整合步骤
一、在你的项目/Lib中建立ORG目录,将TP框架/Extend/Library/ORG/Util/Image.class.php复制到刚刚建立的目录中。
二、打开刚刚复制过来的Image.class.php,将其中的buildImageVerify方法替换为以下代码    /**
    +----------------------------------------------------------
    * 生成图像验证码
    +----------------------------------------------------------
    * @static
    * @access public
    +----------------------------------------------------------
    * @param string $length 位数
    * @param string $mode 类型
    * @param string $type 图像格式
    * @param string $width 宽度
    * @param string $height 高度
    * @param string $size 字体大小
    +----------------------------------------------------------
    * @return string
    +----------------------------------------------------------
    */
    static function buildImageVerify( $length=5, $mode='', $type='png', $width=300, $height=100, $size=40, $verifyName='verify') {

        import('ORG.Util.String');
        $fontPath = "./Public/elephant.ttf";
        $randval = String::randString($length, $mode);
        session($verifyName, md5($randval));

        $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
        
        if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
            $im = @imagecreatetruecolor($width, $height);
        } else {
            $im = @imagecreate($width, $height);
        }

        $r = Array(225, 255, 255, 223);
        $g = Array(225, 236, 237, 255);
        $b = Array(225, 236, 166, 125);
        $key = mt_rand(0, 3);

        $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(随机)
        $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
        $pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //点颜色

        imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);

        @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
        $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
        // 干扰
        for ($i = 0; $i              $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
        }
        for ($i = 0; $i              $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
        }

        $x = $width/$length;
        for ($i = 0; $i              //imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
            imagettftext($im, $size, rand(-30,30) ,$x*$i+5, $height/1.4, $stringColor, $fontPath,$randval{$i});
        }
        
        // @imagestring($im, 5, 5, 3, $randval, $stringColor);
        Image::output($im, $type);
    }
三、下载elephant.ttf放置于项目目录/Public文件夹中
http://pan.baidu.com/share/link?uk=554464104&shareid=1278879446
四、建立生成验证的方法并调用     
    Class PublicAction extends Action {

        Public function verify () {
            import('@.ORG.Image');
            Image::buildImageVerify();
        }
    }
漂亮的验证码
-------------------------------------以下为懒人包-----------------------------------------------------------
将附件解压到和TP框架同级的目录中

附件 haran.rar ( 42.59 KB 下载:457 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

29

2025.12.25

错误代码dns_probe_possible
错误代码dns_probe_possible

本专题整合了电脑无法打开网页显示错误代码dns_probe_possible解决方法,阅读专题下面的文章了解更多处理方案。

20

2025.12.25

网页undefined啥意思
网页undefined啥意思

本专题整合了undefined相关内容,阅读下面的文章了解更多详细内容。后续继续更新。

37

2025.12.25

word转换成ppt教程大全
word转换成ppt教程大全

本专题整合了word转换成ppt教程,阅读专题下面的文章了解更多详细操作。

6

2025.12.25

msvcp140.dll丢失相关教程
msvcp140.dll丢失相关教程

本专题整合了msvcp140.dll丢失相关解决方法,阅读专题下面的文章了解更多详细操作。

2

2025.12.25

笔记本电脑卡反应很慢处理方法汇总
笔记本电脑卡反应很慢处理方法汇总

本专题整合了笔记本电脑卡反应慢解决方法,阅读专题下面的文章了解更多详细内容。

6

2025.12.25

微信调黑色模式教程
微信调黑色模式教程

本专题整合了微信调黑色模式教程,阅读下面的文章了解更多详细内容。

5

2025.12.25

ps入门教程
ps入门教程

本专题整合了ps相关教程,阅读下面的文章了解更多详细内容。

4

2025.12.25

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

218

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Git 教程
Git 教程

共21课时 | 2.2万人学习

Kotlin 教程
Kotlin 教程

共23课时 | 2万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号