首页 > 语言 > PHP > 正文

php中使用GD库做验证码

2024-05-04 23:44:48
字体:大 中 小
来源:转载
供稿:网友

本文给大家分享的是使用php的GD库来实现验证码功能的方法和全部代码,有需要的小伙伴可以参考下
 

  1. <?php  
  2. require_once 'string.func.php'; 
  3. //通过GD库做验证码 
  4. /** 
  5.  *添加验证文字 
  6.  * @param int $type 
  7.  * @param int $length 
  8. */ 
  9. function buildRandomString($type=1,$length=4){ 
  10.   $row=''; 
  11.   if($type==1){ 
  12.     $row=join('',range(0, 9)); 
  13.   }else if($type==2){ 
  14.     $row=join('', array_merge(range('a','z'),range('A', 'Z'))); 
  15.   }else if($type==3){ 
  16.     $row=join('', array_merge(range('a','z'),range('A', 'Z'),range(0, 9))); 
  17.   }; 
  18.   $row=str_shuffle($row); 
  19.    
  20.   $row=substr($row,0,$length); 
  21.   return $row; 
  22. } 
  23. /** 
  24.  * 生成缩略图 
  25.  * @param int $type //包含数字或者英文 
  26.  * @param int $length 多少个字符 
  27.  * @param int $pixel 干扰小点的密度 
  28.  * @param int $dst_h 干扰线的密度 
  29.  * @param string //验证码在$_SESSION中的名字 
  30.  */ 
  31.  
  32. function verifyImage($type=1,$length=4,$pixel=0,$line=0,$sess_name = "verify"){ 
  33.   //session_start(); 
  34.   //创建画布 
  35.   $width = 100; 
  36.   $height = 40; 
  37.   $image = imagecreatetruecolor ( $width, $height ); 
  38.   $white = imagecolorallocate ( $image, 255, 255, 255 ); 
  39.   $black = imagecolorallocate ( $image, 0, 0, 0 ); 
  40.   //用填充矩形填充画布 
  41.   imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white ); 
  42.   $chars = buildRandomString ( $type, $length ); 
  43.   $_SESSION [$sess_name] = $chars; 
  44.   //$fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" ); 
  45.   $fontfiles = array ("SIMKAI.TTF" ); 
  46.   //由于字体文件比较大,就只保留一个字体,如果有需要的同学可以自己添加字体,字体在你的电脑中的fonts文件夹里有,直接运行输入fonts就能看到相应字体 
  47.   for($i = 0; $i < $length; $i ++) { 
  48.     $size = mt_rand ( 14, 18 ); 
  49.     $angle = mt_rand ( - 15, 15 ); 
  50.     $x = 5 + $i * $size; 
  51.     $y = mt_rand ( 20, 26 ); 
  52.     $fontfile = "../fonts/" . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )]; 
  53.     $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) ); 
  54.     $text = substr ( $chars, $i, 1 ); 
  55.     imagettftext ( $image, $size, $angle, $x, $y, $color, $fontfile, $text ); 
  56.   } 
  57.   if ($pixel) { 
  58.     for($i = 0; $i < 50; $i ++) { 
  59.       imagesetpixel ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $black ); 
  60.     } 
  61.   } 
  62.   if ($line) { 
  63.     for($i = 1; $i < $line; $i ++) { 
  64.       $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) ); 
  65.       imageline ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $color ); 
  66.     } 
  67.   } 
  68.   header ( "content-type:image/gif" ); 
  69.   imagegif ( $image ); 
  70.   imagedestroy ( $image ); 
  71. } 

主要要点:

1、如果前面没有申明session_start();则需要申明;

2、字体可以在cmf输入fonts下载到自己定义的fonts文件夹;

3、$_SESSION [$sess_name]可以通过$_POST获得用户输入的验证码进行比较。


注:相关教程知识阅读请移步到PHP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选