/**
*生成验证码
* @param int $wide 宽度
* @param int $high 高度
* @param int $num 位数
*/
function code($wide=80,$high=60,$num=4){
//创建画布
$img = imagecreatetruecolor($wide, $high)
//设置画布颜色并渲染
$color = imagecolorallocate($img, rand(128,255), rand(128,255), rand(128,255))
imagefill($img,0,0,$color)
//设置干扰元素
for ($i=0
$x = mt_rand(0,$wide)
$y = mt_rand(0,$high)
$color = imagecolorallocate($img, rand(128,255), rand(128,255), rand(128,255))
imagesetpixel($img,$x,$y,$color)
if($i%6==0){
$endX = mt_rand(0,$wide)
$endY = mt_rand(0,$high)
imageline($img,$x,$y,$endX,$endY,$color)
}
}
//生成随机code
$string = '123456789ABCDEFG'
$len = strlen($string)-1
$code = ''
for ($i=0
$n = mt_rand(0,$len)
$code.=$string[$n]
}
//调试位置并写入
$fontsize = ceil(($wide-20)/$num)
$highY = ceil($high/2+$fontsize/2)
imagettftext($img,$fontsize,0,10,$highY,$color,'E:\phpstudy_pro\WWW\day\1.ttf',$code)
//告诉浏览器这是个图片
header("content-type: image/png")
//保存图片到filename
imagepng($img)
//释放资源
imagedestroy($img)
}
code()