PHP 绘制图形验证码

 


 

// 设定验证码中可能出现的文字
$str = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$result = '';

// 取随机4位做验证码
for($i = 0; $i < 4; $i ++)
{
    $result.= $str[mt_rand(0, strlen($str) - 1)];
}

// 创建画布,设置背景色
$img = imagecreatetruecolor(100, 30);
$background = imagecolorallocate($img, 255, 255, 255);
imagefill($img,0,0,$background);

// 填充验证码文字
for($i = 0; $i < strlen($result); $i ++)
{
    $te = imagecolorallocate($img, mt_rand(0, 150),mt_rand(0, 150),mt_rand(0, 150));
    imagettftext($img, 26, mt_rand(2, 10), $i * 21 + 10,26, $te, "./CHILLER.TTF", $result[$i]);
}

// 绘制干扰线条
for($i = 0; $i < 3; $i ++)
{
    $te = imagecolorallocate($img, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
    imageline($img, 0, mt_rand(0, 30), mt_rand(40,100), mt_rand(0, 50), $te);
}

// 绘制干扰噪点
for($i = 0; $i < 200; $i ++)
{
    $t = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    imagesetpixel($img, mt_rand(1, 100), mt_rand(1, 30), $t);
}

// 输出图片
header("Content-type: image/jpeg");
imagejpeg($img);
die();

 

posted @ 2022-08-02 10:33  何效名  阅读(85)  评论(0编辑  收藏  举报