ulabox/barcode-bundle

此包已被放弃且不再维护。未建议替代包。

Symfony2 Barcode Generator Bundle 与 Twig 函数扩展

安装次数: 35,508

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 1

开放问题: 0

类型:symfony-bundle

v4.0.4 2019-12-13 17:23 UTC

This package is auto-updated.

Last update: 2020-09-13 18:56:50 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

SGKBarcodeBundle 是您想要的 Symfony2 条形码生成 Bundle!此 README 还提供法语(法语)和中文(中文)版本。

功能

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

SGKBarcodeBundle

安装

通过运行以下命令添加 SGKBarcodeBundle

$ php composer.phar require ulabox/barcode-bundle:~3.0

Composer 将将此包安装到您的项目的 vendor/ulabox 目录。

然后,在内核中启用该包

<?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 Data Matrix

一维条码

类型 符号 示例(编码123456)
c39 Code 39
c39+ Code 39 CHECK_DIGIT
c39e Code 39 EXTENDED
c39e+ Code 39 EXTENDED CHECK_DIGIT
c93 Code 93
s25 标准2位5
s25+ 标准2位5 CHECK_DIGIT
i25 交错2位5
i25+ 交错2位5 CHECK_DIGIT
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 CHECK_DIGIT
postnet POSTNET
planet PLANET
rms4cc RMS4CC
kix KIX-code
imb IM条码
codabar Codabar
code11 Code 11
pharma Pharmacode
pharma2t Pharmacode Two-Track

需求

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

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

测试

要执行单元测试

$ phpunit --coverage-text