php 创建验证码方法

php创建验证码方法:

 1 <?php  2     function getVerify($length=4,$sessName='verify'){  3         //验证码  4  //获取字符串 去除01ol等较难辨认字符
 5         $chars = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";  6         $chars = str_shuffle($chars);  7         $chars = substr($chars, 0, $length);  8         //将字符存入session
 9         $_SESSION[$sessName] = $chars; 10         //定义画布大小
11         $width = 80; 12         $height = 30; 13         $image = imagecreatetruecolor($width, $height); 14         
15         //定义背景颜色和边框颜色
16         $white = imagecolorallocate($image, 255, 255, 255); 17         $black = imagecolorallocate($image, 0, 0, 0); 18         
19         //用填充矩形填充画布
20         imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white); 21         
22         //定义验证码颜色
23         $verifyColor = imagecolorallocate($image, 15, 164, 80); 24         
25         //将字符写入画布
26         for($i=0; $i<$length; $i++){ 27             $size = 18; 28             $angle = mt_rand(-12, 12); 29             $x = 5 + $i*$size; 30             $y = 23; 31             $fontfile = 'GEORGIA.TTF';//字体
32             $color = $verifyColor; 33             $text = substr($chars, $i, 1); 34             imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); 35  } 36         //干扰点
37         for($i=0; $i<80; $i++){ 38             imagesetpixel($image, mt_rand(0, $width-1), mt_rand(0, $height-1), $verifyColor); 39  } 40         //干扰线
41         for($i=0; $i<3; $i++){ 42             imageline($image, mt_rand(0, $width-1), mt_rand(0, $height-1), mt_rand(0, $width-1), mt_rand(0, $height-1), $verifyColor); 43  } 44         //告诉浏览器输出格式
45         header("content-type:image/gif"); 46         //输出
47         imagegif($image); 48         //销毁,释放内存
49         imagedestroy($image); 50     }

 

你可能感兴趣的:(php 创建验证码方法)