這篇文章主要介紹了PHP生成圖像驗證碼的方法,結(jié)合實例形式分析了加法運算驗證碼與字符驗證碼2種方法供大家參考借鑒,需要的朋友可以參考下
1、生成加法運算驗證碼圖片
session_start ();
/*定義頭文件為圖片*/
header("Content-type: image/png");
/*生成驗證碼*/
/*創(chuàng)建圖片設(shè)置字體顏色*/
$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;
/*設(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);
/*在畫布上隨機生成大量點*/
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 ();
/*設(shè)置文件頭為圖片輸出*/
Header("Content-type: image/JPEG");
/*調(diào)用生成驗證碼函數(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);
}
/*將字符寫入文件中*/
$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程序設(shè)計有所幫助。