Developed with love by KnpLabs Hire us for your project!
45

QRcodeBundle

by AntoineTesson

Génération de QR code à la volée dans twig

YellowskiesQRcodeBundle

WORKS WITH PHP 7.2 AND SYMFONY 2 / 3 / 4 AND Twig 2.x

Latest Stable Version

YellowskiesQRcodeBundle is a Symfony2/3/4 Barcode Generator Bundle.

Features:

  1. Support 3 two-dimensional (2D) and 30 one-dimensional (1D) Barcode types
  2. Three output formats: HTML, PNG and SVG canvas
  3. Twig integration: you can simply use a extensional function of Twig in the template to generate Barcode
  4. Core of this bundle from this project tc-lib-barcode

Installation

Add YellowskiesQRcodeBundle by running the command:

$ php composer.phar require yellowskies/qr-code-bundle 

Or, add YellowskiesQRcodeBundle to your composer.json, then execute php composer.phar update
```json

"require": {
"yellowskies/qr-code-bundle": "1.2.7"
}


Composer will install the bundle to your project's vendor/yellowskies directory.

Then, Enable the bundle in the kernel:
```php
<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Skies\QRcodeBundle\SkiesQRcodeBundle(),
    );
}

Generate options

To generate one barcode, you have 5 options can be configured.

option type required allowed values description

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

The bundle registers one service: skies_barcode.generator which will allows you to generate barcode:

  • outpout html ```php $options = array( 'code' => 'string to encode', 'type' => 'c128', 'format' => 'html', );

$barcode =
$this->get('skies_barcode.generator')->generate($options);

return new Response($barcode);
```

  • outpout svg ```php $options = array( 'code' => 'string to encode', 'type' => 'qrcode', 'format' => 'svg', 'width' => 10, 'height' => 10, 'color' => 'green', );

$barcode =
$this->get('skies_barcode.generator')->generate($options);

return new Response($barcode);
```

  • outpout png ```php $options = array( 'code' => 'string to encode', 'type' => 'datamatrix', 'format' => 'png', 'width' => 10, 'height' => 10, 'color' => array(127, 127, 127), );

$barcode =
$this->get('skies_barcode.generator')->generate($options);

return new Response('');
```

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
    twig
    {{ barcode({code: 'string to encode', type: 'c128', format: 'html'}) }}

  • display svg
    twig
    {{ barcode({code: 'string to encode', type: 'qrcode', format: 'svg', width: 10, height: 10, color: 'green'}) }}

  • display png
    twig
    <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 Skies\SkiesQRcodeBundle\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 ```php $savePath = '/tmp/'; $fileName = 'sample.html';

file_put_contents($savePath.$fileName, $barcode);
```

  • save as svg ```php $savePath = '/tmp/'; $fileName = 'sample.svg';

file_put_contents($savePath.$fileName, $barcode);
```

  • save as png ```php $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)

1d barcodes

type Symbology Example(encode 123456)

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:
sh
$ phpunit --coverage-text

skies_barcode:        []
  • Merge pull request #9 from 0xf1a/master
    By web-flow, 27 days ago
  • Fix if no parameters provided
    By , 1 month ago
  • Enable parameters for 2D barcodes
    By 0xf1a, 1 month ago
  • Merge pull request #8 from web-monster/master
    By web-flow, 8 months ago
  • Fix for Twig 3.0 and Symfony 5.0+
    By , 8 months ago
  • Merge pull request #7 from web-monster/master
    By web-flow, 9 months ago
  • Fix: Array and string offset access syntax with curly braces is deprecated
    By web-monster, 10 months ago
  • Fixed "Bit shift by negative number" error when printing pdf417 bar code
    By AntoineTesson, 1 year ago
  • remove name deprecated QR-code-bundle / qr-code-bundle
    By AntoineTesson, 1 year ago
  • Merge pull request #4 from web-monster/master
    By web-flow, 1 year ago
  • Fix tree builder for symfony > 4.1
    By web-monster, 1 year ago
  • LCIENCE
    By AntoineTesson, 2 years ago
  • V1.2.5
    By AntoineTesson, 2 years ago
  • Update composer.json
    By web-flow, 2 years ago
  • Update composer.json
    By web-flow, 2 years ago
  • composer
    By AntoineTesson, 3 years ago
  • tested on twig 2 and sf 3.3
    By Antoine, 3 years ago
  • CI
    By AntoineTesson, 3 years ago
  • readme
    By AntoineTesson, 3 years ago
  • ci
    By AntoineTesson, 3 years ago
  • CI
    By AntoineTesson, 3 years ago
  • ci
    By AntoineTesson, 3 years ago
  • readme
    By AntoineTesson, 3 years ago
  • Modification du readme
    By AntoineTesson, 4 years ago
  • update readme
    By AntoineTesson, 4 years ago
  • update composer
    By AntoineTesson, 4 years ago
  • update composer
    By AntoineTesson, 4 years ago
  • .
    By AntoineTesson, 4 years ago
  • update readme
    By AntoineTesson, 4 years ago
  • Modification
    By AntoineTesson, 4 years ago