yellowskies/qr-code-bundle

Symfony2Barcode 生成器 Bundle,带有 Twig 函数扩展

安装量: 558 184

依赖关系: 0

建议者: 0

安全: 0

星级: 32

关注者: 3

分支: 9

开放问题: 1

类型:symfony-bundle

1.2.11 2023-02-27 11:54 UTC

This package is auto-updated.

Last update: 2024-08-27 14:57:44 UTC


README

与 PHP >= 7.1 和 Symfony 4/5/6 以及 Twig 2.x 兼容

Latest Stable Version

YellowskiesQRcodeBundle 是一个 Symfony4/5/6 条形码生成器 Bundle。

特性

  1. 支持 3 种二维(2D)和 30 种一维(1D)条形码类型
  2. 三种输出格式:HTML、PNG 和 SVG 画布
  3. Twig 集成:您可以在模板中使用 Twig 扩展函数来简单地生成条形码
  4. 此 Bundle 的核心来自此项目 tc-lib-barcode

安装

运行以下命令添加 YellowskiesQRcodeBundle

$ php composer.phar require yellowskies/qr-code-bundle 

或者,将 YellowskiesQRcodeBundle 添加到您的 composer.json,然后执行 php composer.phar update

"require": {
        "yellowskies/qr-code-bundle": "1.2.10"
    }

Composer 会将 Bundle 安装到您的项目 vendor/yellowskies 目录。

然后,在内核中启用该 Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Skies\QRcodeBundle\SkiesQRcodeBundle(),
    );
}

生成选项

要生成一个条形码,您有 5 个可配置的选项。

2D 条形码的默认宽度和高度为 5, 5,1D 为 2, 30。HTML 和 SVG 的默认颜色为黑色,PNG 为数组(0, 0, 0)

通过服务使用

该 Bundle 注册了一个服务:skies_barcode.generator,这将允许您生成条形码

  • 输出 HTML
$options = array(
    'code'   => 'string to encode',
    'type'   => 'c128',
    'format' => 'html',
);

$barcode =
    $this->get('skies_barcode.generator')->generate($options);
    
return new Response($barcode);
  • 输出 SVG
$options = array(
    'code'   => 'string to encode',
    'type'   => 'qrcode',
    'format' => 'svg',
    'width'  => 10,
    'height' => 10,
    'color'  => 'green',
);

$barcode =
    $this->get('skies_barcode.generator')->generate($options);
    
return new Response($barcode);
  • 输出 PNG
$options = array(
    'code'   => 'string to encode',
    'type'   => 'datamatrix',
    'format' => 'png',
    'width'  => 10,
    'height' => 10,
    'color'  => array(127, 127, 127),
);

$barcode =
    $this->get('skies_barcode.generator')->generate($options);

return new Response('<img src="data:image/png;base64,'.$barcode.'" />');

对于 PNG 格式,生成器返回 PNG 文件的 base64,因此您可以通过 base64_decode($barcode) 获取 PNG 的真实数据。在这里,我们使用 数据 URI 方案 直接在网页中显示 PNG。

在 Twig 模板中使用

此 Bundle 扩展了一个 Twig 函数:barcode,您可以直接在 twig 模板中使用它来生成条形码。

barcode 使用相同的选项,只是您需要将一个 Twig 数组(它看起来非常像 Json,但不是)传递给函数。

  • 显示 HTML
{{ barcode({code: 'string to encode', type: 'c128', format: 'html'}) }}
  • 显示 SVG
{{ barcode({code: 'string to encode', type: 'qrcode', format: 'svg', width: 10, height: 10, color: 'green'}) }}
  • 显示 PNG
<img src="data:image/png;base64,
{{ barcode({code: 'string to encode', type: 'datamatrix', format: 'png', width: 10, height: 10, color: [127, 127, 127]}) }}
" />

不使用服务使用

use Skies\SkiesQRcodeBundle\Generator\Generator;
//...
$options = array(
    'code'   => 'string to encode',
    'type'   => 'qrcode',
    'format' => 'html',
);

$generator = new Generator();
$barcode = $generator->generate($options);

return new Response($barcode);

将条形码保存到文件

如您所见,该 Bundle 在文件系统上不保存任何内容,但如果您想保留条形码,没问题!

  • 保存为 HTML
$savePath = '/tmp/';
$fileName = 'sample.html';

file_put_contents($savePath.$fileName, $barcode);
  • 保存为 SVG
$savePath = '/tmp/';
$fileName = 'sample.svg';

file_put_contents($savePath.$fileName, $barcode);
  • 保存为 PNG
$savePath = '/tmp/';
$fileName = 'sample.png';

file_put_contents($savePath.$fileName, base64_decode($barcode));

支持的条形码类型

请阅读维基百科页面,了解您应该选择哪种类型。

二维条形码

一维条形码

需求

如果存在需求问题,请确保您已安装这两个 PHP 扩展(在您的 phpinfo() 中检查)。

  • 条形码需要 GDImageMagick 来在 PHP 5.3 中创建 PNG。
  • 条形码需要 PHP bcmath 扩展来创建智能邮件条形码

测试

执行单元测试

$ phpunit --coverage-text