chrisandchris / barcode-bundle
Symfony 3 & 4 条形码生成器Bundle,具有Twig函数扩展
Requires
- php: >=5.5.9|^7.0
- symfony/framework-bundle: ^3.0|^4.0
- symfony/options-resolver: ^3.0|^4.0
- symfony/twig-bundle: ^3.0|^4.0
Requires (Dev)
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2021-02-19 12:32:46 UTC
README
SGKBarcodeBundle是一个用于Symphony3 / 4的条形码生成器Bundle。
特性
- 支持3种二维(2D)和30种一维(1D)条形码类型
- 三种输出格式:HTML、PNG和SVG画布
- Twig集成:您可以在模板中简单地使用Twig的扩展函数来生成条形码
- 此bundle的核心来自此项目 tc-lib-barcode
安装
运行以下命令添加SGKBarcodeBundle:
$ php composer.phar require sgk/barcode-bundle:v2.0.0
或者,将SGKBarcodeBundle添加到您的composer.json中,然后执行php composer.phar update
"require": { "sgk/barcode-bundle": "v2.0.0" }
Composer会将bundle安装到您的项目的vendor/sgk目录。
然后,在kernel中启用该bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new SGK\BarcodeBundle\SGKBarcodeBundle(), ); }
如果您使用的是带有Flex的Symphony 4,则该行应该已自动添加到config/bundles.php
生成选项
要生成一个条形码,您可以配置5个选项。
| 选项 | 类型 | 必需 | 允许的值 | 描述 |
|---|---|---|---|---|
| code | 字符串 | 必需 | 您想要编码的内容 | |
| 类型 | 字符串 | 必需 | 支持的类型 | 条形码类型 |
| 格式 | 字符串 | 必需 | html, svg, png | 输出格式 |
| width | 整数 | 可选 | 单位宽度 | |
| height | 整数 | 可选 | 单位高度 | |
| color | html, svg的字符串 / png的数组 | 可选 | HTML颜色名称 / 数组(R, G, B) | 条形码颜色 |
2D条形码的默认宽度为5,5,1D条形码的默认宽度为2,30。html, svg的默认颜色为黑色,png的默认颜色为array(0, 0, 0)
通过服务使用
从版本3.4开始已弃用,并在Symphony 4中会中断。请参阅“无服务使用”。
此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的真正数据。在这里,我们使用数据URI方案在网页中直接显示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));
支持的条形码类型
请阅读维基百科页面了解您应该选择哪种类型。
二维条形码
| 类型 | 名称 | 示例(编码123456) |
|---|---|---|
| qrcode | QR码 | ![]() |
| pdf417 | PDF417 | ![]() |
| datamatrix | Data Matrix | ![]() |
一维条形码
| 类型 | 符号 | 示例(编码123456) |
|---|---|---|
| c39 | Code 39 | ![]() |
| c39+ | Code 39 CHECK_DIGIT | ![]() |
| c39e | Code 39 EXTENDED | ![]() |
| c39e+ | Code 39 EXTENDED CHECK_DIGIT | ![]() |
| c93 | Code 93 | ![]() |
| s25 | 标准2/5 | ![]() |
| s25+ | 标准2/5 CHECK_DIGIT | ![]() |
| i25 | 交错2/5 | ![]() |
| i25+ | 交错2/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条形码 | ![]() |
| codabar | Codabar | ![]() |
| code11 | Code 11 | ![]() |
| pharma | Pharmacode | ![]() |
| pharma2t | Pharmacode Two-Track | ![]() |
要求
如果有要求的问题,请确保您已安装这两个PHP扩展(在phpinfo()中检查)。
- Barcodes需要GD和ImageMagick来在PHP 5.3中创建PNG文件。
- Barcodes需要PHP bcmath扩展来生成智能邮件条形码。
测试
要执行单元测试,运行composer install然后
phpunit --coverage-text

































