|
復(fù)制代碼 代碼如下:
$authnum='';
$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list=explode(",",$ychar);//分割函數(shù)
for($i=0;$i<4;$i++){
$randnum=rand(0,35);
$authnum.=$list[$randnum];//以數(shù)組的形式輸出
第二種:
復(fù)制代碼 代碼如下:
private function createCheckCode()
{
for(i=0;i<this->codeNum;i++)
{
number = rand(0,2);
switch(number)
{
case 0: rand_number = rand(48,57); break;//數(shù)字
case 1: rand_number = rand(65,90);break;//大寫字母
case 2: rand_number = rand(97,122);break;//小寫字母
}
$asc = sprintf("%c",rand_number);
$asc_number = asc_number.asc;
}
return asc_number;
}
第三種:
復(fù)制代碼 代碼如下:
srand(microtime()*100000);//相當(dāng)于計時器
$string="abcdefghigklmnopqrstuvwxyz123456789";
for($i=0;$i<4;$i++)
{
$new_number.=$string[rand(0,strlen($string)-1)];//隨即的產(chǎn)生一個數(shù)組
}
第四種:
復(fù)制代碼 代碼如下:
for($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));//將十進制轉(zhuǎn)化為十六進制
}
GD庫:(提供了一系列圖片處理函數(shù)的IPI,生成圖片處理圖片)
啟用php中GD庫:php.ini配置文件中,去掉";extension=php_gd2.dll"中“;”;
部分GD庫函數(shù)的介紹:1.imagecreatetruecolor(int x_size,int Y_size) 新建真彩色圖像
2.imagecolorallocate(resource image,int red,int green,int blue) 為一幅圖像分配顏色,三原色
3.imagestring(resource,font,int x,int y,content,color)繪圖函數(shù)4.header("Content-type:image/jpeg") 輸出函數(shù)php的header是定義頭的動作,php5中支持3中類型: 1,Content-type:xxxx/yyyy 2,Location:xxxx:yyyy/zzzz 3,Status:nnn xxxxxx xxxx/yyyy表示內(nèi)容文件的類型 如:image/gif image/jpeg image/png imagejpeg(),imagegif(),imagepang() 5.iamgeline(resource image,int x1,int y1,int x2,int y2,int color); 畫線函數(shù),(int x,int y)起始坐標(biāo)6.imagesetpixel(resource image,int x,int y,int color) 畫點函數(shù)7.imagettftext(resource image,float size,float angle,int x,int y,int color,string fontfile,string text) 帶字體寫入函數(shù)8.iconv("gb2312","utf-8","字符串"); //首先要將文字轉(zhuǎn)換成utf-8格式 php驗證碼插入中文的方法。
隨機生成數(shù)字,字母的代碼:
復(fù)制代碼 代碼如下:
<?php
//che.php
session_start();
for($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));
}
$_SESSION['check_num']=$rand;
$image=imagecreatetruecolor(50,30);
$bg=imagecolorallocate($im,0,0,0);//第一次用調(diào)色板的時候,背景顏色
$te=imagecolorallocate($im,255,255,255);
imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
ob_clean();//php網(wǎng)頁中因為 要生成驗證碼而出現(xiàn) 圖像"http://localhost/**.php"因其本身有錯無法顯示
header("Content-type:image/jpeg"); imagejpeg($image);
?>
給圖片畫出干擾線代碼:
復(fù)制代碼 代碼如下:
for($i=0;$i<8;$i++)//畫出多條線
{
$cg=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//產(chǎn)生隨機的顏色
imageline($im,rand(10,40),0,rand(10,40),20,$cg);
}
給圖片畫出干擾點的代碼:
復(fù)制代碼 代碼如下:
for($i=0;$i<80;$i++)//畫出多個點
{
imagesetpixel($im,rand(0,40),rand(0,20),$cg);
}
把文字寫入圖片代碼:
復(fù)制代碼 代碼如下:
$str=array('我','我','親','親');//存儲顯示的漢字
for($i=0;$i<4;$i++)
{
$sss.=$str[rand(0,3)];//隨機顯示漢字
}
//$str=iconv("gb2312","utf-8",$str); //漢字編碼轉(zhuǎn)化,我的好像不需要
imagettftext($im,10,0,rand(5,60),rand(5,60),$te,"simhei.ttf",$sss);//
0:字體的傾斜度,“simhei.ttf”:字體樣式,一般放在根目錄下;
php技術(shù):PHP圖片驗證碼制作實現(xiàn)分享(全),轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。