hrvoj3e / endroid-qr-code
Endroid QR Code
Requires
- php: >=7.2
- bacon/bacon-qr-code: ^2.0
- khanamiryan/qrcode-detector-decoder: ^1.0.2
- myclabs/php-enum: ^1.5
- symfony/options-resolver: ^3.4||^4.4||^5.0
- symfony/property-access: ^3.4||^4.4||^5.0
Requires (Dev)
- endroid/quality: ^1.3.7
- setasign/fpdf: ^1.8
Suggests
- ext-gd: Required for generating PNG images
- roave/security-advisories: Avoids installation of package versions with vulnerabilities
- setasign/fpdf: Required to use the FPDF writer.
- symfony/security-checker: Checks your composer.lock for vulnerabilities
- dev-master / 3.x-dev
- 3.9.6
- 3.9.5
- 3.9.4
- 3.9.3
- 3.9.2
- 3.9.1
- 3.9.0
- 3.8.2
- 3.8.1
- 3.8.0
- 3.7.9
- 3.7.8
- 3.7.7
- 3.7.6
- 3.7.5
- 3.7.4
- 3.7.3
- 3.7.2
- 3.7.1
- 3.7.0
- 3.6.1
- 3.6.0
- 3.5.9
- 3.5.8
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.9
- 3.4.8
- 3.4.7
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.0
- 3.2.12
- 3.2.11
- 3.2.10
- 3.2.9
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.5.1
- 2.5.0
- 2.4.0
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.4
- dev-patch1
- dev-hotfix-remove-installer
This package is not auto-updated.
Last update: 2024-09-27 00:38:33 UTC
README
由endroid编写
这个库可以帮助您快速生成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();
编码
您可以选择以下值进行编码
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 文件。