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

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

1、生成加法運算驗證碼圖片

session_start ();

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

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

/*生成驗證碼*/

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

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

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

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

/*隨機生成兩個數(shù)字*/

$num1 = rand(1, 20);

$num2 = rand(1, 20);

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

/*設置圖片背景顏色*/

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

/*在畫布上隨機生成大量點*/

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

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

}

/*將計算驗證碼寫入到圖片中*/

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、生成字符驗證碼圖片【值得注意的是在字體哪里,需要引入實際的字體路徑,否則,可能出現(xiàn)圖像顯示不了驗證碼】

session_start ();

/*設置文件頭為圖片輸出*/

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

/*調用生成驗證碼函數(shù)*/

$str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234565789";

$result="";

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

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

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

}

$text = $result;

$_SESSION ["administratorConfirmCode"] = $text;

/*設置圖片的寬度和高度*/

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

}

/*將字符寫入文件中*/

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

    }

  }

}

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

$count = 160;

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

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

希望本文所述對大家PHP程序設計有所幫助。

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:PHP生成圖像驗證碼的方法小結(2種方法)
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權所有:易賢網(wǎng)