參考網址:
http://www.thinkphp.cn/topic/7749.html
http://blog.csdn.net/stxyc/article/details/44650971
php生成二維碼的幾種方式方法:http://blog.csdn.net/small_rice_/article/details/22922455
我用的是thinkphp框架
第一步:導入插件,把phpqrcode解壓到thinkphp專門擺放第三方插件的地方,我這個項目的是ThinkPHP\Library\Vendor,官方的是:ThinkPHP/Extend/Vendor
第二步:在common/function.php公用函數中添加一個調用函數:
function qrcode($text=""){ //include(realpath('D:\phpStudy\WWW\phpqrcode\qrlib.php')); //include(realpath('D:\phpStudy\WWW\phpqrcode\phpqrcode.php')); vendor("phpqrcode.phpqrcode"); $data = $text; $filename =dirname( dirname(dirname( dirname( __FILE__ ) )) )."\Uploads\qrcode\z" .md5($data).".png"; $errorCorrectionLevel = 'L'; $matrixPointSize = 2; $margin = 5; // import("Common.Extend.phppqrcode",'phpqrcode','.php'); QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, $margin); return "/Uploads/qrcode/z".md5($data).".png"; }
第三步:在控制器函數用調用該公共函數
//生成二維碼 $text= "http://".I( 'server.HTTP_HOST' ).'/Home/Login/register?id='.$userData['ue_id']; $erweima=qrcode($text); $this->assign('erweima',$erweima);
第四步:在模板文件中調用
<img src="{$erweima}" alt="">
效果圖:
注意的問題:
1.插件的引入一定要對,可以用include
2. $filename這個地址很重要,可能是物理地址。
3.return的地址和$filename有關聯,寫法又不一樣。
出問題的話基本都是這三個地址沒填好。