composer 链接: https://packagist.org/packages/endroid/qrcode

注意:PHP版本 要求 7.1+

1. 使用 composer 安装 endroid/qrcode:

composer require endroid/qrcode

2 将二维码生成封装为服务

位置: /appliction/common/services/QrcodeService.php

3 QrcodeServer.php 代码如下

<?php
/**
* 二维码服务
* 依然范儿特西
*/ namespace app\common\services; use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode; class QrcodeServer
{
protected $_qr;
protected $_encoding = 'UTF-8'; // 编码类型
protected $_size = 300; // 二维码大小
protected $_logo = false; // 是否需要带logo的二维码
protected $_logo_url = ''; // logo图片路径
protected $_logo_size = 80; // logo大小
protected $_title = false; // 是否需要二维码title
protected $_title_content = ''; // title内容
protected $_generate = 'display'; // display-直接显示 writefile-写入文件
protected $_file_name = './'; // 写入文件路径
const MARGIN = 10; // 二维码内容相对于整张图片的外边距
const WRITE_NAME = 'png'; // 写入文件的后缀名
const FOREGROUND_COLOR = ['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]; // 前景色
const BACKGROUND_COLOR = ['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]; // 背景色 public function __construct($config) {
isset($config['generate']) && $this->_generate = $config['generate'];
isset($config['encoding']) && $this->_encoding = $config['encoding'];
isset($config['size']) && $this->_size = $config['size'];
isset($config['logo']) && $this->_logo = $config['logo'];
isset($config['logo_url']) && $this->_logo_url = $config['logo_url'];
isset($config['logo_size']) && $this->_logo_size = $config['logo_size'];
isset($config['title']) && $this->_title = $config['title'];
isset($config['title_content']) && $this->_title_content = $config['title_content'];
isset($config['file_name']) && $this->_file_name = $config['file_name'];
} /**
* 生成二维码
* @param $content //需要写入的内容
* @return array | page input
*/
public function createServer($content) {
$this->_qr = new QrCode($content);
$this->_qr->setSize($this->_size);
$this->_qr->setWriterByName(self::WRITE_NAME);
$this->_qr->setMargin(self::MARGIN);
$this->_qr->setEncoding($this->_encoding);
$this->_qr->setErrorCorrectionLevel(new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH)); // 容错率
$this->_qr->setForegroundColor(self::FOREGROUND_COLOR);
$this->_qr->setBackgroundColor(self::BACKGROUND_COLOR);
// 是否需要title
if ($this->_title) {
$this->_qr->setLabel($this->_title_content, 16, null, LabelAlignment::CENTER);
}
// 是否需要logo
if ($this->_logo) {
$this->_qr->setLogoPath($this->_logo_url);
$this->_qr->setLogoWidth($this->_logo_size);
} $this->_qr->setValidateResult(false); if ($this->_generate == 'display') {
// 展示二维码
// 前端调用 例:<img src="http://localhost/qr.php?url=base64_url_string">
header('Content-Type: ' . $this->_qr->getContentType());
return $this->_qr->writeString();
} else if ($this->_generate == 'writefile') {
// 写入文件
$file_name = $this->_file_name;
return $this->generateImg($file_name);
} else {
return ['success' => false, 'message' => 'the generate type not found', 'data' => ''];
}
} /**
* 生成文件
* @param $file_name //目录文件 例: /tmp
* @return array
*/
public function generateImg($file_name) {
$file_path = $file_name . DIRECTORY_SEPARATOR . uniqid() . '.' . self::WRITE_NAME; if (!file_exists($file_name)) {
mkdir($file_name, 0777, true);
} try {
$this->_qr->writeFile($file_path);
$data = [
'url' => $file_path,
'ext' => self::WRITE_NAME,
];
return ['success' => true, 'message' => 'write qrimg success', 'data' => $data];
} catch (\Exception $e) {
return ['success' => false, 'message' => $e->getMessage(), 'data' => ''];
}
} }

4 调用

<?php
/**
* 二维码
*/ namespace app\index\controller; use app\common\services\QrcodeServer;
class Qrcode
{
/**
* 直接输出二维码 + 生成二维码图片文件
*/
public function create(){
// 自定义二维码配置
$config = [
'title' => true,
'title_content' => '嗨,老范',
'logo' => true,
'logo_url' => './logo.png',
'logo_size' => 80,
]; // 直接输出
$qr_url = 'http://www.baidu.com?id=' . rand(1000, 9999); $qr_code = new QrcodeServer($config);
$qr_img = $qr_code->createServer($qr_url);
echo $qr_img; // 写入文件
$qr_url = '这是个测试二维码';
$file_name = './static/qrcode'; // 定义保存目录 $config['file_name'] = $file_name;
$config['generate'] = 'writefile'; $qr_code = new QrcodeServer($config);
$rs = $qr_code->createServer($qr_url);
print_r($rs); exit;
}
}

