nikitospush/barcode-bundle

Symfony 4, 5, 6 ,7 条码生成器 Bundle,具有 Twig 函数扩展

安装次数: 10,594

依赖者: 0

建议者: 0

安全性: 0

星标: 3

关注者: 0

分支: 18

类型:symfony-bundle

4.0.0 2023-03-10 13:29 UTC

This package is auto-updated.

Last update: 2024-09-09 13:11:47 UTC


README

SGKBarcodeBundle 是一个 Symfony3 / 4 条码生成器 Bundle。

特性

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

SGKBarcodeBundle

安装

运行以下命令添加 SGKBarcodeBundle

$ php composer.phar require sgk/barcode-bundle:v2.0.0

或者,将 SGKBarcodeBundle 添加到您的 composer.json 文件中,然后执行 php composer.phar update

"require": {
        "sgk/barcode-bundle": "v2.0.0"
    }

Composer 将将 Bundle 安装到您项目的 vendor/sgk 目录中。

然后,在 kernel 中启用此 Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new SGK\BarcodeBundle\SGKBarcodeBundle(),
    );
}

如果您使用的是带有 Flex 的 Symfony 4,则该行已自动添加到 config/bundles.php

生成选项

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

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

通过服务使用

此功能自 3.4 版本以来已弃用,并在 Symfony 4 中将中断。请参阅“无需服务的使用”。

此 Bundle 注册了一个服务:sgk_barcode.generator,它允许您生成条码

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

$barcode =
    $this->get('sgk_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('sgk_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('sgk_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 SGK\BarcodeBundle\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 扩展以用于智能邮件条码

测试

要执行单元测试,请先运行 composer install,然后执行

phpunit --coverage-text