Giter Club home page Giter Club logo

qrcodebundle's Introduction

YellowskiesQRcodeBundle

WORKS WITH PHP >= 7.1 AND SYMFONY 4/ 5 / 6 AND Twig 2.x

Latest Stable Version

YellowskiesQRcodeBundle is a Symfony4/5/6 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

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

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

Then, Enable the bundle in the kernel:

<?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
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

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

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

$barcode =
    $this->get('skies_barcode.generator')->generate($options);
    
return new Response($barcode);
  • output svg
$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);
  • output png
$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('<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 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
$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:

$ phpunit --coverage-text

qrcodebundle's People

Contributors

0xf1a avatar antoinetesson avatar chris8934 avatar web-monster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

qrcodebundle's Issues

Composer update to Symfony 4.4 - PHP Fatal error: Class 'Twig_Extension' not found in vendor/yellowskies/qr-code-bundle/Twig/Extensions/Barcode.php on line 14

Hi your work is great, is very useful.

I tried to upgrade a Symfony project from version 4.3 to 4.4 and I got this error:

PHP Fatal error: Class 'Twig_Extension' not found in /home/carlos/www/gdocumentBack_sf44/vendor/yellowskies/qr-code-bundle/Twig/Extensions/Barcode.php on line 14
Symfony\Component\Debug\Exception\ClassNotFoundException^ {#7689
#message: """
Attempted to load class "Twig_Extension" from the global namespace.\n
Did you forget a "use" statement?
"""
#code: 0
#file: "./vendor/yellowskies/qr-code-bundle/Twig/Extensions/Barcode.php"
#line: 14
#severity: E_ERROR
}

Thanks for your help.

How to dimension a 2d code in a very precise size

Hi and thank you for this bundle

I would have liked to know how to dimension the width and height of a 2d code very precisely. Currently for the datamatrix for example the smallest size is 14x14 when you specify 1x1 and then it is multiples of 14.

Is it possible to solve this problem by encoding in base64 in an img tag for svg?

Barcode in not working

Hi:

I have a proyect hosted in AWS and barcode is no generating a QR. This is the code:


 {%  set qr = barcode({code: url('dataCard',{'card':card.url}), type: 'qrcode', format: 'png', color: [0, 0, 0]})  %}
    <div class="codigo-qr"> <img src="{{ 'data:image/png;base64,' ~ qr }}" /></div>

In my local machine, is working Ok and generating the image.

Syntax error, unexpected '?', expecting variable (T_VARIABLE)

Info

php version:
PHP 7.1.16 (cli) (built: Mar 31 2018 02:59:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

QRcodeBundle version: 1.2

Issue on

#2 /var/www/html/vendor/yellowskies/qr-code-bundle/Generator/Generator.php(50): spl_autoload_call('Symfony\\Compone...')

$this->resolver = new OptionsResolver();

My Code

$options = array(
    'code'   => '/app/vehicles/1/public/'
    'type'   => 'datamatrix',
    'format' => 'png',
    'width'  => 10,
    'height' => 10,
    'color'  => array(127, 127, 127)
);

$generator = new Generator();
$barcode = $generator->generate($options);

$qrData = array('qr' => $barcode, 'plate' => $model->plate);

image

Symfony 5.4 Deprecation Warning

I am not sure if there are more depcreations right now.
Should be fixed for Support for Symfony 6.x

Method "Symfony\Component\Config\Definition\ConfigurationInterface::getConfigTreeBuilder()" might add "TreeBuilder" as a native return type declaration in the future. Do the same in implementation "Skies\QRcodeBundle\DependencyInjection\Configuration" now to avoid errors or add an explicit @return annotation to suppress this message.

Support Symfony 4

Hi,

would it be possible to bump the dependencies to support symfony 4?

Thx and merry christmas :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.