hyperf2.1框架+endroid/qr-code组件包生成网址url二维码流程步骤

2023-06-01 00:00:00 框架 hyperf2 qr

qr-code组件包介绍:

他的库可以帮助你快速生成二维码。

利用bacon/bacon二维码生成矩阵,khanamiryan/qrcode检测器解码器验证生成的二维码。

通过细枝延伸、生成路线、工厂和Symfony捆绑包进一步扩展,便于安装和配置。

提供不同的编写器来生成二维码,如PNG、SVG、EPS或二进制格式。


我直接composer开始安装

[[email protected] ~]# cd /home/www/hyperf-skeleton/
[[email protected] hyperf-skeleton]# composer require endroid/qr-code

Using version ^4.1 for endroid/qr-code
./composer.json has been updated
Running composer update endroid/qr-code
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
  - Locking bacon/bacon-qr-code (2.0.3)
  - Locking dasprid/enum (1.0.3)
  - Locking endroid/qr-code (4.1.4)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Downloading dasprid/enum (1.0.3)
  - Downloading bacon/bacon-qr-code (2.0.3)
  - Downloading endroid/qr-code (4.1.4)
  - Installing dasprid/enum (1.0.3): Extracting archive
  - Installing bacon/bacon-qr-code (2.0.3): Extracting archive
  - Installing endroid/qr-code (4.1.4): Extracting archive
4 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> rm -rf runtime/container
73 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

[[email protected] hyperf-skeleton]# php bin/hyperf.php start

路由:

我这里通过注解定义路由 @AutoController


添加控制器

/hyperf/app/Controller/ToolsController.php

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;

//生成二维码
public function qrcode(RenderInterface $render)
{
        $tdk = [];
        $tdk['title'] = '在线生成网址url二维码,QR code-技术博客集';
        $tdk['keywords'] = '在线生成网址url二维码,QR code';
        $tdk['description'] = '在线生成网址url二维码,QR code,在线工具,技术博客集(blog.zongscan.com)是一个提供各类主流程序开发语言程序教程,合适自学编程/建站seo/框架/前端/后端技术的同学收藏,交流,分享,项目架构分享';
        
        $q = $this->request->input('url')??'https://www.zongscan.com/';
        
        $writer = new PngWriter();
        
        // Create QR code
        $qrCode = QrCode::create($q)
            ->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));
            
        $result = $writer->write($qrCode);
        
        // Directly output the QR code
        header('Content-Type: '.$result->getMimeType());
        
        // Save it to a file
        //$result->saveToFile(__DIR__.'/qrcode.png');
        
        // Generate a data URI to include image data inline (i.e. inside an  tag)
        $dataUri = $result->getDataUri();
        
        return $render->render('home/tools/qrcode', ['tdk' => $tdk ,'img' => $dataUri]);
}


view

/hyperf/storage/view/home/tools/qrcode.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{{$tdk['title']}}</title>
    <meta name="keywords" content="{{$tdk['keywords']}}" />
    <meta name="description" content="{{$tdk['description']}}" />
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="row clearfix">
                <p>使用方式:</p>
                <p>在网站链接后面?url=你自己要生成二维码的链接</p>
                <p>默认会生成https://www.zongscan.com/的二维码</p>
                <p>下载:</p>
                <p>自行下载该二维码,直接把鼠标移动到二维码图片上面,右击另存为下载</p>
                <img src="{{$img}}" alt="二维码">
            </div>
        </div>
    </div>
</div>
</body>
</html>


使用方式:

在网站链接后面?url=你自己要生成二维码的链接

默认会生成https://www.zongscan.com/的二维码  

下载:

自行下载该二维码,直接把鼠标移动到二维码图片上面,右击另存为.


效果图:

1.png

相关文章