Maison  >  Article  >  développement back-end  >  Décrire brièvement le processus de conception du code de vérification en PHP

Décrire brièvement le processus de conception du code de vérification en PHP

藏色散人
藏色散人original
2019-11-01 09:42:012070parcourir

Décrire brièvement le processus de conception du code de vérification en PHP

Décrivez brièvement le processus de conception du code de vérification en PHP

php utilise la bibliothèque GD pour générer le code de vérification

1. Étapes de dessin :

1. Créez un canevas, attribuez des couleurs et utilisez les deux fonctions suivantes (disponibles dans la fonction de la bibliothèque GD dans le manuel PHP) :

imagecreatetruecolor()

imagecolorallocate()

2. Processus de peinture :

imagefill()

imagettftext()

imagesetpixel( )

imageline ()

3. Image de sortie :

header("Content-Type:image/png");

imagepng();

imagejpeg( ; >

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

Dessinez le code de vérification

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

Image de sortie

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

3. Démonstration de code

 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();
?>

Pour plus de connaissances liées à PHP , veuillez visiter le

site Web PHP chinois

 !

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:principe de la session phpArticle suivant:principe de la session php