ofcold/qrcode

此包的最新版本(1.0.2)没有可用的许可证信息。

易于使用的PHP二维码生成器。

1.0.2 2019-03-03 06:20 UTC

This package is auto-updated.

Last update: 2024-09-21 01:58:29 UTC


README

Ofcold QR code

易于使用的PHP二维码生成器。

简体中文文档

简介

Simple QrCode 是一个基于 Bacon/BaconQrCode 优秀作品的简单易用的 Laravel 框架包装器。我们创建了一个对 Laravel 用户熟悉且易于安装的接口。

示例

安装

composer require ofcold/qrcode

用法

注意!如果在链中使用,此方法必须最后调用。

默认情况下,生成将返回一个 SVG 图片字符串。您可以使用以下代码直接在 Laravel 的 Blade 系统中打印到现代浏览器

QrCode::generate($text);

// The generate method has a second parameter that will accept a filename and path to save the QrCode.
QrCode::generate($text, 'public/qrcode.svg');

基本

use Ofcold\QrCode\Facades\QrCode;
use Ofcold\QrCode\HexToRgb;

$text = 'Happy New Year';

// Default output svg format file.
QrCode::generate($text);

响应

浏览器直接输出图片。

使用 QRcodeResponse

use Ofcold\QrCode\QRcodeResponse

new QRcodeResponse(QrCode::generate($text))

使用 response()

$qr = QrCode::color('#ff0000');

response($qr->generate($text))->header('Content-Type', $qr->getContentType())

格式更改

QrCode 生成器默认设置为返回 SVG 图片。注意!必须在任何其他格式化选项(如大小、颜色、backgroundColor 和 margin)之前调用格式方法。

// Output other file formats.
QrCode::format('png')
	->generate($text);

大小更改

QrCode 生成器默认将返回以像素为单位创建二维码可能的最小大小。

// You can change the size of a QrCode by using the size method. Simply specify the size desired in pixels using the following syntax:
QrCode::size(400)
	->generate($text);

颜色更改

更改二维码的颜色时要小心。一些读取器很难读取彩色二维码。

// Change the QR code color, Supported rgb and hex
// All colors must be expressed in RGB (Red Green Blue). You can change the color of a QrCode by using the following: 255.255.0 OR #ff0000
QrCode::color(HexToRgb::make('#ff0000'))
	// color([255, 0, 0])
	->format('png')
	->generate($text);

边距更改

支持更改二维码周围的边距。只需使用以下语法指定所需的边距

QrCode::margin(100)->generate($text);

错误纠正

更改错误纠正级别很容易。只需使用以下语法:L = 1 M = 0 Q = 3 H = 1

QrCode::errorCorrection(1)->generate($text);

编码

更改用于构建二维码的字符编码。默认情况下,ISO-8859-1 被选为编码器。有关字符编码的更多信息,您可以将此更改为以下任何一种

QrCode::encoding('UTF-8')->generate($text);

字符编码器: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,

无法将内容编码为 ISO-8859-1 的错误意味着正在使用错误的字符编码类型。如果您不确定,我们建议使用 UTF-8。

合并

合并方法将一个图像合并到二维码上。这通常用于在二维码中放置徽标。

QrCode::merge($filename, $percentage, $absolute);

// Generates a QrCode with an image centered in the middle.
QrCode::format('png')->merge('path-to-image.png')->generate();

// Generates a QrCode with an image centered in the middle.  The inserted image takes up 20% of the QrCode.
QrCode::format('png')->merge('path-to-image.png', .2)->generate();

// Generates a QrCode with an image centered in the middle.  The inserted image takes up 20% of the QrCode.
QrCode::format('png')->merge('http://ofcold.com/icon.png', .2, true)->generate();

合并方法目前仅支持 PNG。如果 $absolute 设置为 false,则文件路径相对于应用程序基本路径。将此变量更改为 true 以使用绝对路径。在使用合并方法时,应使用高错误纠正级别以确保二维码仍然可读。我们建议使用 errorCorrection(1)。

