SGKBarcodeBundle
SGKBarcodeBundle is a Symfony3 / 4 Barcode Generator Bundle., (*1)
Features:, (*2)
- 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
, (*3)
Installation
Add SGKBarcodeBundle by running the command:, (*4)
$ php composer.phar require sgk/barcode-bundle:v2.0.0
Or, add SGKBarcodeBundle to your composer.json
, then execute php composer.phar update
, (*5)
"require": {
"sgk/barcode-bundle": "v2.0.0"
}
Composer will install the bundle to your project's vendor/sgk directory., (*6)
Then, Enable the bundle in the kernel:, (*7)
<?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
, (*8)
Generate options
To generate one barcode, you have 5 options can be configured., (*9)
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), (*10)
Usage by service
This is deprecated in version 3.4 and will break in Symfony 4. See 'usage without service' instead., (*11)
The bundle registers one service: sgk_barcode.generator
which will allows you to generate barcode:, (*12)
$options = array(
'code' => 'string to encode',
'type' => 'c128',
'format' => 'html',
);
$barcode =
$this->get('sgk_barcode.generator')->generate($options);
return new Response($barcode);
$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);
$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., (*13)
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., (*14)
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., (*15)
{{ barcode({code: 'string to encode', type: 'c128', format: 'html'}) }}
{{ barcode({code: 'string to encode', type: 'qrcode', format: 'svg', width: 10, height: 10, color: 'green'}) }}
<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!, (*16)
$savePath = '/tmp/';
$fileName = 'sample.html';
file_put_contents($savePath.$fileName, $barcode);
$savePath = '/tmp/';
$fileName = 'sample.svg';
file_put_contents($savePath.$fileName, $barcode);
$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., (*17)
2d barcodes
1d barcodes
Requirements
If there is some problem of requirements, make sure you have install these two extensions of PHP (check in your phpinfo())., (*18)
- 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:, (*19)
phpunit --coverage-text