thinkphp5 二维码生成 composer
进入extend文件夹
composer require endroid/qrcode
2.将二维码生成封装为服务

QrcodeServer.php代码如下:
<?php
/**
* Created by PhpStorm.
* User: cdjyj21
* Date: 2018/9/4
* Time: 11:57
*/ namespace app\services; //引入刚刚添加的composer安装的类 里面的自动加载类
use think\facade\App;
require_once App::getRootPath().'/extend/vendor/autoload.php';
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode; class QrcodeServer
{
protected $_qr;
protected $_encoding = 'UTF-8'; // 编码类型
protected $_size = ; // 二维码大小
protected $_logo = false; // 是否需要带logo的二维码
protected $_logo_url = ''; // logo图片路径
protected $_logo_size = ; // logo大小
protected $_title = false; // 是否需要二维码title
protected $_title_content = ''; // title内容
protected $_generate = 'display'; // display-直接显示 writefile-写入文件
protected $_file_name = './static/qrcode'; // 写入文件路径
const MARGIN = ; // 二维码内容相对于整张图片的外边距
const WRITE_NAME = 'png'; // 写入文件的后缀名
const FOREGROUND_COLOR = ['r' => , 'g' => , 'b' => , 'a' => ]; // 前景色
const BACKGROUND_COLOR = ['r' => , 'g' => , 'b' => , 'a' => ]; // 背景色 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(ErrorCorrectionLevel::HIGH); // 容错率
$this->_qr->setForegroundColor(self::FOREGROUND_COLOR);
$this->_qr->setBackgroundColor(self::BACKGROUND_COLOR);
// 是否需要title
if ($this->_title) {
$this->_qr->setLabel($this->_title_content, , 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, , 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' => ''];
}
} }
3.调用
例:
<?php
/**
* Created by PhpStorm.
* User: cdjyj21
* Date: 2018/9/4
* Time: 11:57
*/ namespace app\test\controller; use app\services\QrcodeServer; class Qrcode
{
/**
* 直接输出二维码 + 生成二维码图片文件
*/
public function create(){
// 自定义二维码配置
$config = [
'title' => true,
'title_content' => 'test',
'logo' => true,
'logo_url' => './logo.png',
'logo_size' => ,
]; // 直接输出
$qr_url = 'http://www.baidu.com?id=' . rand(, ); $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;
}
}
在浏览器中直接访问create()方法,会直接输出二维码,同时会在自定义保存目录下生成一张二维码图片。效果如下:

