国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > 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.  * @param int $type //包含數字或者英文 
  25.  * @param int $length 多少個字符 
  26.  * @param int $pixel 干擾小點的密度 
  27.  * @param int $dst_h 干擾線的密度 
  28.  * @param string //驗證碼在$_SESSION中的名字 
  29.  */ 
  30.  
  31. function verifyImage($type=1,$length=4,$pixel=0,$line=0,$sess_name = "verify"){ 
  32.   //session_start(); 
  33.   //創建畫布 
  34.   $width = 100; 
  35.   $height = 40; 
  36.   $image = imagecreatetruecolor ( $width$height ); 
  37.   $white = imagecolorallocate ( $image, 255, 255, 255 ); 
  38.   $black = imagecolorallocate ( $image, 0, 0, 0 ); 
  39.   //用填充矩形填充畫布 
  40.   imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white ); 
  41.   $chars = buildRandomString ( $type$length ); 
  42.   $_SESSION [$sess_name] = $chars
  43.   //$fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" ); 
  44.   $fontfiles = array ("SIMKAI.TTF" ); 
  45.   //由于字體文件比較大,就只保留一個字體,如果有需要的同學可以自己添加字體,字體在你的電腦中的fonts文件夾里有,直接運行輸入fonts就能看到相應字體 
  46.   for($i = 0; $i < $length$i ++) { 
  47.     $size = mt_rand ( 14, 18 ); 
  48.     $angle = mt_rand ( - 15, 15 ); 
  49.     $x = 5 + $i * $size
  50.     $y = mt_rand ( 20, 26 ); 
  51.     $fontfile = "../fonts/" . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )]; 
  52.     $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) ); 
  53.     $text = substr ( $chars$i, 1 ); 
  54.     imagettftext ( $image$size$angle$x$y$color$fontfile$text ); 
  55.   } 
  56.   if ($pixel) { 
  57.     for($i = 0; $i < 50; $i ++) { 
  58.       imagesetpixel ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $black ); 
  59.     } 
  60.   } 
  61.   if ($line) { 
  62.     for($i = 1; $i < $line$i ++) { 
  63.       $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) ); 
  64.       imageline ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $color ); 
  65.     } 
  66.   } 
  67.   header ( "content-type:image/gif" ); 
  68.   imagegif ( $image ); 
  69.   imagedestroy ( $image ); 

主要要點:

1、如果前面沒有申明session_start();則需要申明;

2、字體可以在cmf輸入fonts下載到自己定義的fonts文件夾;

3、$_SESSION [$sess_name]可以通過$_POST獲得用戶輸入的驗證碼進行比較。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 绩溪县| 萨迦县| 青河县| 兴义市| 澳门| 化州市| 靖州| 枣庄市| 安陆市| 原平市| 杨浦区| 桂阳县| 长宁县| 察隅县| 克拉玛依市| 沙河市| 南涧| 水城县| 无为县| 滦南县| 新津县| 大名县| 杨浦区| 五大连池市| 微博| 长兴县| 彭山县| 新乡县| 丹棱县| 双桥区| 长武县| 永年县| 那坡县| 鱼台县| 盱眙县| 江北区| 夏河县| 收藏| 忻城县| 沂水县| 庆安县|