php教程 中文與英文驗證碼程序代碼
//英文驗證碼相對簡單,不要作hex處理,直接用色彩值就OK了。如果
session_start();
function rand_create()
{
//通知浏覽器將要輸出PNG圖片
Header("Content-type: image/PNG");
//准備好隨機數發生器種子
srand((double)microtime()*1000000);
//准備圖片的相關參數
$im = imagecreate(62,22);
$black = ImageColorAllocate($im, 0,0,0); //RGB黑色標識符
$white = ImageColorAllocate($im, 255,255,255); //RGB白色標識符
$gray = ImageColorAllocate($im, 200,200,200); //RGB灰色標識符
//開始作圖
imagefill($im,0,0,$gray);
while(($randval=rand()%100000)<10000);{
$_SESSION["Auth_code"] = $randval;
//將四位整數驗證碼繪入圖片
imagestring($im, 5, 10, 3, $randval, $black);
}
//加入干擾象素
for($i=0;$i<200;$i++){
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
//輸出驗證圖片
ImagePNG($im);
//銷毀圖像標識符
ImageDestroy($im);
}
rand_create();
//調用方法<img src=www.bkjia.com.php />
//中文驗證碼程序如下
Header("Content-type: image/PNG");
$str = "這裡設置一中文如果中國WEB第一站www.bkjia.com";
$imagesW = 140;
$imagesH = 40;
//
$Auimg = imagecreate($imagesW,$imagesH);
$bgc = ImageColorAllocate($Auimg,255,255,255);
$font = "heiti.ttf";//這裡設置字體,你可以隨便下載一款字體哦。
$white=imagecolorallocate($Auimg,234,185,95);
imagearc($Auimg, 150, 8, 20, 20, 75, 170, $white);
imagearc($Auimg, 180, 7,50, 30, 75, 175, $white);
imageline($Auimg,20,20,180,30,$white);
imageline($Auimg,20,18,170,50,$white);
imageline($Auimg,25,50,80,50,$white);
$noise_num = 800;
$line_num = 20;
imagecolorallocate($Auimg,0xff,0xff,0xff);
$rectangle_color=imagecolorallocate($Auimg,0xAA,0xAA,0xAA);
$noise_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
$font_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
$line_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++){
imagesetpixel($Auimg,mt_rand(0,$imagesW),mt_rand(0,$imagesH),$noise_color);
}
for($i=0;$i<$line_num;$i++){
imageline($Auimg,mt_rand(0,$imagesW),mt_rand(0,$imagesH),mt_rand(0,$imagesW),mt_rand(0,$imagesH),$line_color);
}
$mtRnd=rand(0,strlen($str)-4);
if($mtRnd%2)$mtRnd+=1;
$str = substr($str,$mtRnd,8);
$str = iconv("GB2312","UTF-8",$str);
ImageTTFText($Auimg, 20, 0, 16, 30, $font_color, $font, $str);
ImagePNG($Auimg);
ImageDestroy($Auimg);
/*
共同點就是驗證碼都借助於其它容器來保存如session,cookie等,否則就沒有驗證的意義了
本文章由www.bkjia.com整,轉載請注明來源謝謝合作。