那这种直接输出的二维码怎么应用于项目中呢,一般都是直接写在html 中的 <img> 标签中,例如:
<img src="http://localhost:8080/projecttest/qrtest?id=1234" alt="这是一个二维码" />
这里罗列下我看懂的几个参数,也算给自己做个笔记吧。
| 参数名 | 描述 | 示例 |
|---|---|---|
| setText | 设置文本 | https://www.baidu.com |
| setSize | 设置二维码的大小,这里二维码应该是正方形的,所以相当于长宽 | 400 |
| setMargin | 设置二维码边距 | 10 |
| setForegroundColor | 设置前景色,RGB颜色 | array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0) |
| setBackgroundColor | 设置背景色,RGB颜色 | array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0) |
| setEncoding | 设置编码 | utf8 |
| setErrorCorrectionLevel | 设置错误级别(low / medium / quartile / high) | high |
| setLogoPath | 设置logo路径 | logo.png |
| setLogoWidth | 设置logo大小 | 50 |
| setLabel | 设置标签 | test |
| setLabelFontSize | 设置标签字体大小 | 16 |
| setLabelFontPath | 设置标签字体路径 | null |
| setLabelAlignment | 设置标签对齐方式(left / center / right) | center |
| setLabelMargin | 设置标签边距 | array('t' => 10,'r' => 20,'b' => 10,'l' => 30) |
| setWriterRegistry | ||
| setWriter | ||
| setWriterByName | 写入文件的后缀名 | png |
| setWriterByPath | ||
| setWriterByExtension | ||
| setValidateResult | ||
| writeString | ||
| writeDataUri | ||
| writeFile | 写入文件 | test.png |
thinkphp5 二维码生成 composer的更多相关文章
- thinkphp5 二维码生成
1.下载二维码插件Phpqrcode,地址 https://sourceforge.net/projects/phpqrcode/files/,把下载的文件夹放到\thinkphp\vendor下 2 ...
- 【thinkphp5.1】 endroid/qrcode 二维码生成
composer 链接: https://packagist.org/packages/endroid/qrcode 注意:PHP版本 要求 7.1+ 1. 使用 composer 安装 endroi ...
- PHP 自定义二维码生成
环境:PHP 7.*.* ,Composer 包管理工具.QrCode 效果如下: 使用 Composer 安装 QrCode QrCode 类库基于 php 的 GD 库,用于生成任意尺寸的二维码, ...
- [开源]C#二维码生成解析工具,可添加自定义Logo
二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能存更多的信息,也能表示更多的数据类型:比如:字 ...
- 聊聊 Web 项目二维码生成的最佳姿势
在设计和实现的过程之后,你永远不知道部署上去的程序会已什么样的姿势运行. 本篇借一次生成二维码逻辑的不同实现,阐述 Web 项目中二维码生成的正确姿势. 文中如有批量,欢迎各位看客老爷拍砖.试运行前5 ...
- .NET 二维码生成(ThoughtWorks.QRCode)
引用ThoughtWorks.QRCode.dll (源代码里有) 1.简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string n ...
- iOS开发 二维码生成
基于libqrencode的二维码生成 + (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size { ...
- PHP二维码生成的方法(google APi,PHP类库,libqrencode等)
原文地址: http://blog.csdn.net/liuxinmingcode/article/details/7910975 ================================== ...
- Android 二维码 生成和识别(附Demo源码)
今天讲一下目前移动领域很常用的技术——二维码.现在大街小巷.各大网站都有二维码的踪迹,不管是IOS. Android.WP都有相关支持的软件.之前我就想了解二维码是如何工作,最近因为工作需要使用相关技 ...
随机推荐
- The working copy is locked due to a previous error.
SVN报错: The working copy is locked due to a previous error. 不能更新项目代码... 解决方式: 解决:右键你的左侧管理目录中的相关目录,然后点 ...
- vue中如何使用event对象
原文地址 一.event 对象 (一)事件的 event 对象 你说你是搞前端的,那么你肯定就知道事件,知道事件,你就肯定知道 event 对象吧?各种的库.框架多少都有针对 event 对象的处理. ...
- java面试指导2019-9-10
11. Java 面向对象编程三大特性: 封装 继承 多态 封装 封装把一个对象的属性私有化,同时提供一些可以被外界访问的属性的方法,如果属性不想被外界访问,我们大可不必提供方法给外界访问.但是如果一 ...
- python手撸桌面计算器
网上有一些许多关于计算器的源码,但我似乎不太care 一直寻思着自己手撸一个才有意思,于是这就开始了 实现功能: 1.基本的两个数 +-x÷ 运算以及取反,百分之,平方等 2.支持连续运算 3.暂不支 ...
- selenium开发-C#/java/Python
背景:之前由于自己有编写CefSharp.WinForms 窗体版以及 接口化 WCF+CefSharp.WinForms的网站版本,但是由于某些原因,延伸出Selenium学习与研究 总结:sele ...
- Linux_目录基本操作_常用命令【详解】
Linux_常用命令 Linux文件系统的目录树结构:[Linux世界里一切皆文件]:说白了,就是文件和文件夹(目录)之间的操作. 普通用户kkb所有文件及文件夹,其实都位于root用户的 /home ...
- Sentinal LDK 加密狗的使用
公司的软件用了第三方的加密key,在代码里只是用了其中的一个功能:GetKeyInfo()判断电脑是否有插入u盾.现做简单的说明如下: 第一步.插入master key 到电脑,下载正式的hvc 授权 ...
- raspberrypi 树莓派 内核编译
相关版本信息 硬件:树莓派 2b 目标系统: linux 编译环境:ubuntu 14.4 32bit 用户路径:/home/hi/ 安装交叉编译链 cdmkdir pi/kernelcd pi/ke ...
- 链表中环的入口结点——牛客剑指offer
题目描述: 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目分析: 从上图中可以看出,环的入口结点和其他结点的区别:环的入口结点是有两个指针指向的,其他结点除了头结点都 ...
- 【sublime Text】sublime Text3安装可以使xml格式化的插件
应该有机会 ,会碰到需要格式化xml文件的情况. 例如,修改word转化的xml文件之后再将修改之后的xml文件转化为word文件. 但是,word另存的xml文件是没有格式的一片: 那怎么格式化 这 ...