说明:php环境要求 >= 7.1
【安装】
1. 需要先安装 composer。【phpStudy 自带了composer】
2. 运行命令:
composer require endroid/qr-code
3. 下载成功,如下图:
4. 运行代码(注意改成自己的文件路径)
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Response\QrCodeResponse;
$qrCode = new QrCode('http://www.baidu.com');
$qrCode->setSize(300);
// Set advanced options
$qrCode->setWriterByName('png');
$qrCode->setMargin(10);
$qrCode->setEncoding('UTF-8');
$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH);
$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
$qrCode->setLabel('百度一下', 16, __DIR__.'/../library/Endroid/qrcode/assets/fonts/noto_sans.otf', LabelAlignment::CENTER);
$qrCode->setLogoPath(__DIR__.'/../library/Endroid/qrcode/assets/images/baidu.png');
$qrCode->setLogoWidth(80);
$qrCode->setRoundBlockSize(true);
$qrCode->setValidateResult(false);
// Directly output the QR code
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();
// Save it to a file
$qrCode->writeFile(__DIR__.'/qrcode.png');
// Create a response object
$response = new QrCodeResponse($qrCode);