php實(shí)現(xiàn)生成驗(yàn)證碼實(shí)例分享
來源:易賢網(wǎng) 閱讀:949 次 日期:2016-08-26 16:10:46
溫馨提示:易賢網(wǎng)小編為您整理了“php實(shí)現(xiàn)生成驗(yàn)證碼實(shí)例分享”,方便廣大網(wǎng)友查閱!

由于注冊的時(shí)候常常會用到注冊碼來防止機(jī)器惡意注冊,這里我發(fā)表一個產(chǎn)生圖片驗(yàn)證碼的基本圖像,很簡陋,有需要的小伙伴可以參考下

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;

/* 創(chuàng)建畫布*/

$image=imagecreatetruecolor($width, $height);

/*本函數(shù)用來匹配圖形的顏色,供其它繪圖函數(shù)使用。參數(shù) image 表示圖形的 handle。參數(shù) red、green、blue 是色彩三原色,其值從 0 至 255....我在此定義黑色和白色*/

$white=imagecolorallocate($image, 255, 255, 255);

$black=imagecolorallocate($image,0,0,0);

/*本函數(shù)將圖片的封閉長方形區(qū)域著色。參數(shù) x1、y1 及 x2、y2 分別為矩形對角線的坐標(biāo)。參數(shù) col 表示欲涂上的顏色*/

imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white);

/*buildRandomString函數(shù)用來生成一個驗(yàn)證碼*/

$chars=buildRandomString($type,$length);

/*將驗(yàn)證碼給session以便用來判斷用戶輸入是否正確*/

$_SESSION[$sess_name]=$chars;

/*定義字體庫*/

$fontfiles=array('msyh.ttf','msyhbd.ttf','simsun.ttc','SIMYOU.TTF','STHUPO.TTF','STKAITI.TTF','STLITI.TTF');

/*用循環(huán)來將驗(yàn)證碼一個一個的寫入圖片中*/

for($i=0;$i<$length;$i++)

{

  $size=mt_rand(14,18);

  $angle=mt_rand(-15,15);

  /*驗(yàn)證碼的橫坐標(biāo)與縱坐標(biāo)*/

  $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);

  /*本函數(shù)將 TTF (TrueType Fonts) 字型文字寫入圖片*/

 imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);

}

if($pixel)

{

for($i=0;$i<50;$i++)

{

  /*本函數(shù)可在圖片上繪出一點(diǎn)。參數(shù) x、y 為欲繪點(diǎn)的坐標(biāo),參數(shù) col 表示該點(diǎn)的顏色*/

  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圖 并輸出到網(wǎng)頁*/

imagegif($image);

/*釋放與 image 關(guān)聯(lián)的內(nèi)存*/

imagedestroy($image);

}

string.func.php:

<?php 

function buildRandomString($type=1,$length=4){

if($type==1)

{

  /*join函數(shù)把數(shù)組轉(zhuǎn)換為字符串。。join() 函數(shù)是 implode() 函數(shù)的別名*/

  $chars=join("",range(0,9));

}elseif ($type==2) {

  /*array_merge函數(shù)合并數(shù)組*/

  $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);

}

 ?>

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php實(shí)現(xiàn)生成驗(yàn)證碼實(shí)例分享
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)