php實現(xiàn)自動生成驗證碼的實例講解
更新時間:2021年10月22日 08:32:33 作者:小妮淺淺
在本篇文章里小編給大家整理了一篇關于php實現(xiàn)自動生成驗證碼的實例講解內容,有需要的朋友們可以跟著學習下。
現(xiàn)在驗證碼在表單中的應用越來越多了,但是如果用js來實現(xiàn)總覺得不太方便,因此使用php來實現(xiàn)下,在此記錄下。
當然,我們也可以封裝成一個函數(shù),以后使用的時候也是很方便的,這里并未封裝,感興趣的小伙伴可以自己封裝下。
具體實現(xiàn)代碼:
新建一個cap_sz.php文件:
<?php
session_start(); //設置session,一定要在頂部
$width = 150; //設置圖片寬為300像素
$height = 40; //設置圖片高為40像素
$image = imagecreatetruecolor($width, $height); //設置驗證碼大小的函數(shù)
$bgcolor = imagecolorallocate($image, 255, 255, 255); //驗證碼顏色RGB為(255,255,255)#ffffff
imagefill($image, 0, 0, $bgcolor); //區(qū)域填充
$cap_code = "";
for($i=0;$i<4;$i++){
$fontsize = 7; //設置字體大小
$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);
//設置坐標
imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
$_SESSION['code'] = $cap_code; //存到session
//設置干擾元素,設置雪花點
for($i=0;$i<300;$i++){
$inputcolor = imagecolorallocate($image, rand(50,200), rand(20,200), rand(50,200));
//設置顏色,20-200顏色比數(shù)字淺,不干擾閱讀
imagesetpixel($image, rand(1,149), rand(1,39), $inputcolor);
//畫一個單一像素的元素
}
//增加干擾元素,設置橫線(先設置線的顏色,在設置橫線)
for ($i=0;$i<4;$i++) {
$linecolor = imagecolorallocate($image, rand(20,220), rand(20,220),rand(20,220));
//設置線的顏色
imageline($image, rand(1,149), rand(1,39), rand(1,299), rand(1,149), $linecolor);
}
//因為有些瀏覽器,訪問的content-type會是文本型(亂碼),所以我們需要設置成圖片的格式類型
header('Content-Type:image/png');
imagepng($image); //建立png函數(shù)
imagedestroy($image); //結束圖形函數(shù),消除$image
實例擴展:
<?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;//驗證碼字符數(shù)
private $height;
private $width;
private $path = '..\myfolder\fonts';
private $keys;
//構造函數(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)];
//使用字體路徑標識
$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);//偏轉角度±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坐標基本位置
$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:長度不正確.';
}else{
for($i=0;$i < $this->count;$i++){
//0 o O I l 1 校準,根據(jù)所選擇的字體確定是否需要手動校準
if($input[$i] != $this->keys[$i]['char']){
return 'SUCCESS.';
}else{
return 'ERROR:請輸入正確驗證碼.';
}
}
}
}
}
?>
到此這篇關于php實現(xiàn)自動生成驗證碼的實例講解的文章就介紹到這了,更多相關php實現(xiàn)自動生成驗證碼的方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PHP正則表達式函數(shù)preg_replace用法實例分析
這篇文章主要介紹了PHP正則表達式函數(shù)preg_replace用法,結合實例形式分析了PHP正則表達式函數(shù)preg_replace基本功能、參數(shù)描述與相關使用技巧,需要的朋友可以參考下2020-06-06

