mwsimple / barcode-bundle
Symfony2 条码生成器 Bundle,带有 Twig 函数扩展
Requires
- php: >=5.3.2
- symfony/framework-bundle: ~2.3
- symfony/options-resolver: ~2.1
- symfony/twig-bundle: ~2.3
Requires (Dev)
- phpunit/phpunit: 4.5.0
This package is auto-updated.
Last update: 2024-09-10 12:00:16 UTC
README
SGKBarcodeBundle 是您想要的 Symfony2 条码生成器 Bundle!此 README 也提供法语版本(法语)和中文版本(中文)。
特性
- 支持 3 种二维(2D)和 30 种一维(1D)条码类型
- 三种输出格式:HTML、PNG 和 SVG 画布
- Twig 集成:您可以在模板中简单地使用 Twig 扩展函数来生成条码
- 此 Bundle 的核心使用此 laravel 项目:dinesh/barcode
安装
运行以下命令添加 SGKBarcodeBundle:
// Symfony version < 2.7 $ php composer.phar require mwsimple/barcode-bundle:~1.0 // Symfony version >= 2.7 $ php composer.phar require mwsimple/barcode-bundle:~2.0
或者,将 SGKBarcodeBundle 添加到您的 composer.json
中,然后执行 php composer.phar update
// Symfony version < 2.7 "require": { "mwsimple/barcode-bundle": "~1.0" } // Symfony version >= 2.7 "require": { "mwsimple/barcode-bundle": "~2.0" }
Composer 将将此 Bundle 安装到您项目的 vendor/sgk 目录。
然后,在内核中启用此 Bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new SGK\BarcodeBundle\SGKBarcodeBundle(), ); }
生成选项
要生成一个条码,您可以配置 5 个选项。
2D 条码的默认宽度和高度为 5, 5,1D 为 2, 30。HTML 和 SVG 的默认颜色为黑色,PNG 为数组(0, 0, 0)
通过服务使用
此 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 的实际数据。这里我们使用 Data URI scheme 直接在网页中显示 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() 中检查)。
- 条码要求 PHP 的 GD 和 ImageMagick 扩展来在 PHP 5.3 中创建 PNG
- 条码需要 PHP 的 bcmath 扩展来创建智能邮件条码
测试
要执行单元测试
$ phpunit --coverage-text