在 Laravel 5 中使用 Simple QrCode 扩展包生成二维码

Laravel二维码

安装

下载

composer require simplesoftwareio/simple-qrcode 1.3.*

配置

config/app.php 下的 providers 添加:

SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class

config/app.php 下的 aliases 添加:

'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class

快速上手

在模板中使用

{!! QrCode::size(100)->generate(Request::url()); !!}

基本语法

基础使用

QrCode::generate('Content of qr code!');

如果想显示中文则使用:

QrCode::encoding('UTF-8')->generate('卓帅!');

generate() 方法用于生产二维码图片,默认情况下,返回一个 SVG 格式的图片字符串,在视图中我们可以可以通过如下方式将其打印出来:

{!! QrCode::generate('Content of qr code!'); !!}

generate() 方法还可以接受第二个参数用于指定文件名以及保存路径:

QrCode::generate('Content of qr code!', public_path('qrcode.svg'));

这样生成的文件会保存在 /public/qrcode.svg

修改保存格式

QrCode::format('png')->generate('Content of qr code!', public_path('qrcode.svg'));
format() 方法必须在所有其它格式化方法之前调用,这些格式化方法包括 sizecolorbackgroundColormargin

修改二维码的大小

QrCode::size(100)->generate('Content of qr code!');

修改二维码前景色

QrCode::size(100)->color(0,255,0)->generate('Content of qr code!');

修改二维码背景色

QrCode::size(100)->backgroundColor(0,0,128)->generate('Content of qr code!');

修改二维与容器的距离

QrCode::size(100)->backgroundColor(0,0,128)->margin(100)->generate('Content of qr code!');

编码修改

使用 encoding() , 上面已经有过例子了.

支持编码

字符编码
ISO-8859-1
ISO-8859-2
ISO-8859-3
ISO-8859-4
ISO-8859-5
ISO-8859-6
ISO-8859-7
ISO-8859-8
ISO-8859-9
ISO-8859-10
ISO-8859-11
ISO-8859-12
ISO-8859-13
ISO-8859-14
ISO-8859-15
ISO-8859-16
SHIFT-JIS
WINDOWS-1250
WINDOWS-1251
WINDOWS-1252
WINDOWS-1256
UTF-16BE
UTF-8
ASCII
GBK
EUC-KR
如果在生成二维码过程中报错:Could not encode content to xxxx

这就意味着你需要修改编码了。

更多使用方法请查看官方文档

https://github.com/Bacon/BaconQrCode

添加新评论