Maison  >  Article  >  développement back-end  >  Comment implémenter un code de vérification de nombres et de lettres aléatoires en php

Comment implémenter un code de vérification de nombres et de lettres aléatoires en php

不言
不言original
2018-07-13 09:03:582369parcourir

PHP implémente un code de vérification de chiffres et de lettres aléatoires

Vous pouvez personnaliser la taille, la quantité, les éléments d'interférence, etc. du texte du code de vérification généré, ou vous pouvez le personnaliser. Définit la police du texte de validation. . .

Sans plus attendre, passons directement au code :

<?php
Class Captcha{
        private $_fontfile=&#39;&#39;;
        private $_size=36;
        private $_width=200;
        private $_height=100;
        private $_length=4;
        private $_image=null;
        private $_snow=0;
        private $_pixel=0;
        private $_line=0;
    public function __construct($config=array()){
        if(is_array($config)&&count($config)>0){
            if(isset($config[&#39;fontfile&#39;])&&is_file($config[&#39;fontfile&#39;])&&is_readable($config[&#39;fontfile&#39;])){
                $this->_fontfile=$config[&#39;fontfile&#39;];
            }else{
                return false;
            }
            if(isset($config[&#39;size&#39;])&&$config[&#39;size&#39;]>0){
                $this->_size=(int)$config[&#39;size&#39;];
            }
            if(isset($config[&#39;width&#39;])&&$config[&#39;width&#39;]>0){
                $this->_width=(int)$config[&#39;width&#39;];
            }
            if(isset($config[&#39;height&#39;])&&$config[&#39;height&#39;]>0){
                $this->_height=(int)$config[&#39;height&#39;];
            }
            if(isset($config[&#39;length&#39;])&&$config[&#39;length&#39;]>0){
                $this->_length=(int)$config[&#39;length&#39;];
            }
            if(isset($config[&#39;snow&#39;])&&$config[&#39;snow&#39;]>0){
                $this->_snow=(int)$config[&#39;snow&#39;];
            }
            if(isset($config[&#39;pixel&#39;])&&$config[&#39;pixel&#39;]>0){
                $this->_pixel=(int)$config[&#39;pixel&#39;];
            }
            if(isset($config[&#39;line&#39;])&&$config[&#39;line&#39;]>0){
                $this->_line=(int)$config[&#39;line&#39;];
            }
            $this->_image=imagecreatetruecolor($this->_width,$this->_height);
            return $this->_image;

         }
         else{
            return false;
        }
    }
    public function getCaptcha(){
        $white=imagecolorallocate($this->_image,255,255,255);
        imagefilledrectangle($this->_image,0,0,$this->_width,$this->_height,$white);
        $str=$this->_generateStr($this->_length);
        if(false===$str){
            return false;
        }
        $fontfile=$this->_fontfile;
        for($i=0;$i<$this->_length;$i++){
            $size=$this->_size;
            $angle=mt_rand(-30,30);
            $x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10);
            $y=ceil($this->_height/1.5);
            $color=$this->_getRandColor();
            //针对中文字符截取
            //$text=mb_substr($str,$i,1,&#39;utf-8&#39;);
            $text=$str{$i};
            imagettftext($this->_image, $size, $angle, $x, $y, $color, $fontfile, $text);
        }
        if($this->_snow){
            $this->_getSnow();

        }else{
            if($this->_pixel){
                $this->_getPixel();

            }
            if($this->_line){
                $this->_getLine();
            }
        }
        header(&#39;content-type:image/png&#39;);
        imagepng($this->_image);
        imagedestroy($this->_image);
        return strtolower($str);
    }
    private function _getSnow(){
        for($i=1;$i<=$this->_snow;$i++){
            imagestring($this->_image,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),&#39;*&#39;,$this->_getRandColor());
        }
    }
    private function _getPixel(){
        for($i=1;$i<=$this->_pixel;$i++){
            imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
        }
    }
    private function _getLine(){
        for($i=1;$i<=$this->_line;$i++){
            imageline($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());

        }
    }
    private function _generateStr($length=4){
        if($length<1 || $length>30){
            return false;
        }
        $chars=array(
            &#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;,&#39;h&#39;,&#39;k&#39;,&#39;m&#39;,&#39;n&#39;,&#39;p&#39;,&#39;x&#39;,&#39;y&#39;,&#39;z&#39;,
            &#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;,&#39;E&#39;,&#39;F&#39;,&#39;G&#39;,&#39;H&#39;,&#39;K&#39;,&#39;M&#39;,&#39;N&#39;,&#39;P&#39;,&#39;X&#39;,&#39;Y&#39;,&#39;Z&#39;,
            1,2,3,4,5,6,7,8,9
            );
        $str=join(&#39;&#39;,array_rand(array_flip($chars),$length));
        return $str;
    }
    private function _getRandColor(){
        return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    }
}
?>

2. testCaptcha.php

<?php
require_once &#39;classgd.class.php&#39;;
$config=array(
&#39;fontfile&#39;=>&#39;fonts/simfang.ttf&#39;,   //引入字体文件
//&#39;snow&#39;=>50,
&#39;pixel&#39;=>100,
&#39;line&#39;=>10
    );
$captcha=new Captcha($config);
$captcha->getCaptcha();
?>

Ce qui précède représente l'intégralité du contenu de cet article. J'espère qu'il sera utile à l'étude de chacun. Pour plus de contenu connexe, veuillez faire attention au site Web PHP chinois !

Recommandations associées :

Comment réécrire le mécanisme de stockage de session en php

Comment parcourir les dossiers pour faire passer des images php Nom du répertoire et nom du fichier

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