attestra/qr-code

Attestra QR Code

资助包维护!
endroid

4.4.3 2021-11-24 18:48 UTC

README

endroid 提供

Latest Stable Version Build Status Total Downloads Monthly Downloads License

这个库可以帮助您快速生成二维码。利用 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();

QR Code

写入器选项

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 文件。