改进的某生成验证码图片的PHP代码

  在我的一本PHP教程的光盘里,附带了一段生成验证码图片的PHP代码,不过我感觉效果并不是很好。去网上搜了一下,有一些文章介绍的也是同样的代码。于是自己动手丰衣足食,正好借此机会动手实践一下刚学习的PHP……下面是原来的代码:

 1 <?php 
2 session_start();
3 //图片类型
4 $type = 'gif';
5 //图片的尺寸
6 $width= 40;
7 $height= 16;
8 header("Content-type: image/".$type);
9 srand((double)microtime()*1000000);
10 //生成字符的个数
11 $randval = randStr(4,"");
12 if($type!='gif' && function_exists('imagecreatetruecolor')){
13 $im = @imagecreatetruecolor($width,$height);
14 }else{
15 $im = @imagecreate($width,$height);
16 }
17 //色彩设置
18 $r = Array(225,211,255,223);
19 $g = Array(225,236,237,215);
20 $b = Array(225,236,166,125);
21
22 $key = rand(0,3);
23 $backColor = ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]); //背景色(随机)
24 $borderColor = ImageColorAllocate($im, 0, 0, 0); //边框色
25 $pointColor = ImageColorAllocate($im, 255, 170, 255); //点颜色
26
27 @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);//背景位置
28 @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor); //边框位置
29 $stringColor = ImageColorAllocate($im, 255,51,153);
30
31 for($i=0;$i<=100;$i++){
32 $pointX = rand(2,$width-2);
33 $pointY = rand(2,$height-2);
34 @imagesetpixel($im, $pointX, $pointY, $pointColor);
35 }
36
37 @imagestring($im, 3, 5, 1, $randval, $stringColor);
38 $ImageFun='Image'.$type;
39 $ImageFun($im);
40
41 @ImageDestroy($im);
42 //将显示在图片中的文字保存在$_SESSION中
43 $_SESSION['SafeCode'] = $randval;
44 //产生随机字符串
45 function randStr($len=6,$format='ALL') {
46 switch($format) {
47 case 'ALL':
48 $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
49 break;
50 case 'CHAR':
51 $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
52 break;
53 case 'NUMBER':
54 $chars='0123456789';
55 break;
56 default :
57 $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
58 break;
59 }
60 $string="";
61 while(strlen($string)<$len) {
62 $string.=substr($chars,(mt_rand()%strlen($chars)),1);
63 }
64 return $string;
65 }
66 ?>

  

  上面的代码在我的win2k3+PHP5.3.8下运行出现了问题,提示说

PHP Warning:  imagegif() [<a href='function.imagegif'>function.imagegif</a>]: Unable to open temporary file in C:\Inetpub\wwwroot\index.php on line 39

  去百度了一下,有人说把imagegif()换成imagepng()就好了,他估计是gd库的问题。试了一下,把第四行的type = 'gif'; 改成png,果然顺利执行,结果就像这样:

  不仅图片很小,字符也很规整。。。一个字:改!

  

 1 <?php 
2 session_start();
3
4 $type = 'png';
5 header("Content-type: image/".$type);
6
7 $num = 4;
8 $randval = randStr($num);
9 $width= 20 + 25 * $num; //图片大小,每个字符占25个像素,左右各预留出10个像素
10 $height= 36;
11
12 if($type!='gif' && function_exists('imagecreatetruecolor')){
13 $im = @imagecreatetruecolor($width,$height);
14 }else{
15 $im = @imagecreate($width,$height);
16 }
17
18 $br = mt_rand(127,255); //backgrand-R 背景色应该是浅色调的,字符的颜色为深色调
19 $bg = mt_rand(127,255); //backgrand-G
20 $bb = mt_rand(127,255); //backgrangd-B
21
22 $backColor = ImageColorAllocate($im, $br, $bg, $bb); //背景色
23 $borderColor = ImageColorAllocate($im, 0, 0, 0); //边框色
24 $pointColor = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //点颜色
25 $stringColor = ImageColorAllocate($im, $br - 90 - mt_rand(0,37), $bg - 90 - mt_rand(0,37), $bb - 90 - mt_rand(0,37));
26 //其实这里的意思就是$br - 127,但是这样的话就没有了随机的效果。。。于是。。。
27
28 @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);//背景位置
29 @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor); //边框位置
30
31
32
33 for($i = 0; $i <= 100; $i++){ //画干扰点
34 $pointX = mt_rand(2, $width - 2);
35 $pointY = mt_rand(2, $height - 2);
36 @imagesetpixel($im, $pointX, $pointY, $pointColor);
37 }
38
39 $fontttf = "arial.ttf"; //需要arial.ttf文件
40
41 for ($i = 0; $i < $num; $i++)
42 @imagettftext($im, 24, mt_rand(-30,30), ($width - 20) / $num * $i + 10, $height - 8, $stringColor, $fontttf, $randval[$i]);
43 //随机旋转-30~+30度。需要注意的是传进这个函数的x和y是左下角的坐标
44
45 for ($i = 0; $i < 2; $i++){ //随机画两条线
46 $pointX = mt_rand(0, $width / 4); //起点在左侧1/4内,终点在右侧1/4内
47 $pointY = mt_rand(0, $height);
48 $pointX2 = mt_rand($width / 4 * 3, $width);
49 $pointY2 = mt_rand(0, $height);
50 @imageline($im, $pointX, $pointY, $pointX2, $pointY2, $pointColor);
51 }
52 @imageellipse($im, mt_rand(0,$width), mt_rand(0,$height), mt_rand($width / 2, $width * 2), mt_rand($height / 2, $height), $pointColor);
53 //随机画个椭圆
54
55 $ImageFun='Image'.$type;
56 $ImageFun($im);
57
58 @ImageDestroy($im);
59 $_SESSION['SafeCode'] = $randval;
60
61 function randStr($len,$format="") {
62 switch($format) { //我把万恶的Oo0去掉了……
63 case 'CHAR':
64 $chars='ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz';
65 break;
66 case 'NUMBER':
67 $chars='123456789';
68 break;
69 default :
70 $chars='ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz123456789';
71 break;
72 }
73 $string="";
74 while(strlen($string) < $len) {
75 $string.=substr($chars,(mt_rand()%strlen($chars)),1);
76 }
77 return $string;
78 }
79 ?>

  上面的代码执行效果:

  感觉还可以……恩 创新项目的网站就用这个了把。

原文地址:https://www.cnblogs.com/tuesday/p/2270615.html