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

首頁 > 語言 > PHP > 正文

php實現生成驗證碼實例分享

2024-05-04 23:44:56
字體:
來源:轉載
供稿:網友
由于注冊的時候常常會用到注冊碼來防止機器惡意注冊,這里我發表一個產生圖片驗證碼的基本圖像,很簡陋,有需要的小伙伴可以參考下
 

image.func.php

<?php require_once('string.func.php');  function verifyImage( $type=1,$length=4,$pixel=0,$line=0,$sess_name="verify"){    session_start();  /*定義長度和寬度*/$width=80;$height=30;/* 創建畫布*/$image=imagecreatetruecolor($width, $height); /*本函數用來匹配圖形的顏色,供其它繪圖函數使用。參數 image 表示圖形的 handle。參數 red、green、blue 是色彩三原色,其值從 0 至 255....我在此定義黑色和白色*/$white=imagecolorallocate($image, 255, 255, 255);$black=imagecolorallocate($image,0,0,0); /*本函數將圖片的封閉長方形區域著色。參數 x1、y1 及 x2、y2 分別為矩形對角線的坐標。參數 col 表示欲涂上的顏色*/imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white); /*buildRandomString函數用來生成一個驗證碼*/$chars=buildRandomString($type,$length); /*將驗證碼給session以便用來判斷用戶輸入是否正確*/$_SESSION[$sess_name]=$chars; /*定義字體庫*/$fontfiles=array('msyh.ttf','msyhbd.ttf','simsun.ttc','SIMYOU.TTF','STHUPO.TTF','STKAITI.TTF','STLITI.TTF');  /*用循環來將驗證碼一個一個的寫入圖片中*/for($i=0;$i<$length;$i++){  $size=mt_rand(14,18);  $angle=mt_rand(-15,15);   /*驗證碼的橫坐標與縱坐標*/  $x=5+$i*$size;  $y=mt_rand(20,26);   $color=imagecolorallocate($image,mt_rand(50,190),mt_rand(50,200),mt_rand(50,90));  $fontfile="../font/".$fontfiles[mt_rand(0,count($fontfiles)-1)];  $text=substr($chars,$i,1);   /*本函數將 TTF (TrueType Fonts) 字型文字寫入圖片*/ imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);}if($pixel){for($i=0;$i<50;$i++){  /*本函數可在圖片上繪出一點。參數 x、y 為欲繪點的坐標,參數 col 表示該點的顏色*/  imagesetpixel($image, mt_rand(0,$width-1), mt_rand(0,$height-1), $black);}}if($line){  for($i=0;$i<10;$i++)  {  $color=imagecolorallocate($image,mt_rand(50,90),mt_rand(50,200),mt_rand(50,90));  /*畫線段*/  imageline($image, mt_rand(0,$width-1), mt_rand(0,$height-1), mt_rand(0,$width-1), mt_rand(0,$height-1), $color);}}/*以gif形式輸出*/header("content-type:image/gif");/*建立GIF圖 并輸出到網頁*/imagegif($image);/*釋放與 image 關聯的內存*/imagedestroy($image);}

string.func.php

<?php  function buildRandomString($type=1,$length=4){ if($type==1){  /*join函數把數組轉換為字符串。。join() 函數是 implode() 函數的別名*/  $chars=join("",range(0,9));}elseif ($type==2) {  /*array_merge函數合并數組*/  $chars=join("",array_merge(range("a","z"),range("A","Z")));}elseif($type==3){  $chars=join("",array_merge(range("a","z"),range("A","Z"),range(0,9)));} if($length>strlen($chars)){  exit("字符串長度不夠");}/*打亂字符串*/$chars=str_shuffle($chars);return substr($chars,0,$length); } ?>


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

圖片精選

主站蜘蛛池模板: 高要市| 岳阳市| 沧源| 盐城市| 冀州市| 刚察县| 留坝县| 丰原市| 沁水县| 观塘区| 焦作市| 柳河县| 精河县| 张家界市| 平遥县| 灵宝市| 桦甸市| 泽库县| 高陵县| 四子王旗| 原阳县| 东乡县| 将乐县| 桂林市| 札达县| 醴陵市| 泰兴市| 保德县| 合江县| 宜黄县| 宜兰县| 沐川县| 吉林市| 南召县| 若羌县| 衡南县| 临颍县| 宜昌市| 通化县| 库车县| 大荔县|