hrvoj3e/endroid-qr-code

Endroid QR Code

资助包维护!
endroid

3.9.6 2020-11-27 14:30 UTC

README

endroid编写

Latest Stable Version Build Status Total Downloads Monthly Downloads License

这个库可以帮助您快速生成QR码。它使用bacon/bacon-qr-code来生成矩阵,并使用khanamiryan/qrcode-detector-decoder来验证生成的QR码。还通过Twig扩展、生成路由、工厂和Symfony包扩展,以方便安装和配置。

提供了不同的写入器来生成PNG、SVG、EPS、PDF或二进制格式的QR码。

安装

使用Composer安装库。

$ composer require endroid/qr-code

基本用法

use Endroid\QrCode\QrCode;

$qrCode = new QrCode('Life is too short to be generating QR codes');

header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();

高级用法

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Response\QrCodeResponse;

// Create a basic QR code
$qrCode = new QrCode('Life is too short to be generating QR codes');
$qrCode->setSize(300);
$qrCode->setMargin(10); 

// Set advanced options
$qrCode->setWriterByName('png');
$qrCode->setEncoding('UTF-8');
$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
$qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER());
$qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
$qrCode->setLogoSize(150, 200);
$qrCode->setValidateResult(false);

// Round block sizes to improve readability and make the blocks sharper in pixel based outputs (like png).
// There are three approaches:
$qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_MARGIN); // The size of the qr code is shrinked, if necessary, but the size of the final image remains unchanged due to additional margin being added (default)
$qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_ENLARGE); // The size of the qr code and the final image is enlarged, if necessary
$qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_SHRINK); // The size of the qr code and the final image is shrinked, if necessary

// Set additional writer options (SvgWriter example)
$qrCode->setWriterOptions(['exclude_xml_declaration' => true]);

// Directly output the QR code
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();

// Save it to a file
$qrCode->writeFile(__DIR__.'/qrcode.png');

// Generate a data URI to include image data inline (i.e. inside an <img> tag)
$dataUri = $qrCode->writeDataUri();

QR Code

编码

您可以选择以下值进行编码

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, US-ASCII, GBK EUC-KR

如果您使用条形码扫描仪,您在读取生成的QR码时可能会遇到一些麻烦。根据您选择的编码,您将会有相应数量的ECI块数据。一些条形码扫描仪没有编程来解释这个信息块。例如,UTF-8的ECI块是000026,所以上面的例子将产生:\000026Life is too short to be generating QR codes。为了确保最大兼容性,您可以使用默认编码ISO-8859-1,这是条形码扫描仪使用的默认编码。

可读性

QR码的可读性主要取决于大小、输入长度、错误纠正级别以及图像上的任何可能的水印,因此如果您正在寻找最佳结果,您可以调整这些参数。您还可以检查$qrCode->getRoundBlockSize()值以查看是否已对块尺寸进行四舍五入,以便图像更加清晰和可读。请注意,四舍五入块尺寸可能会导致额外的填充以补偿四舍五入的差异。

内置验证读取器

您可以通过调用setValidateResult(true)启用内置验证读取器(默认禁用)。此验证读取器不能保证所有读取器都能读取QR码,但它可以帮助您提供最小质量级别。

请注意,验证器可能会消耗大量额外的资源。

Symfony集成

endroid/qr-code-bundle将QR码库集成到Symfony中,以提供更好的体验。

  • 配置默认设置(如图像大小、默认写入器等)
  • 通过工厂服务从任何地方快速生成QR码
  • 直接通过输入URL(如/qr-code/<text>.png?size=300)生成QR码
  • 使用专用函数通过Twig直接生成QR码或URL

阅读bundle文档以获取更多信息。

版本控制

版本号遵循 MAJOR.MINOR.PATCH 机制。向后兼容性的破坏性更改将尽可能减少,但请注意,这些更改可能会发生。在生产环境中锁定您的依赖关系,并在升级时测试您的代码。

许可证

本软件包采用 MIT 许可证。有关完整的版权和许可证信息,请查看与源代码一同分发的 LICENSE 文件。