php實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼的實(shí)例講解
現(xiàn)在驗(yàn)證碼在表單中的應(yīng)用越來越多了,但是如果用js來實(shí)現(xiàn)總覺得不太方便,因此使用php來實(shí)現(xiàn)下,在此記錄下。
當(dāng)然,我們也可以封裝成一個(gè)函數(shù),以后使用的時(shí)候也是很方便的,這里并未封裝,感興趣的小伙伴可以自己封裝下。
具體實(shí)現(xiàn)代碼:
新建一個(gè)cap_sz.php文件:
<?php
session_start(); //設(shè)置session,一定要在頂部
$width = 150; //設(shè)置圖片寬為300像素
$height = 40; //設(shè)置圖片高為40像素
$image = imagecreatetruecolor($width, $height); //設(shè)置驗(yàn)證碼大小的函數(shù)
$bgcolor = imagecolorallocate($image, 255, 255, 255); //驗(yàn)證碼顏色RGB為(255,255,255)#ffffff
imagefill($image, 0, 0, $bgcolor); //區(qū)域填充
$cap_code = "";
for($i=0;$i<4;$i++){
$fontsize = 7; //設(shè)置字體大小
$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
//數(shù)字越大,顏色越淺,這里是深顏色0-120
$fontcontent = rand(0,9);
$cap_code.=$fontcontent; //.=連續(xù)定義變量
$x = ($i*150/4)+rand(5,10);
$y = rand(5,10);
//設(shè)置坐標(biāo)
imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
$_SESSION['code'] = $cap_code; //存到session
//設(shè)置干擾元素,設(shè)置雪花點(diǎn)
for($i=0;$i<300;$i++){
$inputcolor = imagecolorallocate($image, rand(50,200), rand(20,200), rand(50,200));
//設(shè)置顏色,20-200顏色比數(shù)字淺,不干擾閱讀
imagesetpixel($image, rand(1,149), rand(1,39), $inputcolor);
//畫一個(gè)單一像素的元素
}
//增加干擾元素,設(shè)置橫線(先設(shè)置線的顏色,在設(shè)置橫線)
for ($i=0;$i<4;$i++) {
$linecolor = imagecolorallocate($image, rand(20,220), rand(20,220),rand(20,220));
//設(shè)置線的顏色
imageline($image, rand(1,149), rand(1,39), rand(1,299), rand(1,149), $linecolor);
}
//因?yàn)橛行g覽器,訪問的content-type會(huì)是文本型(亂碼),所以我們需要設(shè)置成圖片的格式類型
header('Content-Type:image/png');
imagepng($image); //建立png函數(shù)
imagedestroy($image); //結(jié)束圖形函數(shù),消除$image
實(shí)例擴(kuò)展:
<?php
$iC = new idCode(5,60,30);
$iC->createPNG();
class idCode{
private $words = array('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','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',
'0','1','2','3','4','5','6','7','8','9');
private $fonts;
private $count;//驗(yàn)證碼字符數(shù)
private $height;
private $width;
private $path = '..\myfolder\fonts';
private $keys;
//構(gòu)造函數(shù)
public function __construct($count,$width,$height){
$this->count = $count;
$this->getFonts();
$this->height = $height;
$this->width = $width;
}
private function getFonts(){
$dir = dir($this->path);
while(false !== ($file = $dir->read())){
if($file != '.' && $file != '..'){
$this->fonts[count($this->fonts)] = basename($file);
}
}
$dir->close();
}
private function createKeys(){
for($i = 0;$i < $this->count;$i++){
$this->keys[$i]['char'] = $this->words[rand(0,count($this->words)-1)];
//使用字體路徑標(biāo)識(shí)
$this->keys[$i]['filename'] = $this->path.'\\'.$this->fonts[rand(0,count($this->fonts)-1)];
}
}
public function createPNG(){
$this->createKeys();
//創(chuàng)建畫布以及顏色塊兒
$bg = imagecreatetruecolor($this->width + 10*2,$this->height + 3*2);//兩邊留10px空白,上下3px
$grey = imagecolorallocate($bg,155,155,155);
$blue = imagecolorallocate($bg,0x00,0x00,0xff);
//填充背景
imagefill($bg,0,0,$grey);
//添加字符
$pwidth = $this->width/$this->count;
$x;$y;
for($i = 0;$i < $this->count;$i++){
$rotation = rand(-40,40);//偏轉(zhuǎn)角度±40°
$fontsize = 33;
$width_txt;
$height_txt;
do{
$fontsize--;
$bbox = imagettfbbox($fontsize,$rotation,$this->keys[$i]['filename'],$this->keys[$i]['char']);
$width_txt = $bbox[2] - $bbox[0];//x 0 2 4 6,y1 3 5 7;左下,右下,右上,左上
$height_txt = $bbox[7] - $bbox[1];
}while($fontsize > 8 && ($height_txt > $this->height || $width_txt > $pwidth));
$fontcolor = imagecolorallocate($bg,rand(0,255),rand(0,255),rand(0,255));
$x = 8 + $pwidth*$i + $pwidth/2 - $width_txt/2;//x坐標(biāo)基本位置
$y = $this->height/2 - $height_txt/2;
imagettftext($bg,$fontsize,$rotation,$x,$y,$fontcolor,$this->keys[$i]['filename'],$this->keys[$i]['char']);
}
//繪制干擾線
//根據(jù)字體酌情增加干擾線
imageline($bg,0,15,40,10,$blue);
//圖像輸出頭文件
header('Content-type:image/png');
//輸出png圖像
imagepng($bg);
//清除緩存資源
imagedestroy($bg);
}
public function checkKeys($input){
if(count($input)!=$this->count){
return 'ERROR:長(zhǎng)度不正確.';
}else{
for($i=0;$i < $this->count;$i++){
//0 o O I l 1 校準(zhǔn),根據(jù)所選擇的字體確定是否需要手動(dòng)校準(zhǔn)
if($input[$i] != $this->keys[$i]['char']){
return 'SUCCESS.';
}else{
return 'ERROR:請(qǐng)輸入正確驗(yàn)證碼.';
}
}
}
}
}
?>
到此這篇關(guān)于php實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼的實(shí)例講解的文章就介紹到這了,更多相關(guān)php實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼的方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php編程實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁版計(jì)算器功能示例
這篇文章主要介紹了php編程實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁版計(jì)算器功能,涉及php簡(jiǎn)單表單操作與數(shù)值運(yùn)算相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-04-04
理清PHP在Linxu下執(zhí)行時(shí)的文件權(quán)限方法
下面小編就為大家?guī)硪黄砬錚HP在Linxu下執(zhí)行時(shí)的文件權(quán)限方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
比file_get_contents穩(wěn)定的curl_get_contents分享
相信使用過file_get_contents函數(shù)的朋友都知道,當(dāng)獲取的$url訪問不了時(shí),會(huì)導(dǎo)致頁面漫長(zhǎng)的等待,甚至還能導(dǎo)致PHP進(jìn)程占用CPU達(dá)100%,因此這個(gè)函數(shù)就誕生了2012-01-01
PHP執(zhí)行外部命令的函數(shù):exec(), system(), passthru(), sh
system()輸出并返回最后一行shell結(jié)果。exec()不輸出結(jié)果,返回最后一行shell結(jié)果,所有結(jié)果可以保存到一個(gè)返回的數(shù)組里面。passthru()只調(diào)用命令,把命令的運(yùn)行結(jié)果原樣地直接輸出到標(biāo)準(zhǔn)輸出設(shè)備上...2023-05-05
php下通過curl抓取yahoo boss 搜索結(jié)果的實(shí)現(xiàn)代碼
php下通過curl抓取yahoo boss 搜索結(jié)果的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-06-06
php+mysqli實(shí)現(xiàn)批量執(zhí)行插入、更新及刪除數(shù)據(jù)的方法
這篇文章主要介紹了php+mysqli實(shí)現(xiàn)批量執(zhí)行插入、更新及刪除數(shù)據(jù)的方法,主要涉及multi_query()函數(shù)的用法,需要的朋友可以參考下2015-01-01

