nikitospush/barcode-bundle
Composer 安装命令:
composer require nikitospush/barcode-bundle
包简介
Symfony 4, 5, 6 ,7 Barcode Generator Bundle with Twig function extension
README 文档
README
SGKBarcodeBundle is a Symfony3 / 4 Barcode Generator Bundle.
Features:
- Support 3 two-dimensional (2D) and 30 one-dimensional (1D) Barcode types
- Three output formats: HTML, PNG and SVG canvas
- Twig integration: you can simply use a extensional function of Twig in the template to generate Barcode
- Core of this bundle from this project tc-lib-barcode
Installation
Add SGKBarcodeBundle by running the command:
$ php composer.phar require sgk/barcode-bundle:v2.0.0
Or, add SGKBarcodeBundle to your composer.json, then execute php composer.phar update
"require": { "sgk/barcode-bundle": "v2.0.0" }
Composer will install the bundle to your project's vendor/sgk directory.
Then, Enable the bundle in the kernel:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new SGK\BarcodeBundle\SGKBarcodeBundle(), ); }
If you are using Symfony 4 with Flex the line should have automatically been added to config/bundles.php
Generate options
To generate one barcode, you have 5 options can be configured.
| option | type | required | allowed values | description |
|---|---|---|---|---|
| code | string | required | what you want encoded | |
| type | string | required | Supported Types | type of barcode |
| format | string | required | html, svg, png | output format |
| width | integer | optional | width of unit | |
| height | integer | optional | height of unit | |
| color | string for html, svg / array for png | optional | HTML Color Names / array(R, G, B) | barcode color |
Default width and height for 2D barcode are 5, 5, for 1D are 2, 30. Default color for html, svg is black, for png is array(0, 0, 0)
Usage by service
This is deprecated in version 3.4 and will break in Symfony 4. See 'usage without service' instead.
The bundle registers one service: sgk_barcode.generator which will allows you to generate barcode:
- outpout html
$options = array( 'code' => 'string to encode', 'type' => 'c128', 'format' => 'html', ); $barcode = $this->get('sgk_barcode.generator')->generate($options); return new Response($barcode);
- outpout 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);
- outpout 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.'" />');
For format png, the generator return the based64 of png file, so you can get the real data of png by
base64_decode($barcode). Here we use Data URI scheme to direct display the png in webpage.
Usage in Twig template
This bundle extend one function of Twig: barcode which you can simply use it to generate barcode in the twig template.
barcode use the same options, only different thing is your need pass a Twig array (it looks really like Json, but it isn't) in the function.
- display html
{{ barcode({code: 'string to encode', type: 'c128', format: 'html'}) }}
- display svg
{{ barcode({code: 'string to encode', type: 'qrcode', format: 'svg', width: 10, height: 10, color: 'green'}) }}
- display png
<img src="data:image/png;base64, {{ barcode({code: 'string to encode', type: 'datamatrix', format: 'png', width: 10, height: 10, color: [127, 127, 127]}) }} " />
Usage without service
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);
Save Barcode in file
As you can see, the Bundle save nothing on the file system, But if you want to keep the barcode, No problem!
- save as html
$savePath = '/tmp/'; $fileName = 'sample.html'; file_put_contents($savePath.$fileName, $barcode);
- save as svg
$savePath = '/tmp/'; $fileName = 'sample.svg'; file_put_contents($savePath.$fileName, $barcode);
- save as png
$savePath = '/tmp/'; $fileName = 'sample.png'; file_put_contents($savePath.$fileName, base64_decode($barcode));
Supported Barcode Types
Please read Wikipedia page to know which type you should choice.
2d barcodes
| type | Name | Example(encode 123456) |
|---|---|---|
| qrcode | QR code | ![]() |
| pdf417 | PDF417 | ![]() |
| datamatrix | Data Matrix | ![]() |
1d barcodes
| type | Symbology | Example(encode 123456) |
|---|---|---|
| c39 | Code 39 | ![]() |
| c39+ | Code 39 CHECK_DIGIT | ![]() |
| c39e | Code 39 EXTENDED | ![]() |
| c39e+ | Code 39 EXTENDED CHECK_DIGIT | ![]() |
| c93 | Code 93 | ![]() |
| s25 | Standard 2 of 5 | ![]() |
| s25+ | Standard 2 of 5 CHECK_DIGIT | ![]() |
| i25 | Interleaved 2 of 5 | ![]() |
| i25+ | Interleaved 2 of 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 barcode | ![]() |
| codabar | Codabar | ![]() |
| code11 | Code 11 | ![]() |
| pharma | Pharmacode | ![]() |
| pharma2t | Pharmacode Two-Track | ![]() |
Requirements
If there is some problem of requirements, make sure you have install these two extensions of PHP (check in your phpinfo()).
- Barcodes requires GD and ImageMagick to create PNGs in PHP 5.3.
- Barcodes requires PHP bcmath extension for Intelligent Mail barcodes
Tests
To execute unit tests, composer install and run:
phpunit --coverage-text
nikitospush/barcode-bundle 适用场景与选型建议
nikitospush/barcode-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.94k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 03 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「generator」 「twig」 「bundle」 「barcode」 「qr code」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nikitospush/barcode-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nikitospush/barcode-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nikitospush/barcode-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
The bundle for easy using json-rpc api on your project
统计信息
- 总下载量: 19.94k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2020-03-04

































