php網(wǎng)站制作如何生成二維碼

2023-03-06    分類: 網(wǎng)站制作

第一種方法找網(wǎng)上的api接口

比如:聯(lián)圖
品味二維碼:

草料:
其他都可以在網(wǎng)上搜到很多,但是這些api可能過一段時間會失效,以前就遇到過這樣的情況,這就接口不可控性太大,所以我們要介紹第二種方法
二、用插件生成二維碼
這里使用的是PHPQRcode新建函數(shù)(這里用的thinkPhp框架)
/**
*生成二維碼
*@param$save_path二維碼保存路徑
*@paramstring$qr_data手機掃描后要跳轉(zhuǎn)的網(wǎng)址
*@paramstring$qr_level默認(rèn)糾錯比例分為L、M、Q、H四個等級,H代表最高糾錯能力
*@paramint$qr_size二維碼圖大小,1-10可選,數(shù)字越大圖片尺寸越大
*@paramstring$save_prefix圖片名稱前綴
*@returnbool|string
*/
functioncreateQRcode($save_path,$qr_data='PHPQRCode:)',$qr_level='L',$qr_size=4,$save_prefix='qrcode'){
if(!isset($save_path))return'';
//設(shè)置生成png圖片的路徑
$PNG_TEMP_DIR=&$save_path;
//導(dǎo)入二維碼核心程序
vendor('PHPQRcode.class#phpqrcode');//PHPQRcode是文件夾名字,class#phpqrcode就代表class.phpqrcode.php文件名
//檢測并創(chuàng)建生成文件夾
if(!file_exists($PNG_TEMP_DIR)){
mkdir($PNG_TEMP_DIR);
}
$filename=$PNG_TEMP_DIR.'test.png';
$errorCorrectionLevel='L';
if(isset($qr_level)&&in_array($qr_level,array('L','M','Q','H'))){
$errorCorrectionLevel=&$qr_level;
}
$matrixPointSize=4;
if(isset($qr_size)){
$matrixPointSize=&min(max((int)$qr_size,1),10);
}
if(isset($qr_data)){
if(trim($qr_data)==''){
die('datacannotbeempty!');
}
//生成文件名文件路徑+圖片名字前綴+md5(名稱)+.png
$filename=$PNG_TEMP_DIR.$save_prefix.md5($qr_data.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
//開始生成
QRcode::png($qr_data,$filename,$errorCorrectionLevel,$matrixPointSize,2);
}else{
//默認(rèn)生成
QRcode::png('PHPQRCode:)',$filename,$errorCorrectionLevel,$matrixPointSize,2);
}
if(file_exists($PNG_TEMP_DIR.basename($filename))){
returnbasename($filename);
}else{
returnFALSE;
}
}
函數(shù)調(diào)用
$save_path='Qrcode/';//圖片存儲的絕對路徑
$qr_data=C('PROTOCOL').$_SERVER['SERVER_NAME']..'/Share/member.html?str='.$member['mstr'];
$qr_level='H';
$qr_size='10';
$save_prefix='ZETA';
$filename=createQRcode($save_path,$qr_data,$qr_level,$qr_size,$save_prefix);
if($filename){
$pic=.'/'.$save_path.$filename;
}
$this->pic=$pic;
如何在利用PHPQRcode生成的二維碼中間加上logo圖片呢
$logo=.$member['picture'];//logo的圖片地址
import("Org.Util.File");
$file=new\File($logo);
$logo=$file->getRealFile();
$file=new\File($pic);
$QR=$file->getRealFile();;//二維碼圖片地址
if($logo!==FALSE){
$QR=imagecreatefromstring(file_get_contents($QR));
$logo=imagecreatefromstring(file_get_contents($logo));
$QR_width=imagesx($QR);
$QR_height=imagesy($QR);
$logo_width=imagesx($logo);
$logo_height=imagesy($logo);
$logo_qr_width=$QR_width/5;
$scale=$logo_width/$logo_qr_width;
$logo_qr_height=$logo_height/$scale;
$from_width=($QR_width-$logo_qr_width)/2;
imagecopyresampled($QR,$logo,$from_width,$from_width,0,0,$logo_qr_width,$logo_qr_height,$logo_width,$logo_height);
}
imagepng($QR,$save_path.$filename);//跟logo合并之后的地址

網(wǎng)頁標(biāo)題:php網(wǎng)站制作如何生成二維碼
地址分享:http://www.bm7419.com/news25/241975.html

網(wǎng)站建設(shè)、網(wǎng)絡(luò)推廣公司-創(chuàng)新互聯(lián),是專注品牌與效果的網(wǎng)站制作,網(wǎng)絡(luò)營銷seo公司;服務(wù)項目有網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作