idci/barcode-bundle

Symfony2 Barcode Generator Bundle 使用 Twig 函数扩展

安装次数: 4,191

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:symfony-bundle

v1.0.0 2016-06-06 12:43 UTC

This package is auto-updated.

Last update: 2024-08-24 20:26:56 UTC


README

IDCIBarcodeBundle 是您想要的 Symfony2 Barcode Generator Bundle!

特性

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

IDCIBarcodeBundle

安装

在您的 composer.json 文件中添加依赖项

"require": {
    ...
    "idci/barcode-bundle": "dev-master"
},

使用 composer 在您的应用程序中安装这些新依赖项

$ php composer.phar update

在您的应用程序内核中注册所需的包

<?php
// app/AppKernel.php

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

生成选项

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

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

通过服务使用

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

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

$barcode =
    $this->get('idci_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('idci_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('idci_barcode.generator')->generate($options);

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

对于格式 png,生成器返回 png 文件的 base64,因此您可以通过 base64_decode($barcode) 获取 png 的真实数据。在这里,我们使用 Data URI scheme 在网页中直接显示 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 IDCI\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));

支持的条码类型

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

二维条码

一维条码

要求

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

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

测试

要执行单元测试

$ phpunit --coverage-text