合并二进制字符串

mergeString 方法可以用于实现与 merge 调用相同的效果,但它允许您提供文件字符串表示而不是文件路径。这对于使用 Storage 门面很有用。它的接口与 merge 调用非常相似。

QrCode::mergeString(Storage::get('path/to/image.png'), $percentage);

// Generates a QrCode with an image centered in the middle.
QrCode::format('png')->mergeString(Storage::get('path/to/image.png'))->generate();

// Generates a QrCode with an image centered in the middle.  The inserted image takes up 20% of the QrCode.
QrCode::format('png')->mergeString(Storage::get('path/to/image.png'), .2)->generate();

与正常的 merge 调用一样,目前仅支持 PNG。同样适用于错误纠正,建议使用高级别。

高级用法

所有方法都支持链式调用。必须最后调用generate方法,任何格式更改必须首先调用。例如,你可以运行以下任何一种

QrCode::size(250)->color(150,90,10)->backgroundColor(10,14,244)->generate($text);
QrCode::format('png')->size(399)->color(40,40,40)->generate($text);

辅助工具

什么是辅助工具?

辅助工具是创建二维码的简单方法,当扫描时,会使读取器执行某些操作。

比特币

这个辅助工具生成可扫描的比特币二维码,用于发送付款。 更多信息

QrCode::BTC($address, $amount);

//Sends a 0.334BTC payment to the address
QrCode::BTC('bitcoin address', 0.334);

// Sends a 0.334BTC payment to the address with some optional arguments
QrCode::size(500)->BTC('address', 0.0034, [
	'label' => 'my label',
	'message' => 'my message',
	'returnAddress' => 'https://www.returnaddress.com'
]);

电子邮件

这个辅助工具生成能够填写电子邮件地址、主题和正文的电子邮件二维码。

QrCode::email($to, $subject, $body);

// Fills in the to address
QrCode::email('foo@bar.com');

// Fills in the to address, subject, and body of an e-mail.
QrCode::email('foo@bar.com', 'This is the subject.', 'This is the message body.');

// Fills in just the subject and body of an e-mail.
QrCode::email(null, 'This is the subject.', 'This is the message body.');

地理定位

这个辅助工具生成手机可以读取的经纬度,并可以在Google Maps或类似的应用中打开位置。

QrCode::geo($latitude, $longitude);

QrCode::geo(37.822214, -122.481769);

电话号码

这个辅助工具生成一个可扫描的二维码,扫描后可以拨打一个号码。

QrCode::phoneNumber($phoneNumber);

QrCode::phoneNumber('18898726543');
QrCode::phoneNumber('1-800-MY-APPLE');

短信(文本消息)

这个辅助工具创建可以预先填写发送地址和消息正文的短信。

QrCode::SMS($phoneNumber, $message);

// Creates a text message with the number filled in.
QrCode::SMS('555-555-5555');

// Creates a text message with the number and message filled in.
QrCode::SMS('555-555-5555', 'Body of the message');

Wi-Fi

这个辅助工具创建可扫描的二维码,可以将手机连接到Wi-Fi网络。

QrCode::wiFi([
	'encryption' => 'WPA/WEP',
	'ssid' => 'SSID of the network',
	'password' => 'Password of the network',
	'hidden' => 'Whether the network is a hidden SSID or not.'
]);

// Connects to an open WiFi network.
QrCode::wiFi([
	'ssid' => 'Network Name',
]);

// Connects to an open, hidden WiFi network.
QrCode::wiFi([
	'ssid' => 'Network Name',
	'hidden' => 'true'
]);

// Connects to an secured, WiFi network.
QrCode::wiFi([
	'ssid' => 'Network Name',
	'encryption' => 'WPA',
	'password' => 'myPassword'
]);

许可证

本软件在MIT许可证下发布。