thinkphp6: 生成带logo的二维码(php 8.1.1 / thinkphp v6.0.10LTS )

一,qr-code代码库的地址:

https://github.com/endroid/qr-code

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,用composer下载:

1,下载
liuhongdi@lhdpc:/data/php/admapi$ composer require endroid/qr-code
2,查看已下载的qr-code库的版本:
liuhongdi@lhdpc:/data/php/admapi$ composer show endroid/qr-code
name     : endroid/qr-code
descrip. : Endroid QR Code
keywords : code, endroid, php, qr, qrcode
versions : * 4.4.7
type     : library
license  : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText
homepage : https://github.com/endroid/qr-code
source   : [git] https://github.com/endroid/qr-code.git d9f12af739c11c70fa1e8132dba8f849395e939b
dist     : [zip] https://api.github.com/repos/endroid/qr-code/zipball/d9f12af739c11c70fa1e8132dba8f849395e939b d9f12af739c11c70fa1e8132dba8f849395e939b
path     : /data/php/admapi/vendor/endroid/qr-code
names    : endroid/qr-code
 
support
issues : https://github.com/endroid/qr-code/issues
source : https://github.com/endroid/qr-code/tree/4.4.7
 
autoload
psr-4
Endroid\QrCode\ => src/
 
requires
bacon/bacon-qr-code ^2.0
php ^7.4||^8.0
 
requires (dev)
endroid/quality dev-master
ext-gd *
khanamiryan/qrcode-detector-decoder ^1.0.4
setasign/fpdf ^1.8.2
 
suggests
ext-gd Enables you to write PNG images
khanamiryan/qrcode-detector-decoder Enables you to use the image validator
roave/security-advisories Makes sure package versions with known security issues are not installed
setasign/fpdf Enables you to use the PDF writer
 
可以看到版本是4.4.7

三,php代码:

controller/Article.php
<?php
declare (strict_types = 1);
 
namespace app\controller;
 
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
 
class Article extends BaseController
{
   //生成pr code
    public function qr() {
        $writer = new PngWriter();
// Create QR code
        $qrCode = QrCode::create('https://www.iliangcang.com')
            ->setEncoding(new Encoding('UTF-8'))
            ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
            ->setSize(300)
            ->setMargin(10)
            ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
            ->setForegroundColor(new Color(0, 0, 0))
            ->setBackgroundColor(new Color(255, 255, 255));
 
// Create generic logo
        //logo是放在app/asset/目录下
        $logo = Logo::create(app()->getRootPath()."/app/asset/logo.png")
            ->setResizeToWidth(50);
 
// Create generic label
        $label = Label::create('良仓.生活美学')
            ->setTextColor(new Color(0, 0, 0));
 
        $result = $writer->write($qrCode, $logo, $label);
        header('Content-Type: '.$result->getMimeType());
        echo $result->getString();
        exit;
 
    }
}

四,测试效果

访问:
http://127.0.0.1:8000/article/qr
返回: thinkphp6: 生成带logo的二维码(php 8.1.1 / thinkphp v6.0.10LTS )

五,查看php和thinkphp的版本:

php:
liuhongdi@lhdpc:/data/php/admapi$ php --version
PHP 8.1.1 (cli) (built: Dec 20 2021 16:12:16) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies 
thinkphp:
liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/
liuhongdi@lhdpc:/data/php/admapi$ php think version
v6.0.10LTS 

 

上一篇:「JOISC 2012」星座(凸包)


下一篇:LOJ3040 「JOISC 2019 Day4」合并