php显示随机验证码()

 1 <?php
 2 header("Content-Type:image/png");
 3 // $str="北京欢迎你!";
 4 // imagefill($img,0,0,$color1);
 5 // // imagestring($img,5,0,0,$str,$color2);
 6 
 7 //1.产生随机字符串
 8 $arr1 = array_merge(range('A','Z'),range(0,9),range('a','z'));
 9 shuffle($arr1);
10 $arr2 = array_rand($arr1,4);
11 $str = "";
12 // print_r($arr2);
13 // echo "<br/>";
14 foreach($arr2 as $index){
15     $str .= $arr1[$index];
16 }
17 // echo $str;
18 // 2.创建空画布
19 $width = 200;
20 $height = 100;
21 
22 $img = imagecreatetruecolor($width,$height);
23 // 3.绘制带填充矩形
24 $color1 = imagecolorallocate($img,mt_rand(80,255),mt_rand(100,255),mt_rand(180,255));
25 $color2 = imagecolorallocate($img,255,0,0);
26 imagefilledrectangle($img,0,0,$width,$height,$color1);
27 // 4.绘制像素点
28 for ($i=0; $i <100; $i++) { 
29     $color3=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));    
30     imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$color3);
31 }
32 $fontfile="F:\phpStudy\WWW\myweb\FZXBSJW.ttf";
33 imagettftext($img,24,0,60,60,$color2,$fontfile,$str);
34 imagepng($img);
35 imagedestroy($img);
————————
 1 <?php
 2 header("Content-Type:image/png");
 3 // $str="北京欢迎你!";
 4 // imagefill($img,0,0,$color1);
 5 // // imagestring($img,5,0,0,$str,$color2);
 6 
 7 //1.产生随机字符串
 8 $arr1 = array_merge(range('A','Z'),range(0,9),range('a','z'));
 9 shuffle($arr1);
10 $arr2 = array_rand($arr1,4);
11 $str = "";
12 // print_r($arr2);
13 // echo "<br/>";
14 foreach($arr2 as $index){
15     $str .= $arr1[$index];
16 }
17 // echo $str;
18 // 2.创建空画布
19 $width = 200;
20 $height = 100;
21 
22 $img = imagecreatetruecolor($width,$height);
23 // 3.绘制带填充矩形
24 $color1 = imagecolorallocate($img,mt_rand(80,255),mt_rand(100,255),mt_rand(180,255));
25 $color2 = imagecolorallocate($img,255,0,0);
26 imagefilledrectangle($img,0,0,$width,$height,$color1);
27 // 4.绘制像素点
28 for ($i=0; $i <100; $i++) { 
29     $color3=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));    
30     imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$color3);
31 }
32 $fontfile="F:\phpStudy\WWW\myweb\FZXBSJW.ttf";
33 imagettftext($img,24,0,60,60,$color2,$fontfile,$str);
34 imagepng($img);
35 imagedestroy($img);