1 <?php
2
3 $type=1;
4 $len=4;
5 header("content-type:image/png");
6 //测试函数
7 newCode();
8
9 function newCode($type=1,$len=4){
10
11 //验证码内容
12 $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
13
14 if($type==1){
15 $m=9;
16 }elseif($type==2){
17 $m=35;
18 }else{
19 $m=61;
20 }
21 $code="";
22 for($i=0;$i<$len;$i++){
23 $code .= $str[rand(0,$m)]; //随机获取验证码
24 }
25 //创建画布
26 $im=imagecreatetruecolor(50*$len,50);
27 //创建画笔和背景
28 $bg=imagecolorallocate($im,230,230,230);
29 $hb=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
30 //背景填充
31 imagefill($im,0,0,$bg);
32 //添加随机颜色的噪点
33 for($i=0;$i<50*$len;$i++){
34 $hb=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
35 imagesetpixel($im,rand(0,50*$len),rand(0,50),$hb);
36 }
37 //添加随机干扰线
38 for($i=0;$i<$len;$i++){
39 $hb=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
40 imageline($im,rand(0,50*$len),rand(0,50),rand(0,50*$len),rand(0,50),$hb);
41 }
42 //添加验证码
43 for($i=0;$i<$len;$i++){
44 $hb=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
45 imagettftext($im,40,rand(-30,30),20+40*$i,44,$hb,"msyh.ttf",$code[$i]);
46 }
47 //输出验证码
48 imagepng($im);
49 //释放资源
50 imagedestroy($im);
51
52
53 }
54
55
56 ?>