PHP 生成验证码 (二)

 1 <?php 
 2     //验证码制作
 3   
 4     session_start();
 5     //生成验证码图片
 6     Header("Content-type: image/PNG");
 7     $im = imagecreate(44,18); // 画一张指定宽高的图片
 8     $back = ImageColorAllocate($im, 245,245,245); // 定义背景颜色
 9     imagefill($im,0,0,$back); //把背景颜色填充到刚刚画出来的图片中
10     $vcodes = "";
11     srand((double)microtime()*1000000);
12     $srcstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
13     //生成4位数字
14     for($i=0;$i<4;$i++) {
15         $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // 生成随机颜色
16         $authnum = $srcstr[mt_rand(0,35)];
17         $vcodes.= $authnum;
18         imagestring($im, 5, 2 + $i * 10, 1, $authnum, $font);
19     }
20     $_SESSION['VCODE'] = $vcodes;
21 
22     for($i=0;$i<100;$i++) {//加入干扰象素   
23         $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
24         imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); // 画像素点函数
25     }
26     ImagePNG($im);
27     ImageDestroy($im); 
28 ?> 

 

posted on 2014-11-17 10:16  嘘寒问暖  阅读(105)  评论(0编辑  收藏  举报

导航