PHP生成圖像驗(yàn)證碼的方法小結(jié)(2種方法)
來(lái)源:易賢網(wǎng) 閱讀:779 次 日期:2016-08-12 15:16:28
溫馨提示:易賢網(wǎng)小編為您整理了“PHP生成圖像驗(yàn)證碼的方法小結(jié)(2種方法)”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了PHP生成圖像驗(yàn)證碼的方法,結(jié)合實(shí)例形式分析了加法運(yùn)算驗(yàn)證碼與字符驗(yàn)證碼2種方法供大家參考借鑒,需要的朋友可以參考下

1、生成加法運(yùn)算驗(yàn)證碼圖片

session_start ();

/*定義頭文件為圖片*/

header("Content-type: image/png");

/*生成驗(yàn)證碼*/

/*創(chuàng)建圖片設(shè)置字體顏色*/

$im = imagecreate($w, $h);

$red = imagecolorallocate($im, 255, 255, 255);

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

/*隨機(jī)生成兩個(gè)數(shù)字*/

$num1 = rand(1, 20);

$num2 = rand(1, 20);

$_SESSION ["administratorConfirmCode"] = $num1+$num2;

/*設(shè)置圖片背景顏色*/

$gray = imagecolorallocate($im, 118, 151, 199);

$black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));

/*創(chuàng)建圖片背景*/

imagefilledrectangle($im, 0, 0, 100, 24, $black);

/*在畫(huà)布上隨機(jī)生成大量點(diǎn)*/

for ($i = 0; $i < 80; $i++) {

  imagesetpixel($im, rand(0, $w), rand(0, $h), $gray);

}

/*將計(jì)算驗(yàn)證碼寫(xiě)入到圖片中*/

imagestring($im, 5, 5, 4, $num1, $red);

imagestring($im, 5, 30, 3, "+", $red);

imagestring($im, 5, 45, 4, $num2, $red);

imagestring($im, 5, 70, 3, "=", $red);

imagestring($im, 5, 80, 2, "?", $white);

/*輸出圖片*/

imagepng($im);

imagedestroy($im);

2、生成字符驗(yàn)證碼圖片【值得注意的是在字體哪里,需要引入實(shí)際的字體路徑,否則,可能出現(xiàn)圖像顯示不了驗(yàn)證碼】

session_start ();

/*設(shè)置文件頭為圖片輸出*/

Header("Content-type: image/JPEG");

/*調(diào)用生成驗(yàn)證碼函數(shù)*/

$str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234565789";

$result="";

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

  $num[$i]=rand(0,61);

  $result.=$str[$num[$i]];

}

$text = $result;

$_SESSION ["administratorConfirmCode"] = $text;

/*設(shè)置圖片的寬度和高度*/

$im_x = $w;

$im_y = $y;

/*創(chuàng)建圖片*/

$im = imagecreatetruecolor($im_x,$im_y);

$text_c = ImageColorAllocate($im, mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));

$tmpC0=mt_rand(100,255);

$tmpC1=mt_rand(100,255);

$tmpC2=mt_rand(100,255);

$buttum_c = ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2);

imagefill($im, 16, 13, $buttum_c);

/*字體文件*/

$font = _WEB_DIR_.'/font/comic.ttf';

for ($i=0;$i<strlen($text);$i++){

  $tmp =substr($text,$i,1);

  $array = array(-1,1);

  $p = array_rand($array);

  $an = $array[$p]*mt_rand(1,10);//角度

  $size = 28;

  imagettftext($im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp);

}

/*將字符寫(xiě)入文件中*/

$distortion_im = imagecreatetruecolor ($im_x, $im_y);

imagefill($distortion_im, 16, 13, $buttum_c);

for ( $i=0; $i<$im_x; $i++) {

  for ( $j=0; $j<$im_y; $j++) {

    $rgb = imagecolorat($im, $i , $j);

    if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) <= imagesx($distortion_im)&& (int)($i+20+sin($j/$im_y*2*M_PI)*10) >=0 ) {

      imagesetpixel ($distortion_im, (int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.1)*4) , $j , $rgb);

    }

  }

}

/*干擾元素點(diǎn)的數(shù)量*/

$count = 160;

/*創(chuàng)建干擾元素點(diǎn)*/

for($i=0; $i<$count; $i++){

  $randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

  imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);

}

/*創(chuàng)建干擾線條*/

$rand = mt_rand(5,30);

$rand1 = mt_rand(15,25);

$rand2 = mt_rand(5,10);

for ($yy=$rand; $yy<=+$rand+2; $yy++){

  for ($px=-80;$px<=80;$px=$px+0.1){

    $x=$px/$rand1;

    if ($x!=0){

      $y=sin($x);

    }

    $py=$y*$rand2;

    imagesetpixel($distortion_im, $px+80, $py+$yy, $text_c);

  }

}

/*以PNG格式將圖像輸出到瀏覽器*/

ImagePNG($distortion_im);

/*銷毀圖像*/

ImageDestroy($distortion_im);

ImageDestroy($im);

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:PHP生成圖像驗(yàn)證碼的方法小結(jié)(2種方法)
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)