cibincasso / 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 not auto-updated.
Last update: 2024-09-14 19:42:08 UTC
README
CibincassoBarcodeBundle 是您想要的 Symfony2 条码生成器 Bundle!本 README 也有法语(法语)和中文(中文)版本。
功能
- 支持 3 种二维(2D)和 30 种一维(1D)条码类型
- 三种输出格式:HTML、PNG 和 SVG 画布
- Twig 集成:您可以在模板中使用 Twig 的扩展函数来简单地生成条码
- 本扩展的核心使用了以下 Laravel 项目:dinesh/barcode,如图
安装
运行以下命令添加 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 会将此包安装到您的项目 vendor/sgk 目录。
然后,在 kernel 中启用此 bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new SGK\BarcodeBundle\SGKBarcodeBundle(), ); }
生成选项
要生成一个条码,您可以配置 5 个选项。
默认情况下,二维条码的宽度和高度为 5, 5,一维条码为 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));
支持的条码类型
请阅读 维基百科页面 了解您应该选择哪种类型。
2D 条码
1D 条码
需求
如果需求存在问题,请确保您已安装这两个 PHP 扩展(在 phpinfo() 中检查)。
- 条码需要 GD 和 ImageMagick 来在 PHP 5.3 中创建 PNG。
- 条码需要 PHP bcmath 扩展以生成智能邮件条码。
测试
要执行单元测试
$ phpunit --coverage-text