tp使用phpqrcode生成二维码

下载phpqrcode包

下载地址:http://phpqrcode.sourceforge.net/
在这里插入图片描述
在这里插入图片描述
下载完成后解压:
在这里插入图片描述
复制phpqrcode到tp项目里的vendor文件夹:
在这里插入图片描述

代码实现

<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
class Index extends Controller
{
    public function index()
    {
    	Vendor('phpqrcode.phpqrcode');
    	$url="https://www.wangchuangcode.cn";
    	$errorCorrectionLevel =2 ;//容错级别 
      	$matrixPointSize = 4;//生成图片大小 
     	//生成二维码图片 
      	$object = new \QRcode();
      	$object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2); 
      	die;
    }
}

实现效果

在这里插入图片描述

把生成的图片保存到指定目录下

<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
class Index extends Controller
{
    public function index()
    {
    	Vendor('phpqrcode.phpqrcode');
    	$url="https://www.wangchuangcode.cn";
    	$errorCorrectionLevel =intval(2) ;//容错级别 
      	$matrixPointSize = intval(4);//生成图片大小 
     	//生成二维码图片 
      	$object = new \QRcode();
      	$object->png($url, './images/qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); 
      	die;
    }
}

png方法的第二个参数:./images/qrcode.png,是public文件夹下的images文件夹,如果不写,默认是public文件夹下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42249896/article/details/87977967
今日推荐