PHP圖片驗(yàn)證碼制作實(shí)現(xiàn)分享(全)
更新時(shí)間:2012年05月10日 19:54:14 作者:
最近正在學(xué)習(xí)php入門,現(xiàn)在剛?cè)腴T,所以許多都不知道,就從最基礎(chǔ)的學(xué)起,不會(huì)的上網(wǎng)查,然后把它記在這個(gè)法寶內(nèi)
就如今天遇到隨即函數(shù)rand();腦海中想到用它做點(diǎn)啥好呢,最后想起了驗(yàn)證碼,數(shù)字驗(yàn)證碼,字母驗(yàn)證碼,中文驗(yàn)證碼,可是自己不會(huì)呀,咋辦呢,上網(wǎng)搜,看別人的代碼,開不懂,看視頻,聽老師講,將其中所遇到的函數(shù),值得注意的地方都拿筆記下,平??吹揭话憔W(wǎng)頁上的隨機(jī)驗(yàn)證碼都是以一定的方框包圍起來,貌似就是以圖片為背景的。經(jīng)過邊看,自己邊敲,雖然遇到很多不會(huì)的問題,但是我相信只要自己腳踏實(shí)地,一定學(xué)會(huì)的?,F(xiàn)在想做一下總結(jié),自己可能寫的很亂,可我相信有一天會(huì)實(shí)現(xiàn)的。1.產(chǎn)生數(shù)字的隨機(jī)數(shù) ——》創(chuàng)建圖片——》隨機(jī)數(shù)寫進(jìn)圖片——》在圖片加入干擾值(點(diǎn),線)——》保持在session中——》在form表單中引用;隨機(jī)函數(shù):rand(int min,int max);萬變不離其宗,我看了網(wǎng)上許多中生成隨機(jī)數(shù)的代碼,有數(shù)字和字母隨機(jī)數(shù),中文隨機(jī)數(shù)(數(shù)組)等等;都離不開rand();代碼如下(有的上網(wǎng)copy,希望各位不要見怪啊第一種:
$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ù)組的形式輸出
第二種:
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;
}
第三種:
srand(microtime()*100000);//相當(dāng)于計(jì)時(shí)器
$string="abcdefghigklmnopqrstuvwxyz123456789";
for($i=0;$i<4;$i++)
{
$new_number.=$string[rand(0,strlen($string)-1)];//隨即的產(chǎn)生一個(gè)數(shù)組
}
第四種:
for($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));//將十進(jìn)制轉(zhuǎn)化為十六進(jì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是定義頭的動(dòng)作,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) 畫點(diǎn)函數(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驗(yàn)證碼插入中文的方法。
隨機(jī)生成數(shù)字,字母的代碼:
<?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)色板的時(shí)候,背景顏色
$te=imagecolorallocate($im,255,255,255);
imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
ob_clean();//PHP網(wǎng)頁中因?yàn)?要生成驗(yàn)證碼而出現(xiàn) 圖像"http://localhost/**.php"因其本身有錯(cuò)無法顯示
header("Content-type:image/jpeg"); imagejpeg($image);
?>
給圖片畫出干擾線代碼:
for($i=0;$i<8;$i++)//畫出多條線
{
$cg=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//產(chǎn)生隨機(jī)的顏色
imageline($im,rand(10,40),0,rand(10,40),20,$cg);
}
給圖片畫出干擾點(diǎn)的代碼:
for($i=0;$i<80;$i++)//畫出多個(gè)點(diǎn)
{
imagesetpixel($im,rand(0,40),rand(0,20),$cg);
}
把文字寫入圖片代碼:
$str=array('我','我','親','親');//存儲(chǔ)顯示的漢字
for($i=0;$i<4;$i++)
{
$sss.=$str[rand(0,3)];//隨機(jī)顯示漢字
}
//$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”:字體樣式,一般放在根目錄下;
復(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)于計(jì)時(shí)器
$string="abcdefghigklmnopqrstuvwxyz123456789";
for($i=0;$i<4;$i++)
{
$new_number.=$string[rand(0,strlen($string)-1)];//隨即的產(chǎn)生一個(gè)數(shù)組
}
第四種:
復(fù)制代碼 代碼如下:
for($i=0;$i<4;$i++)
{
$rand.=dechex(rand(1,15));//將十進(jìn)制轉(zhuǎn)化為十六進(jì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是定義頭的動(dòng)作,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) 畫點(diǎn)函數(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驗(yàn)證碼插入中文的方法。
隨機(jī)生成數(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)色板的時(shí)候,背景顏色
$te=imagecolorallocate($im,255,255,255);
imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
ob_clean();//PHP網(wǎng)頁中因?yàn)?要生成驗(yàn)證碼而出現(xiàn) 圖像"http://localhost/**.php"因其本身有錯(cuò)無法顯示
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)生隨機(jī)的顏色
imageline($im,rand(10,40),0,rand(10,40),20,$cg);
}
給圖片畫出干擾點(diǎn)的代碼:
復(fù)制代碼 代碼如下:
for($i=0;$i<80;$i++)//畫出多個(gè)點(diǎn)
{
imagesetpixel($im,rand(0,40),rand(0,20),$cg);
}
把文字寫入圖片代碼:
復(fù)制代碼 代碼如下:
$str=array('我','我','親','親');//存儲(chǔ)顯示的漢字
for($i=0;$i<4;$i++)
{
$sss.=$str[rand(0,3)];//隨機(jī)顯示漢字
}
//$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”:字體樣式,一般放在根目錄下;
相關(guān)文章
php高清晰度無損圖片壓縮功能的實(shí)現(xiàn)代碼
經(jīng)常會(huì)用到把上傳的大圖片壓縮,特別是體積,在微信等APP應(yīng)用上,也默認(rèn)都是有壓縮的,那么,怎么樣對(duì)圖片大幅度壓縮卻仍能保持較高的清晰度呢?接下來通過本文給大家分享php高清晰度無損圖片壓縮功能,感興趣的朋友一起看看吧2018-12-12PHP設(shè)計(jì)模式之解釋器(Interpreter)模式入門與應(yīng)用詳解
這篇文章主要介紹了PHP設(shè)計(jì)模式之解釋器(Interpreter)模式,結(jié)合實(shí)例形式詳細(xì)分析了PHP解釋器模式的概念、原理、基本應(yīng)用與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-12-12php array_walk 對(duì)數(shù)組中的每個(gè)元素應(yīng)用用戶自定義函數(shù)詳解
php array_walk函數(shù)將用戶自定義函數(shù)應(yīng)用到 array 數(shù)組中的每個(gè)單元,即使用用戶自定義函數(shù)對(duì)數(shù)組中的每個(gè)元素做回調(diào)處理。 本文章向大家講解array_walk函數(shù)的基本語法及使用實(shí)例,需要的朋友可以參考下2016-11-11win10下 php安裝seaslog擴(kuò)展的詳細(xì)步驟
這篇文章主要介紹了win10下 php安裝seaslog擴(kuò)展,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12thinkphp下MySQL數(shù)據(jù)庫讀寫分離代碼剖析
本篇文章主要介紹了thinkphp下MySQL數(shù)據(jù)庫讀寫分離代碼剖析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04php大小寫轉(zhuǎn)換函數(shù)(strtolower、strtoupper)用法介紹
strtoupper() 函數(shù)把字符串轉(zhuǎn)換為大寫而strtolower函數(shù): 該函數(shù)將傳入的字符串參數(shù)所有的字符都轉(zhuǎn)換成小寫,并以小定形式放回這個(gè)字符串。下面通過本文給大家分享php大小寫轉(zhuǎn)換函數(shù)(strtolower、strtoupper)用法,需要的朋友參考下吧2017-11-11