php验证码

简单的验证码可以这么写~

<?php
$img = imagecreatetruecolor(100, 40);
$black = imagecolorallocate($img, 0x00, 0x00, 0x00);
$green = imagecolorallocate($img, 0x00, 0xFF, 0x00);
$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
imagefill($img,0,0,$white);
//生成随机的验证码
$code = '';
for($i = 0; $i < 3; $i++) {
    $code .= rand(0, 9);//数字
    $code .= dechex(rand(0, 15));//字母
}

imagestring($img, 8, 10, 10, $code, $black);
//加入噪点干扰
for($i=0;$i<50;$i++) {
  imagesetpixel($img, rand(0, 100) , rand(0, 100) , $black); 
  imagesetpixel($img, rand(0, 100) , rand(0, 100) , $green);
}
//加入线条干扰
for($n=0;$n<2;$n++){

    imageline($img,0,rand(0, 40),100,rand(0, 40),$black);

    imageline($img,0,rand(0, 40),100,rand(0, 40),$green);

    imageline($img,0,rand(0, 40),100,rand(0, 40),$white);

}
//输出验证码
header("content-type: image/png");
imagepng($img);
imagedestroy($img);
原文地址:https://www.cnblogs.com/ymli/p/6829647.html