Lors de l'écriture d'un code de vérification en PHP, l'éditeur de code en ligne peut l'afficher, mais lorsqu'il est placé sur le site Web local, le code est tronqué ?
phpcn_u53259
phpcn_u53259 2017-11-06 13:50:30
0
9
1174

<?php

check_code();

//Générer aléatoirement des caractères ou des chiffres pour le code de vérification

function check_code($width=100, $height=50,$num=4, $type = 'jpeg')

{

$i=imagecreate($width,$height);

$string='';

for($j=0;$j<$num;$j++)

{ " $ascii=mt_rand(48,57);

break;

cas 1:

$ascii=mt_rand(65,90); break;

}

} $string.=sprintf('%c ' ,$ ascii); //décodage des caractères ascii

} }

//Générer la couleur de fond du code de vérification (randbg() : appel de fonction)

imagefilledrectangle($i,0,0,$width, $height,randbg ($i));

//Générer une interférence aléatoire (randpix() : appel de fonction)

for($j=0;$j<50;$j++)

{

imagesetpixel ($i,mt_rand(0,$width),mt_rand(0,$height) ,randpix($i));

}


//Écrire

for($j=0 ;$j< $num;$j++)

{

$x=floor($width/$num)*$j+2;

$y=mt_rand(3,$height-15);

imagechar ($ i,5,$x,$y,$string[$j],randpix($i));

}


//Format d'image

$fuc='image'. $type ;

$have='cotent-type:image'.$type;

if(function_exists($fuc))

{

header($have);

$fuc($i);

}

else

{


echo 'Type d'image non pris en charge';

}

imagedestroy($i);

return $string;

}

//Module de fonction de couleur d'arrière-plan

function randbg($i)

                                                                                           return imagecolorallocate($i, m t_rand(135,255),,mt_rand(135,255),mt_rand(135,255));

//Module de fonction d'élément d'interférence ou de couleur de caractère

function randpix($i)

{

        return imagecolorallocate($i,mt_rand(0,135),mt_rand(0,135),mt_rand(0,135));

                                                                  }

?>

phpcn_u53259
phpcn_u53259

répondre à tous(2)
路过
这是个验证码类,你可以参考一下
<?php

class Captcha
{
    private $width;
    private $height;
    private $codeNum;
    private $code;
    private $im;

    function __construct($width=80, $height=20, $codeNum=4)
    {
        $this->width = $width;
        $this->height = $height;
        $this->codeNum = $codeNum;
    }

    function showImg()
    {
        //创建图片
        $this->createImg();
        //设置干扰元素
        $this->setDisturb();
        //设置验证码
        $this->setCaptcha();
        //输出图片
        $this->outputImg();
    }

    function getCaptcha()
    {
        return $this->code;
    }

    private function createImg()
    {
        $this->im = imagecreatetruecolor($this->width, $this->height);
        $bgColor = imagecolorallocate($this->im, 0, 0, 0);
        imagefill($this->im, 0, 0, $bgColor);
    }

    private function setDisturb()
    {
        $area = ($this->width * $this->height) / 20;
        $disturbNum = ($area > 250) ? 250 : $area;
        //加入点干扰
        for ($i = 0; $i < $disturbNum; $i++) {
            $color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
            imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
        }
        //加入弧线
        for ($i = 0; $i <= 5; $i++) {
            $color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
            imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
        }
    }

    private function createCode()
    {
        $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";

        for ($i = 0; $i < $this->codeNum; $i++) {
            $this->code .= $str{rand(0, strlen($str) - 1)};
        }
    }

    private function setCaptcha()
    {
        $this->createCode();

        for ($i = 0; $i < $this->codeNum; $i++) {
            $color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
            $size = rand(floor($this->height / 5), floor($this->height / 3));
            $x = floor($this->width / $this->codeNum) * $i + 5;
            $y = rand(0, $this->height - 20);
            imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
        }
    }

    private function outputImg()
    {
        if (imagetypes() & IMG_JPG) {
            header('Content-type:image/jpeg');
            imagejpeg($this->im);
        } elseif (imagetypes() & IMG_GIF) {
            header('Content-type: image/gif');
            imagegif($this->im);
        } elseif (imagetype() & IMG_PNG) {
            header('Content-type: image/png');
            imagepng($this->im);
        } else {
            die("Don't support image type!");
        }
    }

}

// 这样调用 新建文件
<?php
require_once 'captcha.class.php';

$captcha = new Captcha(80,30,4);

$captcha->showImg();


路过

Publiez le message d'erreur. Qui a le temps de lire votre code ? ? ?

  • répondre C'est juste du code tronqué et aucune erreur n'est signalée. Il devrait y avoir une erreur dans //format image//.
    phpcn_u53259 auteur 2017-11-07 17:38:05
  • répondre Je l'ai essayé il y a longtemps, mais c'est toujours tronqué.
    phpcn_u53259 auteur 2017-11-08 09:42:13
  • répondre nom de fichier? Il n'y a aucune instruction pour écrire le nom du fichier ici.
    phpcn_u53259 auteur 2017-11-08 10:29:07
  • répondre D'accord, merci~
    phpcn_u53259 auteur 2017-11-08 14:05:56
  • répondre Oh, vous pouvez ajouter un en-tête pour déclarer utf-8 et l'essayer.
    路过 auteur 2017-11-08 08:19:59
  • répondre Vérifiez si le nom de votre fichier est erroné
    路过 auteur 2017-11-08 10:21:41
  • répondre Il y a un problème avec votre code
    路过 auteur 2017-11-08 13:45:45
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!