PHP生成随机验证码函数

/*
php生成验证码
*/

<?php
getCode(200,100);

function getCode($width,$height){
//$width---验证码图片的宽
//$height---验证码图片高
  $img=imagecreatetruecolor($width,$height);

  $black=imagecolorallocate($img,0,0,0);
  $white=imagecolorallocate($img,255,255,255);
  $blue=imagecolorallocate($img,0,0,255);
  $red=imagecolorallocate($img,255,0,0);
  $yellow=imagecolorallocate($img,255,255,0);
  $green=imagecolorallocate($img,0,255,0);
  imagefill($img,0,0,$white);

  for($i==0;$i<5;$i++)
  {
    imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,
    $width),mt_rand(0,$height),$blue);
    imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,
    $width),mt_rand(0,$height),$red);
    img_linethick($img,$yellow,$width,$height);
    img_linethick($img,$green,$width,$height);

  }

  for($i==0;$i<30;$i++)
  {
    imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$black);
    imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$red);
    imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$blue);
    imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$yellow);
    imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$green);
  }

  shuffle($arr=array_rand(array_flip(array_merge(range(a,z),range(0,9),range (A,Z))),4));
  imagestring($img,5,($width/3),($height/2),implode(' ',$arr),$green);
  header("content-type:image/png");
  imagepng($img);
  imagedestroy($img);
}


function img_linethick($img,$color,$width,$height)
{
//$img---画布资源
//$color---线条颜色
//$width---验证码宽
//$height---验证码高
  $ygomx1=mt_rand(4,$width);
  $ygomy1=mt_rand(4,$height);
  $ygomx11=$ygomx1;
  $ygomy11=$ygomy1+2;
  $ygomx2=mt_rand(4,$width);
  $ygomy2=mt_rand(4,$height);
  $ygomx22=$ygomx2;
  $ygomy22=$ygomy2+2;
  $point=array(
  $ygomx1,$ygomy1,
  $ygomx11,$ygomy11,
  $ygomx2,$ygomy2,
  $ygomx22,$ygomy22);
  imagefilledpolygon($img,$point,4,$color);
}
?>

原文地址:https://www.cnblogs.com/Mr-Wenyan/p/7273372.html