codeages/qrcode

Endroid QR Code

安装: 12

依赖: 0

建议: 0

安全: 0

星星: 0

关注者: 4

分支: 724

2.2.4 2017-07-23 14:07 UTC

README

endroid创建

Latest Stable Version Build Status Total Downloads Monthly Downloads License

这个库可以帮助你轻松生成二维码,并提供一个用于项目快速集成的Symfony组件。

安装

使用Composer安装库。

$ composer require endroid/qrcode

使用

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Symfony\Component\HttpFoundation\Response;

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

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

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

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

// Create a response object
$response = new Response($qrCode->writeString(), Response::HTTP_OK, ['Content-Type' => $qrCode->getContentType()]);

QR Code

Symfony集成

在内核中注册Symfony组件。

// app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        // ...
        new Endroid\QrCode\Bundle\EndroidQrCodeBundle(),
    ];
}

组件使用工厂来创建二维码。默认参数可以通过配置进行覆盖。

endroid_qr_code:
    writer: 'png'
    size: 300
    margin: 10
    foreground_color: { r: 0, g: 0, b: 0 }
    background_color: { r: 255, g: 255, b: 255 }
    error_correction_level: low # low, medium, quartile or high
    encoding: UTF-8
    label: Scan the code
    label_font_size: 20
    label_alignment: left # left, center or right
    label_margin: { b: 20 }
    logo_path: '%kernel.root_dir%/../vendor/endroid/qrcode/assets/symfony.png'
    logo_width: 150
    validate_result: false # checks if the result is readable

二维码的可读性主要取决于大小、输入长度、错误纠正级别以及图像上的任何可能的徽标。validate_result选项使用内置的读取器来验证结果图像。这并不能保证所有读取器都能读取该代码,但可以帮助你提供最低质量标准。请注意,验证器可能会消耗大量资源,默认情况下是禁用的。

现在你可以从服务容器中检索工厂并创建二维码。例如,在你的控制器中,它可能看起来像这样。

$qrCode = $this->get('endroid.qrcode.factory')->create('QR Code', ['size' => 200]);

将以下部分添加到你的路由中,以便能够处理二维码URL。如果你只使用数据URI来显示图像,则可以跳过此步骤。

EndroidQrCodeBundle:
    resource: "@EndroidQrCodeBundle/Controller/"
    type:     annotation
    prefix:   /qrcode

安装和配置后,可以通过将二维码文本追加到URL后面,然后跟任何支持的扩展名来生成二维码。

Twig扩展

组件提供了一个用于生成二维码URL、路径或数据URI的Twig扩展。您可以使用这些函数的第二个参数来覆盖组件或通过配置定义的任何默认值。

<img src="{{ qrcode_path(message) }}" />
<img src="{{ qrcode_url(message, { writer: 'eps' }) }}" />
<img src="{{ qrcode_data_uri(message, { writer: 'svg', size: 150 }) }}" />

版本控制

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

许可

此组件遵循MIT许可。有关完整的版权和许可信息,请查看与源代码一起分发的LICENSE文件。