php制作验证码的过程-成都快上网建站

php制作验证码的过程

本篇文章给大家分享的是有关php制作验证码的过程,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比金城江网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式金城江网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖金城江地区。费用合理售后完善,十多年实体公司更值得信赖。

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['letter'])];
        }
        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='';
        for($i=0;$i<$this->m;$i++){
            $t.=$str[rand(0,$this->type['letter'])];
        }
        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制作验证码的过程
分享地址:http://kswjz.com/article/jojhsh.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流