首页 > php教程 > PHP源码 > 正文

关于php验证码类的实例分享

PHP中文网
发布: 2017-07-15 17:27:54
原创
1514人浏览过

里面有两个方法,分别是用内置字体和自定义字体生成验证码。

<?php
/**
* 验证码生成类
* @example
* $pic = new uImage();
* $code = $pic->getVerifyCode(); 
* header("Content-type:image/png"); 
* $pic->captchaFromFont($font='RAVIE.TTF'); or $pic->captcha();
*/
class uImage {
    /**
    * 验证码字符
    * @access protected
    */
    protected $code;
 
    /**
    * 生成图片验证码,直接输出的是图片,字体大小是内置字体,最大是5
    * @access public
    * @param int $width 验证码图片宽度
    * @param int $height 验证码图片的高度
    * @param int $snow 背景雪花的数量
    * @param int $line 干扰线的条数
    */
    public function captcha($width=100, $height=30, $snow=80, $line=3) {
        $pic = imagecreatetruecolor($width, $height);
        $backageColor = imagecolorallocate($pic, 0xFF, 0xFF, 0xFF);
        imagefill($pic, 0, 0, $backageColor);
        //打雪花
        for($i=0; $i<=$snow;$i++) {
            $color = imagecolorallocate($pic, mt_rand(150, 230), mt_rand(150, 230), mt_rand(150, 230));
            imagechar($pic, 1, mt_rand(0, $width), mt_rand(0, $height), "*", $color);
            imagecolordeallocate ($pic, $color);
        }
        //画干扰线
        for($i=0; $i<=$line; $i++) {
            $x1 = mt_rand(2, $width * 0.2);
            $x2 = mt_rand($width * 0.8, $width - 2);
            $y1 = mt_rand(2, $height - 2);
            $y2 = mt_rand(2, $height - 2);
            $color = imagecolorallocate($pic, mt_rand(130, 250), mt_rand(130, 250), mt_rand(130, 250));
            imageline($pic, $x1, $y1, $x2, $y2, $color);
            imagecolordeallocate ($pic, $color);
        }
        //画字符
        $code = $this->code;
        $eachW = $width / strlen($code); //图片依据字符个数分配等份数
        $fontWidth = imagefontwidth(5); //取得字体宽度
        $fontHeight = imagefontheight(5); //取得字体高度
        for($i=0; $i<strlen($code);$i++) {
            $color = imagecolorallocate($pic, mt_rand(30, 155), mt_rand(30, 155), mt_rand(30, 150));
            $x = mt_rand($eachW * $i, $eachW * ($i+1)-$fontWidth);
            $y = mt_rand(3, $height-$fontHeight);
            imagechar($pic, 5, $x, $y, $code{$i}, $color); //水平画字符
            imagecolordeallocate ($pic, $color);
        }
        //输出
        ob_start();
        ob_clean();
        imagepng($pic);
        imagedestroy($pic);
    }
 
    /**
    * 根据自定义字体生成验证码
    * @access public
    * @param string $font 字符文件, TrueType 字体文件,.ttf字体
    * @param int $fontWeight 字符大小
    * @param int $width 图片宽
    * @param int $height 图片高
    * @param int $snow 背景雪花个数
    * @param int $line 干扰线条数
    * @param int $padding 图片内边距
    */
    public function captchaFromFont($font, $fontWeight=16, $width=100, $height=30, $snow=80, $line=3, $padding=3){
        if(!isset($font)){
            return false;
        }
        $pic = imagecreatetruecolor($width, $height);
        $backageColor = imagecolorallocate($pic, 0xFF, 0xFF, 0xFF);
        imagefill($pic, 0, 0, $backageColor);
        imagecolordeallocate ($pic, $backageColor);
        //打雪花
        for($i=0; $i<=$snow;$i++) {
            $color = imagecolorallocate($pic, mt_rand(150, 230), mt_rand(150, 230), mt_rand(150, 230));
            imagechar($pic, 1, mt_rand(0, $width), mt_rand(0, $height), "*", $color);
            imagecolordeallocate ($pic, $color);
        }
        //画干扰线
        for($i=0; $i<=$line; $i++) {
            $x1 = mt_rand(2, $width * 0.2);
            $x2 = mt_rand($width * 0.8, $width - 2);
            $y1 = mt_rand(2, $height - 2);
            $y2 = mt_rand(2, $height - 2);
            $color = imagecolorallocate($pic, mt_rand(130, 250), mt_rand(130, 250), mt_rand(130, 250));
            imageline($pic, $x1, $y1, $x2, $y2, $color);
            imagecolordeallocate ($pic, $color);
        }
        //画字符
        $code = $this->code;
        $eachW = $width / strlen($code); //图片依据字符个数分配等份数
        $codeArray = str_split($code);
        for($i=0; $i<count($codeArray); $i++){
            //取得字符宽高
            $fontbox = imagettfbbox($fontWeight, 0, $font, $codeArray[$i]);
            $fontWidth = $fontbox[2] - $fontbox[0];
            $fontHeight = $fontbox[1] - $fontbox[7];
 
            $color = imagecolorallocate($pic, mt_rand(30, 155), mt_rand(30, 155), mt_rand(30, 150)); //字符颜色
            $angle = mt_rand(-20, 20); //字符角度
            if($i==0){
                $start = $eachW * $i+$padding;
                $end = $eachW * ($i+1)-$fontWidth;
            }
            elseif($i == count($codeArray)){
                $start = $eachW * $i;
                $end = $eachW * ($i+1)-$fontWidth-$padding;
            }
            else{
                $start = $eachW * $i;
                $end = $eachW * ($i+1)-$fontWidth-$padding;
            }
            $x = $start < $end ? mt_rand($start, $end) : $start;
            $y = ($fontHeight+$padding) > $height ? $padding : mt_rand($fontHeight+$padding, $height-$padding); 
            imagettftext($pic, $fontWeight, $angle, $x, $y, $color, $font, $codeArray[$i]); //用 TrueType 字体向图像写入文本 
            imagecolordeallocate ($pic, $color);
        }
 
        //输出
        ob_start();
        ob_clean();
        imagepng($pic);
        imagedestroy($pic);
    }
 
    /**
    * 获取验证码
    * @access public
    * @param int $len 验证码字符的长度
    * @return strint 生成的验证码字符
    */
    public function getVerifyCode($len=4){
        if(!isset($this->code)){
            $this->code = $this->getCode($len);
        }
        return $this->code;
    }
 
    /**
    * 生成验证码
    * @access protected
    * @param int $len 验证码字符的长度
    * @return strint 生成的验证码字符
    */
    protected function getCode($len) {
        $str = "23456789abcdefghijklmnqrstuvwxyz";
        $code = "";
        for($i=0; $i<$len; $i++) {
            $code .= $str{mt_rand(0, strlen($str)-1)};
        }
        $this->code = $code;
        return $code;
    }
}
登录后复制

 以上就是PHP验证码类的内容,更多相关内容请关注PHP中文网(www.php.cn)!

代码小浣熊
代码小浣熊

代码小浣熊是基于商汤大语言模型的软件智能研发助手,覆盖软件需求分析、架构设计、代码编写、软件测试等环节

代码小浣熊 51
查看详情 代码小浣熊
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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