PHP之验证码

 1 <?php
 2 
 3     header("content-type:image/jpeg");
 4     //验证码函数
 5     function getVerify($type = 4){
 6 
 7         //字码表
 8         $chars = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
 9         //字符串的长度
10         $len = strlen($chars);
11         //预定义code
12         $verify = '';
13 
14         //产生四位随机数 ,从字码表当中
15         for ($i=1; $i <= $type ; $i++) { 
16             $one_index = mt_rand(0 ,$len-1);
17 
18             $verify.= $chars[$one_index];
19         }
20 
21         //创建画布
22         $width = 500;
23         $height = 300;
24 
25         $image = imagecreatetruecolor($width, $height);
26 
27         //配色
28         $color = "";
29 
30         //随机颜色
31         $color = imagecolorallocate($image,210,105,30); 
32         
33         
34         //填充
35         imagefill($image, 0, 0, $color);
36         
37         
38 
39         $font = 5;
40         //获得图像的宽高
41         $img_w = imagesx($image);
42         $img_h = imagesy($image);
43 
44         //获得字体的宽高
45         $font_w = imagefontwidth($font);
46         $font_h = imagefontheight($font);
47 
48         //获得字符串的宽高
49         $str_w = $font_w*$type;
50         $str_h = $font_h;
51 
52         //居中显示
53         $x = ($img_w - $str_w)/2;
54         $y = ($img_h - $str_h)/2;
55         $color = imagecolorallocate($image, 128,0,0);
56         imagestring($image, $font, $x, $y, $verify, $color);
57 
58         //输出
59         imagejpeg($image);
60 
61         //销毁画像资源
62         imagedestroy($image);
63     }
64 
65     getVerify(10);
原文地址:https://www.cnblogs.com/sharecorner/p/6165436.html