zl程序教程

您现在的位置是:首页 >  其他

当前栏目

在TP中生成二维码图片出现乱码

2023-03-07 09:44:42 时间

1. 在TP生成二维码图片出现乱码问题

composer require endroid/qr-code
<?php
namespace app\index\controller;

use Endroid\QrCode\QrCode;

class Index
{
public function index()
{
$qrCode = new QrCode('https://itqaq.com');
header('Content-Type: ' . $qrCode->getContentType());
$qrCode->setSize(200);//设置图片宽高, 默认320x320
$qrCode->setMargin(10);// 二维码上下右左四个方向的外边距
echo $qrCode->writeString();//显示二维码
}
}

2. 通过百度找到解决方案

在输出二维码之前添加以下内容

ob_end_clean(); //清空缓冲区并将缓冲区关闭, 但不会输出内容
<?php
namespace app\index\controller;

use Endroid\QrCode\QrCode;

class Index
{
    public function index()
    {
        $qrCode = new QrCode('https://itqaq.com');
        header('Content-Type: ' . $qrCode->getContentType());
        $qrCode->setSize(200);//设置图片宽高, 默认320x320
        $qrCode->setMargin(10);// 二维码上下右左四个方向的外边距
        ob_end_clean(); //解决TP二维码输出乱码
        echo $qrCode->writeString();//显示二维码
    }
}