markas / 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-20 19:04: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 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 个可配置的选项。
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));
支持的条码类型
请阅读 Wikipedia 页面 了解您应该选择哪种类型。
2d 条码
1d 条码
要求
如果有任何要求问题,请确保您已安装这些 PHP 扩展(在您的 phpinfo() 中检查)。
- 条码要求 PHP (GD) 和 ImageMagick 扩展以在 PHP 5.3 中创建 PNG
- 条码要求 PHP bcmath 扩展以支持智能邮件条码
测试
执行单元测试
$ phpunit --coverage-text
