<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>images</title>
</head>
<body>
<?php
// 执行函数
verify();
// 创建函数
function verify($w = 100,$h = 60,$num = 4,$type = 1){
$img = imagecreate($w,$h);
imagecolorallocate($img, mt_rand(180,255),mt_rand(180,255),mt_rand(180,255));
$str = "";
switch($type){
case '1':
$str = '0123456789';
$str = str_shuffle($str);
$str = substr($str,0,$num);
break;
case '2':
$arr = range('a','z');
shuffle($arr);
$arr = array_slice($arr,0,$num);
$str = join('',$arr);
break;
case '3':
$str = "0123456789fsjprdahbucwntzeqgklxyiovmZVHTGOQPCFIYWAKLXMEBNRUSJD";
$str = substr(str_shuffle($str),0,$num);
break;
}
// 画数字和线
for($i = 0;$i < $num; $i++){
$x = $w/$num * $i +5;
$y = mt_rand(10,$h-20);
imagechar($img,5,$x,$y,$str[$i],deepcolor($img));
imagearc($img,mt_rand(10,$w),mt_rand(10,$h),mt_rand(10,$w),mt_rand(10,$h),mt_rand(0,10),mt_rand(10,270),deepcolor($img));
}
// 画点
for($i = 0;$i < 20; $i++){
imagesetpixel ( $img ,mt_rand(0,$w) , mt_rand(0,$h) , lightcolor($img) );
}
// 清空输出缓冲区
ob_clean();
// 设置输出图片格式
header("Content-type:image/png");
imagepng($img);
imagedestroy($img);
};
// 深颜色函数
function deepcolor($img){
return imagecolorallocate ($img,mt_rand(0,130),mt_rand(0,130),mt_rand(0,130));
};
// 浅颜色函数
function lightcolor($img){
return imagecolorallocate($img,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
};
?>
</body>
</html>
php gd库写一个简单的验证码
最新推荐文章于 2021-03-29 00:21:49 发布