效果:

【thinkphp5.1】 endroid/qrcode 二维码生成的更多相关文章

  1. QRCode二维码生成方案及其在带LOGO型二维码中的应用(1)

    原文:QRCode二维码生成方案及其在带LOGO型二维码中的应用(1) 提要:很多公司为商业宣传之需,常将企业LOGO加入二维码中,但如果LOGO遮挡区域足够地大,二维码就变得无法识别.那么,有没有一 ...

  2. QRCode二维码生成方案及其在带LOGO型二维码中的应用(2)

    原文:QRCode二维码生成方案及其在带LOGO型二维码中的应用(2) 续前:QRCode二维码生成方案及其在带LOGO型二维码中的应用(1)  http://blog.csdn.net/johnsu ...

  3. python qrcode二维码生成与识别

    二维码 二维码生成 1.用法 https://github.com/lincolnloop/python-qrcode 2.使用 简单实用 import qrcode # 二维码内容 data = & ...

  4. PIL:Python Imaging Library(图像处理标准库)和Qrcode:二维码生成

    安装PIL Mac或Linux安装命令:sudo easy_install PIL 如果报错:fatal error: 'freetype/fterrors.h' file not found Mac ...

  5. QRCode二维码生成

    pom配置 <dependency> <groupId>com.github.cloudecho</groupId> <artifactId>qrcod ...

  6. Java实现二维码生成的方法

    1.支持QRcode.ZXing 二维码生成.解析: package com.thinkgem.jeesite.test; import com.google.zxing.BarcodeFormat; ...

  7. thinkphp5 二维码生成 composer

    进入extend文件夹 composer require endroid/qrcode 2.将二维码生成封装为服务 QrcodeServer.php代码如下: <?php /** * Creat ...

  8. .NET 二维码生成(ThoughtWorks.QRCode)

    引用ThoughtWorks.QRCode.dll (源代码里有) 1.简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string n ...

  9. .NET 二维码生成(ThoughtWorks.QRCode)【转发jiangys】

    .NET 二维码生成(ThoughtWorks.QRCode) 2015-06-21 22:19 by jiangys, 3790 阅读, 8 评论, 收藏, 编辑 引用ThoughtWorks.QR ...

随机推荐

  1. 学习Struts--Chap03:struts.xml常用配置(基础)

    1.package属性 name:包名 用来唯一的指定一个package.package可以扩展,当一个package扩展自 另一个package时该package会在本身配置的基础上加入扩展的pac ...

  2. python 计算程序运行耗时的好用的代码

    python 计算程序运行耗时的好用的代码: import time start=time.clock() sum=0 for i in range(50): sum=sum+i print(sum) ...

  3. 小甲鱼Python第十六讲课后习题--017函数

    函数的定义用def,函数名后要用冒号 函数的返回:函数中使用return   测试题: 0. 你有听说过DRY吗? DRY是指Don't Repeat Yourself ,特指在程序设计以及计算中避免 ...

  4. JAVA自学笔记20

    JAVA自学笔记20 1.递归: 1)方法定义中定义中调用方法本身的现象 2)要有出口,否则就是死递归 次数不能太多.否则内存将溢出 构造方法不能递归使用 //斐波那契数列:1,1,2,3,5,8,1 ...

  5. CSS魔法堂:那个被我们忽略的outline

    前言  在CSS魔法堂:改变单选框颜色就这么吹毛求疵!中我们要模拟原生单选框通过Tab键获得焦点的效果,这里涉及到一个常常被忽略的属性--outline,由于之前对其印象确实有些模糊,于是本文打算对其 ...

  6. SQLAlchemy——获取某列为空的数据

    session.query(StockAllInfo).filter( StockAllInfo.ts_code == tsCode and StockAllInfo.py_code==None).a ...

  7. Win10系统的SurfacePro4的触摸笔如何使用

    初次使用需要配对,微软的触摸笔是蓝牙配对的,打开平板的蓝牙,长按触摸笔后面的按钮,触摸笔会闪烁小灯,平板会提示配对准备已就绪   点击配对之后,提示已连接   可以按下触摸笔后面的按钮,一键打开One ...

  8. t-SNE 层次聚类

    https://zhuanlan.zhihu.com/p/28967965 https://haojunsui.github.io/2016/07/16/scipy-hac/

  9. 分析轮子(一)-ArrayList.java

    前言:之前也看过一些JDK源码,不过没有留下痕迹,经久年月就淡忘了,现在的时机也差不多了,想再看一次,并且记录下来自己的感想,于是从自己使用最多最熟悉的地方开始!并且看的过程中,我希望自己思考一下如下 ...

  10. org.apache.xerces.dom.ElementNSImpl.setUserData(Ljava/lang/String;Ljava/lang

    HTTP Status 500 - Handler processing failed; nested exception is java.lang.AbstractMethodError: org. ...