简述php设计验证码的过程-PHP问题

资源魔 22 0

简述php设计验证码的进程

php应用GD库天生验证码

1、画图步骤:

1.创立画布,调配颜色,应用如下两个函数(能够正在php手册GD库函数中找到):

imagecreatetruecolor()

imagecolorallocate()

2.绘画进程:

imagefill()

imagettftext()

imagesetpixel()

imageline()

3.输入图象:

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

imagepng();

imagejpeg();

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

imagedestroy();

2、画图详细步骤

天生验证码字符串

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);
    }

3、代码演示

<?php
/**
* 验证码绘图类
*/
class Image 
{
    private $width;
    private $height;
    private $im;
    private $bg;
    private $m;
    private $type=array('num' => 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中文网!

以上就是简述php设计验证码的进程的具体内容,更多请存眷资源魔其它相干文章!

标签: php 验证码 php教程 php故障解决 php使用问题

抱歉,评论功能暂时关闭!