php简单生成验证码图片

<?php 
$i=imagecreatetruecolor(100, 30);
$iColor=imagecolorallocate($i, 240, 240, 240);
imagefill($i, 0, 0, $iColor);
$str="1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPSDFGHJKLZXCVBNM";
$nStr=str_shuffle($str);
$fourStr=substr($nStr, 0,4);

for ($j=0; $j < strlen($fourStr); $j++) { 

    $s=substr($fourStr, $j,1);
    //随机颜色,尽量不要太浅(和底色白色有差别)
    $fColor=imagecolorallocate($i, mt_rand(0,150), mt_rand(0,150), mt_rand(0,150));
    //随机倾斜
    $angle=mt_rand(0,45);

    $x=8+$j*25;
    $y=25;
    //SIMYOU.TTF字体文件可以在C:WINDOWSFonts下找到,多个字体也可以添加到一个数组中,然后随机提取
    imagefttext($i, 20, $angle, $x, $y, $fColor, "SIMYOU.TTF", $s);
}

//添加干扰线 (4条干扰线)
for ($j=0; $j < 4; $j++) { 
    $lColor=imagecolorallocate($i, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
    $x1=mt_rand(0,100);
    $y1=mt_rand(0,30);
    $x2=mt_rand(0,100);
    $y2=mt_rand(0,30);

    imageline($i, $x1, $y1, $x2, $y2, $lColor);
}


//加干扰点(200个干扰点)
for ($j=0; $j < 200; $j++) { 
    $pColor=imagecolorallocate($i, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
    $x=mt_rand(0,100);
    $y=mt_rand(0,30);
    imagesetpixel($i, $x, $y, $pColor);
}
header("content-type:image/png");
imagepng($i);

?>
原文地址:https://www.cnblogs.com/xuxyblog/p/4233413.html