attestra / qr-code
Attestra QR Code
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.4.3
- 4.4.2
- 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
This package is auto-updated.
Last update: 2024-09-25 00:56:57 UTC
README
由 endroid 提供
这个库可以帮助您快速生成二维码。利用 bacon/bacon-qr-code 生成矩阵,并使用 khanamiryan/qrcode-detector-decoder 验证生成的二维码。进一步扩展了 Twig 扩展、生成路由、工厂和 Symfony 包,以便于安装和配置。提供了不同的写入器来生成 PNG、SVG、EPS 或二进制格式的二维码。
安装
使用 Composer 安装库。
$ composer require attestra/qr-code
用法:使用构建器
use Attestra\QrCode\Builder\Builder; use Attestra\QrCode\Encoding\Encoding; use Attestra\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh; use Attestra\QrCode\Label\Alignment\LabelAlignmentCenter; use Attestra\QrCode\Label\Font\NotoSans; use Attestra\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Attestra\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 Attestra\QrCode\Color\Color; use Attestra\QrCode\Encoding\Encoding; use Attestra\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow; use Attestra\QrCode\QrCode; use Attestra\QrCode\Label\Label; use Attestra\QrCode\Logo\Logo; use Attestra\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Attestra\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 Attestra\QrCode\Writer\SvgWriter; $builder->setWriterOptions([SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true]);
编码
如果您使用条形码扫描仪,在读取生成的二维码时可能会遇到一些麻烦。根据您选择的编码,您将会有额外对应于 ECI 块的数据量。一些条形码扫描仪并未编程以解释此信息块。为了确保最大兼容性,您可以使用条形码扫描仪默认使用的 ISO-8859-1
编码(如果您的字符集支持,即不存在中文字符)。
圆形块大小模式
默认情况下,块大小会四舍五入以保证清晰的图像并提高可读性。然而,还有一些其他四舍五入变体可供选择。
margin (默认)
:如果需要,二维码的大小会缩小,但由于增加了额外的边距,最终图像的大小保持不变。enlarge
:当出现四舍五入差异时,二维码和最终图像的大小会增大。shrink
:当出现四舍五入差异时,二维码和最终图像的大小会缩小。none
:不进行四舍五入。此模式可以在块不需要四舍五入到像素时使用(例如 SVG)。
可读性
二维码的可读性主要取决于大小、输入长度、错误纠正级别以及图像上的任何可能的徽标。因此,如果您在寻找最佳结果,您可以调整这些参数。您还可以检查 $qrCode->getRoundBlockSize() 值以查看是否对块尺寸进行了四舍五入,以便图像更加清晰和可读。请注意,四舍五入块尺寸可能会导致额外的填充来补偿四舍五入差异。最后,如果可能的话,编码(默认 UTF-8 以支持大型字符集)可以设置为 ISO-8859-1
以提高可读性。
内置验证读取器
您可以通过调用 setValidateResult(true) 启用内置验证读取器(默认禁用)。此验证读取器不能保证所有读取器都能读取二维码,但它可以帮助您提供最低质量水平。请注意,验证器可能会消耗大量额外的资源,并且它应该仅在您使用时单独安装。
版本控制
版本号遵循 MAJOR.MINOR.PATCH 机制。向后兼容性破坏性更改将保持在最低限度,但请注意,这些更改可能发生。在生产中锁定依赖关系,并在升级时测试您的代码。
许可证
此包受 MIT 许可证保护。有关完整的版权和许可证信息,请查看与源代码一起分发的 LICENSE 文件。