function vcode($width=120,$height=40,$fontSize=32,$num_letter=4,$num_spot=100,$num_line=5){
//验证码实现
header('Content-type:image/jpeg');
//创建图像
$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(200,255));
//背景小点颜色
$colorDian = imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200));
//验证码字符颜色
$colorString = imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
//区域填充
imagefill($img, 0, 0, $colorBg);
//画一个矩形
imagerectangle($img, 0,0, $width-1, $height-1, $colorBorder);
//画多个小点
for($i=0;$i<$num_spot;$i++){
imagesetpixel($img, rand(0,$width-1), rand(0,$height-1),$colorDian );
}
//画一条线段
for($y=0;$y<$num_line;$y++){
imageline($img, rand(0,$width/4), rand(0,$height-1), rand($width/4*3,$width-1), rand(0,$height-1),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
};
//验证码字符串数组
$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','y','z');
$element_ = '';
for($i=0;$i<$num_letter;$i++){
$element_ =$element_ . $element[rand(0,count($element)-1)];
}
//验证码字符串
/*
不用这个函数
imagestring($img, 5, 0, 0, 'abcd', $colorString);
*/
//图像写入文本函数
imagettftext($img, $fontSize, rand(-5,5), 5, rand(30,39), $colorString, 'font/z.otf', $element_);
//输出图像
imagejpeg($img);
//释放图像内存
imagedestroy($img);
return $element_;
}
调用示例:vcode();