登录  /  注册

简述php设计验证码的过程

藏色散人
发布: 2019-11-01 09:42:01
原创
1924人浏览过

简述php设计验证码的过程

简述php设计验证码的过程

php使用GD库生成验证码

一、绘图步骤:

1.创建画布,分配颜色,使用以下两个函数(可以在php手册GD库函数中找到):

imagecreatetruecolor()

imagecolorallocate()

2.绘画过程:

imagefill()

imagettftext()

imagesetpixel()

imageline()

3.输出图像:

header(“Content-Type:image/png”);

imagepng();

imagejpeg();

4.销毁图片(释放内存)

imagedestroy();

二、绘图具体步骤

生成验证码字符串

private function getCode(){
        $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $t='';
        for($i=0;$i<$this->m;$i++){
            $t.=$str[rand(0,$this->type[&#39;letter&#39;])];
        }
        return $t;
    }
登录后复制

画出验证码

function drawCode(){
        $code=$this->getCode();//获取验证码字符串
        imagefill($this->im, 0, 0, $this->bg);
        for ($i=0; $i <200 ; $i++) { 
            imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255));
        }
        imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255));
        $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200));
        for($i=0;$i<$this->m;$i++){
            imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] );
        }
    }
登录后复制

输出图像

function printImage(){
        $this->drawCode();
        header("Content-Type:image/jpeg");
        imagepng($this->im);
        $this->destroy();
    }
    function destroy(){
        imagedestroy($this->im);
    }
登录后复制

三、代码演示

 9,'letter' => 61 );
    function __construct($width,$height,$m)
    {
        $this->width=$width;
        $this->height=$height;
        $this->m=$m;
        $this->im=imagecreatetruecolor($this->width, $this->height);
        $this->bg=imagecolorallocate($this->im, 220, 220, 220);
    }
    private function getCode(){
        $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $t=&#39;&#39;;
        for($i=0;$i<$this->m;$i++){
            $t.=$str[rand(0,$this->type[&#39;letter&#39;])];
        }
        return $t;
    }
    function drawCode(){
        $code=$this->getCode();//获取验证码字符串
        imagefill($this->im, 0, 0, $this->bg);
        //加干扰点
        for ($i=0; $i <200 ; $i++) { 
            imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255));
        }
        //加干扰线
        imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255));
        $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200));
        for($i=0;$i<$this->m;$i++){
            imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] );
        }
    }
    function printImage(){
        $this->drawCode();
        header("Content-Type:image/jpeg");
        imagepng($this->im);
        $this->destroy();
    }
    function destroy(){
        imagedestroy($this->im);
    }
}
function unit_test(){
    $obj=new Image(20*4,30,4);
    $obj->printImage();
}
unit_test();
?>
登录后复制

更多PHP相关知识,请访问PHP中文网

以上就是简述php设计验证码的过程的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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