生成图片验证码

https://git.oschina.net/wddm/codes/ph5olvjz7cksqfrb2t98y97

//声明要创建的图片格式
header("Content-Type:image/png");
//创建图层/创建一张验证码图片(宽,高)
$img = imagecreatetruecolor(120, 40);
//创建背景颜色
$bjcolor = imagecolorallocate($img, rand(230, 255), rand(230, 255), rand(230, 255));
//创建干扰线条颜色
$lineColor = imagecolorallocate($img, rand(180, 230), rand(180, 230), rand(180, 230));
//填充背景颜色
//($image, $x1, $y1, $x2, $y2, $color)其左上角坐标为 x1,y1 右下角坐标为 x2,y2 0, 0 是图像的最左上角
imagefilledrectangle($img, 0, 0, 120, 40, $bjcolor);
//添加文字
$str = "234567892345678923456789zxcvbnmasdfghjkqwertyuipZXCVBNMASDFGHJKLQWERTYUIP";
$yzm="";
for ($a = 1; $a < 5; $a++) {
    //创建字体颜色
    $zicolor = imagecolorallocate($img, rand(1, 190), rand(1, 190), rand(1, 190));
    //切割任意4个字
    $text = substr($str, mt_rand(0, strlen($str) - 1), 1);
     $yzm .= $text;
    //填充字体颜色
    //($image, $size字体大小, $angle角度, $i x坐标, $a y坐标, $color, $str字体样式, $text几个字)
    imagettftext($img, 30, mt_rand(-25, 25), ($a * 20), 32, $zicolor, "AdobeHeitiStd-Regular.otf", $text);
}
//添加干扰线条
for ($i = 0; $i < 15; $i++) {
    //填充线条颜色
    imageline($img, rand(1, 99), rand(1, 39), rand(1, 99), rand(1, 39), $lineColor);
}
//输出图像
imagepng($img);
//销毁图像资源
imagedestroy($img);
//把验证码放到session里
session_start();
$_SESSION['yzm'] = strtolower($yzm);

原文地址:https://www.cnblogs.com/jhy-ocean/p/7477363.html