PHP实现验证码效果

PHP实现验证码效果

创建画布,创建随机颜色

header("content-type:image/jpeg");
ob_clean();
$img=imagecreatetruecolor(120,50);
$red=mt_rand(10,255);
$green=mt_rand(10,255);
$blue=mt_rand(10,255);
$suiji=imagecolorallocate($img,$red,$green,$blue);
$bai=imagecolorallocate($img,255,255,255);
imagefill($img,0,0,$suiji);

在画布中写入验证码

$width=0;
$str="1234567890qwertyuiopasdfghjklzxcvbnm";
for($z=1;$z<=4;$z++){
    $red=mt_rand(10,255);
    $green=mt_rand(10,255);
    $blue=mt_rand(10,255);
    $suiji=imagecolorallocate($img,$red,$green,$blue);
    $str=str_shuffle($str);
    $widthz=mt_rand(10,30);
    $heightz=mt_rand(20,40);
    imagettftext($img,20,true,$width+=$widthz,$heightz,$suiji,'Lobster-Regular.ttf',$str[0]);
}

在画布中写入随机颜色、随机位置、随机数量的点

for ($d=1;$d<=100;$d++){
    $red=mt_rand(10,255);
    $green=mt_rand(10,255);
    $blue=mt_rand(10,255);
    $suiji=imagecolorallocate($img,$red,$green,$blue);
    $x=mt_rand(0,120);
    $y=mt_rand(0,50);
    imagesetpixel($img,$x,$y,$suiji);
}

在画布中写入随机颜色、随机位置、随机数量的线

for ($x=0;$x<=5;$x++){
    $red=mt_rand(10,255);
    $green=mt_rand(10,255);
    $blue=mt_rand(10,255);
    $suiji=imagecolorallocate($img,$red,$green,$blue);
    $x1=mt_rand(0,120);
    $x2=mt_rand(0,120);
    $y1=mt_rand(0,50);
    $y2=mt_rand(0,50);
    imageline($img,$x1,$y1,$x2,$y2,$suiji);
}
imagejpeg($img);
imagedestroy($img);

猜你喜欢

转载自blog.csdn.net/weixin_43708754/article/details/86660222