/*
函数调用:$code=vcode(100,40,30,4);
返回值是$string,是生成的验证码图片;
参数说明:
$width:图片的宽,
$height:图片的高,
$fontSize:字体大小,
$countElement:验证码个数,
$countPixel:小点的数目,
$countLine:线段数目,
*/
function vcode($width=120,$height=40,$fontSize=30,$countElement=5,$countPixel=100,$countLine=4){
header('Content-type:image/jpeg');
//$width=120;
//$height=40;
$element=array(
'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t', 'u','v','w','x');
//随机生成个验证码
$string='';
for($i=0;$i<$countElement;$i++){
$string.=$element[rand(0,count($element)-1)];
}
//$string=$element[rand(0,count($element)-1)];
$img= imagecreatetruecolor($width,$height);
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));//分配颜色
$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(180,255));//分配颜色
$colorSpolit=imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,255));//分配颜色
$colorText=imagecolorallocate($img,rand(10,100),rand(10,50),rand(10,100));//分配颜色
//填充背景
imagefill($img,0,0,$colorBg);
//绘制一个矩形
imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);
//画小点
for($i=0;$i<$countPixel;$i++){
imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),$colorSpolit);
};
///画线段
for($j=0;$j<$countLine;$j++){
imageline($img,rand(0,$width/2),rand(0,$height/2),rand($width/2,$width),rand($height/2,$height),$colorSpolit);
}
//字体向图像写入文本
imagettftext($img,$fontSize,rand(-5,5),rand(5,25),rand(25,35),$colorText,'fonts/ManyGifts.ttf',$string);
imagejpeg($img);
imagedestroy($img);
return $string;
}