Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Latest commit

 

History

History
254 lines (197 loc) · 9.92 KB

README_zh-CN.md

File metadata and controls

254 lines (197 loc) · 9.92 KB

SGKBarcodeBundle

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

SGKBarcodeBundle 是一个用于生成条形码和二维码的 Symfony2 Bundle。 这份 README 还有英语版(English)和法语版(Français)。

特点:

  1. 支持 3 种二维码和 30 种条形码类型
  2. 可输出三种不同格式:HTML,PNG 和 SVG canvas
  3. 集成 Twig:你可以方便的使用一个 Twig 扩展函数,直接在模板中进行调用来显示条形码和二维码
  4. 这个 Bundle 移植于这个 Laravel 项目:dinesh/barcode

SGKBarcodeBundle

安装

执行这条指令来安装 SGKBarcodeBundle:

// Symfony version < 2.7
$ php composer.phar require sgk/barcode-bundle:~1.0

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

或者,把 SGKBarcodeBundle 依赖添加到你的 composer.json 中,然后执行 php composer.phar update

// Symfony version < 2.7
"require": {
        "sgk/barcode-bundle": "~1.0"
    }

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

Composer 会把 Bundle 安装到你项目下的 vendor/sgk 文件夹中。

然后在 kernel 中注册这个 Bundle :

<?php
// app/AppKernel.php

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

生成参数

共有 5 个参数可用于配置来生成条形码和二维码:

参数 类型 是否必填 允许的值 描述
code string 必填 要进行编码的信息
type string 必填 支持的条码类型 条形码和二维码的类型
format string 必填 html, svg, png 输出格式
width integer 可选 单元宽度
height integer 可选 单元高度
color html和svg为string / png为array 可选 HTML Color Names / array(R, G, B) 颜色

二维条码的默认宽高为5,5。一维条码的默认宽高为2,30。 html,svg输出格式的默认颜色为 black。png输出格式的默认颜色为 array(0, 0, 0)。

用例:通过 service 使用

这个 Bundle 注册了一个 service : sgk_barcode.generator ,你可以通过 Symfony 的服务容器来获得它并生成条码:

  • 输出 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 图片的 based64 数据,你可以通过base64_decode($barcode)来获得原始数据。在这里我们利用 Data URI scheme 来将base64数据直接内嵌并显示到网页上。

用例:在 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]}) }}
" />

用例:不通过 service 使用

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));

支持的条形码和二维码类型

阅读维基百科页面来了解你应该用哪一种条码。

二维码

type Name Example(encode 123456)
qrcode QR code
pdf417 PDF417
datamatrix Data Matrix

条形码

type Symbology Example(encode 123456)
c39 Code 39
c39+ Code 39 CHECK_DIGIT
c39e Code 39 EXTENDED
c39e+ Code 39 EXTENDED CHECK_DIGIT
c93 Code 93
s25 Standard 2 of 5
s25+ Standard 2 of 5 CHECK_DIGIT
i25 Interleaved 2 of 5
i25+ Interleaved 2 of 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 barcode
codabar Codabar
code11 Code 11
pharma Pharmacode
pharma2t Pharmacode Two-Track

Requirements

如果你遇到了依赖问题,请检查你的 phpinfo(),查看你是否安装了以下两个 PHP 扩展。(一般情况下都是已默认安装的)

  • 需要 GDImageMagick 来生成 PNGs in PHP 5.3。
  • 需要 PHP bcmath 来生成 Intelligent Mail barcodes(IMB格式)。

测试

执行单元测试:

$ phpunit --coverage-text