当前位置:首页 > PHP教程 > php应用 > 列表

php实现生成验证码实例分享

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-27 11:24:40 浏览: 评论:0 

由于注册的时候常常会用到注册码来防止机器恶意注册,这里我发表一个产生图片验证码的基本图像,很简陋,有需要的小伙伴可以参考下。

image.func.php

  1. <?php  
  2. require_once('string.func.php'); 
  3.    
  4.  function verifyImage( $type=1,$length=4,$pixel=0,$line=0,$sess_name="verify"){  
  5.    
  6.   session_start(); 
  7.   /*定义长度和宽度*/ 
  8. $width=80; 
  9. $height=30; 
  10. /* 创建画布*/ 
  11. $image=imagecreatetruecolor($width, $height); 
  12.    
  13. /*本函数用来匹配图形的颜色,供其它绘图函数使用。参数 image 表示图形的 handle。参数 red、green、blue 是色彩三原色,其值从 0 至 255....我在此定义黑色和白色*/ 
  14. $white=imagecolorallocate($image, 255, 255, 255); 
  15. $black=imagecolorallocate($image,0,0,0); 
  16.    
  17. /*本函数将图片的封闭长方形区域着色。参数 x1、y1 及 x2、y2 分别为矩形对角线的坐标。参数 col 表示欲涂上的颜色*/ 
  18. imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white); 
  19.    
  20. /*buildRandomString函数用来生成一个验证码*/ 
  21. $chars=buildRandomString($type,$length); 
  22.    
  23. /*将验证码给session以便用来判断用户输入是否正确*/ 
  24. $_SESSION[$sess_name]=$chars; 
  25.    
  26. /*定义字体库*/ 
  27. $fontfiles=array('msyh.ttf','msyhbd.ttf','simsun.ttc','SIMYOU.TTF','STHUPO.TTF','STKAITI.TTF','STLITI.TTF'); 
  28.    
  29.    
  30. /*用循环来将验证码一个一个的写入图片中*/ 
  31. for($i=0;$i<$length;$i++) 
  32. { 
  33.   $size=mt_rand(14,18); 
  34.   $angle=mt_rand(-15,15); 
  35.    
  36.   /*验证码的横坐标与纵坐标*/ 
  37.   $x=5+$i*$size; 
  38.   $y=mt_rand(20,26); 
  39.    
  40.   $color=imagecolorallocate($image,mt_rand(50,190),mt_rand(50,200),mt_rand(50,90)); 
  41.   $fontfile="../font/".$fontfiles[mt_rand(0,count($fontfiles)-1)]; 
  42.   $text=substr($chars,$i,1); 
  43.    
  44.   /*本函数将 TTF (TrueType Fonts) 字型文字写入图片*/ 
  45.  imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); 
  46. } 
  47. if($pixel) 
  48. { 
  49. for($i=0;$i<50;$i++) 
  50. { 
  51.   /*本函数可在图片上绘出一点。参数 x、y 为欲绘点的坐标,参数 col 表示该点的颜色*/ 
  52.   imagesetpixel($image, mt_rand(0,$width-1), mt_rand(0,$height-1), $black); 
  53. }} 
  54. if($line) 
  55. { 
  56.   for($i=0;$i<10;$i++) 
  57.   { 
  58.   $color=imagecolorallocate($image,mt_rand(50,90),mt_rand(50,200),mt_rand(50,90)); 
  59.   /*画线段*/ 
  60.   imageline($image, mt_rand(0,$width-1), mt_rand(0,$height-1), mt_rand(0,$width-1), mt_rand(0,$height-1), $color); 
  61. } 
  62. } 
  63. /*以gif形式输出*/ 
  64. header("content-type:image/gif"); 
  65. /*建立GIF图 并输出到网页*/ 
  66. imagegif($image); 
  67. /*释放与 image 关联的内存*/ 
  68. imagedestroy($image); 
  69. } 

string.func.php

  1. <?php  
  2.    
  3. function buildRandomString($type=1,$length=4){ 
  4.    
  5. if($type==1) 
  6. { 
  7.   /*join函数把数组转换为字符串。。join() 函数是 implode() 函数的别名*/ 
  8.   $chars=join("",range(0,9)); 
  9. }elseif ($type==2) { 
  10.   /*array_merge函数合并数组*/ 
  11.   $chars=join("",array_merge(range("a","z"),range("A","Z"))); 
  12. }elseif($type==3) 
  13. { 
  14.   $chars=join("",array_merge(range("a","z"),range("A","Z"),range(0,9))); 
  15. } 
  16.    
  17. if($length>strlen($chars)) 
  18. { 
  19.   exit("字符串长度不够"); 
  20. } 
  21. /*打乱字符串*/ 
  22. $chars=str_shuffle($chars); 
  23. return substr($chars,0,$length); 
  24.    
  25. } 
  26.  ?>

Tags: php生成验证码

分享到: