sgk/barcode-bundle

该软件包已被废弃且不再维护。作者建议使用https://github.com/tecnickcom/tc-lib-barcode软件包代替。

Symfony2/Symfony3 条码生成器软件包,带有 Twig 函数扩展

安装次数: 7,697

依赖项: 0

建议者: 0

安全性: 0

星级: 23

关注者: 2

分支: 4

类型:symfony-bundle

该软件包尚未发布任何版本,且可用的信息很少。


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

SGKBarcodeBundle 是一个 Symfony2 / Symfony3 条码生成器软件包。本 README 也可用法语(法语)和中文(中文)查看。

特性

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

SGKBarcodeBundle

安装

运行以下命令以添加 SGKBarcodeBundle:

// Symfony version >= 3.0
$ php composer.phar require sgk/barcode-bundle:~3.0

// Symfony version >= 2.7 and < 3.0, use ~2.0
// Symfony version < 2.7, use ~1.0

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

// Symfony version >= 3.0
"require": {
        "sgk/barcode-bundle": "~3.0"
    }

// Symfony version >= 2.7 and < 3.0, use ~2.0
// Symfony version < 2.7, use ~1.0

Composer 将将软件包安装到您的项目 vendor/sgk 目录。

然后,在内核中启用该软件包

<?php
// app/AppKernel.php

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

生成选项

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

选项 类型 必需 允许的值 描述
code 字符串 必需 您想要编码的内容
类型 字符串 必需 支持的类型 条码类型
格式 字符串 必需 html, svg, png 输出格式
width 整数 可选 单位宽度
height 整数 可选 单位高度
color 字符串(html, svg)/ 数组(png) 可选 HTML 颜色名称 / 数组(R, G, B) 条码颜色

二维条码的默认宽度和高度为 5, 5,一维条码为 2, 30。html, svg 的默认颜色为黑色,png 为数组(0, 0, 0)

通过服务使用

该软件包注册了一个服务: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 模板中使用

该软件包扩展了 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);

将条码保存到文件中

如您所见,该软件包在文件系统中不保存任何内容。但是,如果您想保留条码,没问题!

  • 保存为 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));

支持的条码类型

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

二维条码

类型 名称 示例(编码123456)
qrcode QR码
pdf417 PDF417
datamatrix 数据矩阵

一维条码

类型 符号 示例(编码123456)
c39 Code 39
c39+ Code 39 校验位
c39e Code 39 扩展
c39e+ Code 39 扩展校验位
c93 Code 93
s25 标准2of5
s25+ 标准2of5 校验位
i25 交错2of5
i25+ 交错2of5 校验位
c128 Code 128
c128a Code 128A
c128b Code 128B
c128c Code 128C
ean2 EAN 2
ean5 EAN 5
ean8 EAN 8
ean13 EAN 13
upca UPC-A
upce UPC-B
msi MSI
msi+ MSI 校验位
postnet POSTNET
planet PLANET
rms4cc RMS4CC
kix KIX-code
imb IM 码
codabar Codabar
code11 Code 11
pharma Pharmacode
pharma2t Pharmacode 双通道

要求

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

  • 条码需要GDImageMagick以在PHP 5.3中创建PNG文件。
  • 条码需要PHP bcmath扩展以支持智能邮件条码。

测试

执行单元测试

$ phpunit --coverage-text