racinepilote / qr-code
Endroid QR Code v2
Requires
- php: ^7.4||^8.0
- bacon/bacon-qr-code: ^2.0
Requires (Dev)
- ext-gd: *
- endroid/quality: dev-master
- khanamiryan/qrcode-detector-decoder: ^1.0.4
- setasign/fpdf: ^1.8.2
Suggests
- ext-gd: Enables you to write PNG images
- khanamiryan/qrcode-detector-decoder: Enables you to use the image validator
- roave/security-advisories: Makes sure package versions with known security issues are not installed
- setasign/fpdf: Enables you to use the PDF writer
- dev-master / 4.x-dev
- 4.5.1
- 4.5.0
- 4.4.1
- 4.4.0
- 4.3.5
- 4.3.4
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.x-dev
- 3.9.7
- 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.x-dev
- 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
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.3.4
- 1.2.4
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- dev-develop
This package is auto-updated.
Last update: 2024-09-13 03:14:06 UTC
README
由 endroid 提供
这个库可以帮助你快速生成QR码。利用bacon/bacon-qr-code生成矩阵,并使用khanamiryan/qrcode-detector-decoder来验证生成的QR码。进一步扩展了Twig扩展、生成路由、工厂和Symfony包,以便于安装和配置。提供了不同的写入器来生成PNG、SVG、EPS或二进制格式的QR码。
安装
使用 Composer 安装库。
$ composer require racinepilote/qr-code
使用:使用构建器
use Endroid\QrCode\Builder\Builder; use Endroid\QrCode\Encoding\Encoding; use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh; use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter; use Endroid\QrCode\Label\Font\NotoSans; use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Endroid\QrCode\Writer\PngWriter; $result = Builder::create() ->writer(new PngWriter()) ->writerOptions([]) ->data('Custom QR code contents') ->encoding(new Encoding('UTF-8')) ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) ->size(300) ->margin(10) ->roundBlockSizeMode(new RoundBlockSizeModeMargin()) ->logoPath(__DIR__.'/assets/symfony.png') ->labelText('This is the label') ->labelFont(new NotoSans(20)) ->labelAlignment(new LabelAlignmentCenter()) ->build();
使用:不使用构建器
use Endroid\QrCode\Color\Color; use Endroid\QrCode\Encoding\Encoding; use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow; use Endroid\QrCode\QrCode; use Endroid\QrCode\Label\Label; use Endroid\QrCode\Logo\Logo; use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Endroid\QrCode\Writer\PngWriter; $writer = new PngWriter(); // Create QR code $qrCode = QrCode::create('Data') ->setEncoding(new Encoding('UTF-8')) ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow()) ->setSize(300) ->setMargin(10) ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin()) ->setForegroundColor(new Color(0, 0, 0)) ->setBackgroundColor(new Color(255, 255, 255)); // Create generic logo $logo = Logo::create(__DIR__.'/assets/symfony.png') ->setResizeToWidth(50); // Create generic label $label = Label::create('Label') ->setTextColor(new Color(255, 0, 0)); $result = $writer->write($qrCode, $logo, $label);
使用:处理结果
// Directly output the QR code header('Content-Type: '.$result->getMimeType()); echo $result->getString(); // Save it to a file $result->saveToFile(__DIR__.'/qrcode.png'); // Generate a data URI to include image data inline (i.e. inside an <img> tag) $dataUri = $result->getDataUri();
写入器选项
use Endroid\QrCode\Writer\SvgWriter; $builder->setWriterOptions([SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true]);
编码
如果你使用条码扫描器,在读取生成的QR码时可能会遇到一些麻烦。根据你选择的编码,你将会有额外对应于ECI块的数据量。一些条码扫描器未编程为解释此信息块。为了确保最大兼容性,你可以使用默认编码ISO-8859-1,这是条码扫描器使用的默认编码(如果你的字符集支持,即没有中文字符)。
圆形块大小模式
默认情况下,块大小会被四舍五入以保证图像清晰并提高可读性。然而,还有一些其他四舍五入的变体。
margin (默认):如果需要,QR码的大小会缩小,但最终图像的大小保持不变,因为增加了额外的边距。enlarge:当出现四舍五入差异时,QR码和最终图像的大小会增大。shrink:当出现四舍五入差异时,QR码和最终图像的大小会缩小。none:不四舍五入。当块不需要四舍五入到像素时(例如SVG)可以使用此模式。
可读性
QR码的可读性主要取决于大小、输入长度、错误纠正级别以及任何可能覆盖图像的标志,因此如果你在寻找最佳结果,可以调整这些参数。你还可以检查$qrCode->getRoundBlockSize()值,以查看是否四舍五入块尺寸以使图像更加清晰和可读。请注意,四舍五入块大小可能导致额外的填充来补偿四舍五入差异。最后,如果可能,编码(默认UTF-8以支持大型字符集)可以设置为ISO-8859-1以改善可读性。
内置验证读取器
通过调用setValidateResult(true)可以启用内置验证读取器(默认禁用)。此验证读取器不能保证所有读取器都能读取QR码,但它可以帮助你提供最小质量级别。请注意,验证器可能消耗相当数量的额外资源,并且它应该仅在使用时单独安装。
Symfony集成
endroid/qr-code-bundle将QR码库集成到Symfony中,以提供更好的体验。
- 配置默认设置(如图像大小、默认写入器等)
- 支持多配置和通过别名注入
- 通过URL如/qr-code//Hello生成定义配置的QR码
- 使用专用函数直接从Twig生成QR码或URL
阅读更多组件文档
版本管理
版本号遵循MAJOR.MINOR.PATCH方案。将尽量减少向后兼容性破坏性更改,但请注意,这些更改可能会发生。在生产环境中锁定依赖项,并在升级时测试您的代码。
许可证
此组件受MIT许可证的约束。有关完整的版权和许可证信息,请参阅与源代码一起分发的LICENSE文件。