QR Code Generator

Overview

QR Code

By endroid

Latest Stable Version Build Status Total Downloads Monthly Downloads License

If you like my work you can show appreciation by sponsoring via Github Sponsors or send me some ADA at DdzFFzCqrhszSwiFWMBQomfv6mkDM59kWFbeX991yJWWtMKnWrp38CuRN5Un5yDjyQLVx7eSyqaMZ62XEgunwGsTFRmh6K2nf6BxMmYw. Also if you need any help implementing this library just let me know and I'll be glad to help.

This library helps you generate QR codes in a jiffy. Makes use of bacon/bacon-qr-code to generate the matrix and khanamiryan/qrcode-detector-decoder for validating generated QR codes. Further extended with Twig extensions, generation routes, a factory and a Symfony bundle for easy installation and configuration.

Different writers are provided to generate the QR code as PNG, SVG, EPS or in binary format.

Installation

Use Composer to install the library.

$ composer require endroid/qr-code

Usage: using the builder

use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
use Endroid\QrCode\Label\Font\NotoSans;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;

$result = Builder::create()
    ->writer(new PngWriter())
    ->writerOptions([])
    ->data('Custom QR code contents')
    ->encoding(new Encoding('UTF-8'))
    ->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
    ->size(300)
    ->margin(10)
    ->roundBlockSizeMode(new RoundBlockSizeModeMargin())
    ->logoPath(__DIR__.'/assets/symfony.png')
    ->labelText('This is the label')
    ->labelFont(new NotoSans(20))
    ->labelAlignment(new LabelAlignmentCenter())
    ->build();

Usage: without using the builder

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;

$writer = new PngWriter();

// Create QR code
$qrCode = QrCode::create('Data')
    ->setEncoding(new Encoding('UTF-8'))
    ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
    ->setSize(300)
    ->setMargin(10)
    ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
    ->setForegroundColor(new Color(0, 0, 0))
    ->setBackgroundColor(new Color(255, 255, 255));

// Create generic logo
$logo = Logo::create(__DIR__.'/assets/symfony.png')
    ->setResizeToWidth(50);

// Create generic label
$label = Label::create('Label')
    ->setTextColor(new Color(255, 0, 0));

$result = $writer->write($qrCode, $logo, $label);

Usage: working with results

// Directly output the QR code
header('Content-Type: '.$result->getMimeType());
echo $result->getString();

// Save it to a file
$result->saveToFile(__DIR__.'/qrcode.png');

// Generate a data URI to include image data inline (i.e. inside an  tag)
$dataUri = $result->getDataUri();

QR Code

Writer options

use Endroid\QrCode\Writer\SvgWriter;

$builder->setWriterOptions([SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true]);

Encoding

If you use a barcode scanner you can have some troubles while reading the generated QR codes. Depending on the encoding you chose you will have an extra amount of data corresponding to the ECI block. Some barcode scanner are not programmed to interpret this block of information. To ensure a maximum compatibility you can use the ISO-8859-1 encoding that is the default encoding used by barcode scanners (if your character set supports it, i.e. no Chinese characters are present).

Round block size mode

By default block sizes are rounded to guarantee sharp images and improve readability. However some other rounding variants are available.

  • margin (default): the size of the QR code is shrunk if necessary but the size of the final image remains unchanged due to additional margin being added.
  • enlarge: the size of the QR code and the final image are enlarged when rounding differences occur.
  • shrink: the size of the QR code and the final image are shrunk when rounding differences occur.
  • none: No rounding. This mode can be used when blocks don't need to be rounded to pixels (for instance SVG).

Readability

The readability of a QR code is primarily determined by the size, the input length, the error correction level and any possible logo over the image so you can tweak these parameters if you are looking for optimal results. You can also check $qrCode->getRoundBlockSize() value to see if block dimensions are rounded so that the image is more sharp and readable. Please note that rounding block size can result in additional padding to compensate for the rounding difference. And finally the encoding (default UTF-8 to support large character sets) can be set to ISO-8859-1 if possible to improve readability.

Built-in validation reader

You can enable the built-in validation reader (disabled by default) by calling setValidateResult(true). This validation reader does not guarantee that the QR code will be readable by all readers but it helps you provide a minimum level of quality. Take note that the validator can consume quite amount of additional resources and it should be installed separately only if you use it.

Symfony integration

The endroid/qr-code-bundle integrates the QR code library in Symfony for an even better experience.

  • Configure your defaults (like image size, default writer etc.)
  • Support for multiple configurations and injection via aliases
  • Generate QR codes for defined configurations via URL like /qr-code//Hello
  • Generate QR codes or URLs directly from Twig using dedicated functions

Read the bundle documentation for more information.

Versioning

Version numbers follow the MAJOR.MINOR.PATCH scheme. Backwards compatibility breaking changes will be kept to a minimum but be aware that these can occur. Lock your dependencies for production and test your code when upgrading.

License

This bundle is under the MIT license. For the full copyright and license information please view the LICENSE file that was distributed with this source code.

You might also like...
Laravel Design Pattern Generator (api generator)
Laravel Design Pattern Generator (api generator)

Laravel Design Pattern Generator (api generator) you can create your restful api easily by using this library and you can filter, sort and include elo

One time password generator, validator, and qrcode generator that has no web dependencies (self-contained) in PHP
One time password generator, validator, and qrcode generator that has no web dependencies (self-contained) in PHP

otp-thing One time password generator, validator, and qrcode generator that has no web dependencies (self-contained) in PHP Introduction This started

This bundle provides new generator command line tools for doctrine generator.

GenBundle This bundle provides new generator command line tools for doctrine generator, extending SensioGeneratorBundle. php bin/console gen:generate:

Documentation generator for PHP Code using standard technology (SRC, DOCBLOCK, XML and XSLT)

phpDox phpDox is a documentation generator for PHP projects. This includes, but is not limited to, API documentation. The main focus is on enriching t

Basic Crud Generator (With Code Files, like GII (YII2)) Using Laravel, Livewire and Tailwind CSS
Basic Crud Generator (With Code Files, like GII (YII2)) Using Laravel, Livewire and Tailwind CSS

LiveCrud Live Crud Generator. This package generates Basic Crud with Livewire. Features Generate Complete Crud With Livewire Component and Blade Files

A code generator for EasySwoole

wise-generator-easyswoole 基于EasySwoole的code-generation改写,主要应用与内部系统。基本上所有类都有所调整,同时也完善了一些功能。 版本说明 版本说明 code-generation 使用命令行,一键生成业务通用代码,支持代码如下: 一键生成项目初始

A command line code generator for Drupal.

Drupal Code Generator A command line code generator for Drupal. Installation Download the latest stable release of the code generator.

Contracts for Rule Doc Generator. Useful for production code with minimum dependencies.

Rule Doc Generator Contracts Contracts for Rule Doc Generator. Useful for production code with minimum dependencies. Install composer require symplify

Laravel Code Generator based on MySQL Database

Laravel Code Generator Do you have a well structed database and you want to make a Laravel Application on top of it. By using this tools you can gener

Algerian code generator for invoices, quotes or any commercial documents

Algerian invoice code generator The library is useful to generate code for invoices, quotes or any commercial transaction document. Goal Is to provide

Magento 2 Code Generator

Magento 2 Code Generator Created by Orba Inspired by https://github.com/staempfli/magento2-code-generator Purpose In day-to-day Magento 2 development

QR Code Generator
QR Code Generator

QR Code By endroid If you like my work you can show appreciation by sponsoring via Github Sponsors or send me some ADA at DdzFFzCqrhszSwiFWMBQomfv6mkD

An intelligent code generator for Laravel framework that will save you time

An intelligent code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages and/or form-requests! It is extremely flexible and customizable to cover many of the use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.

A Laravel Code Generator based on your Models using Blade Template Engine
A Laravel Code Generator based on your Models using Blade Template Engine

Laravel Code Generator is a PHP Laravel Package that uses Blade template engine to generate code for you. The difference between other code generators

Laravel CRUD Generator, Make a Web Application Just In Minutes, Even With Less Code and fewer Steps !

🚀 CRUDBOOSTER - Laravel CRUD Generator Laravel CRUD Generator, Make a Web Application Just In Minutes, Even With Less Code and fewer Steps ! About CR

Code generator for Magento 2
Code generator for Magento 2

Magento 2 Code Generator Tool Installation Option1: Downloading .phar wget https://github.com/staempfli/magento2-code-generator/releases/download/ver

The fastest pure PHP database framework with a powerful static code generator, supports horizontal scale up, designed for PHP7
The fastest pure PHP database framework with a powerful static code generator, supports horizontal scale up, designed for PHP7

Maghead 4.0.x IS CURRENTLY UNDER HEAVY DEVELOPMENT, API IS NOT STABLE Maghead is an open-source Object-Relational Mapping (ORM) designed for PHP7. Mag

Runtime Code Generator like Lombok for PocketMine-MP
Runtime Code Generator like Lombok for PocketMine-MP

PlumbokPM Runtime Code Generator like Lombok for PocketMine-MP. Code generation starts when additional autoloader detects class uses PlumbokPM annotat

A QR Code generator for PHP7.4+

chillerlan/php-qrcode A PHP 7.4+ QR Code library based on the implementation by Kazuhiko Arase, namespaced, cleaned up, improved and other stuff. Docu

Comments
  • Replace default font with smaller one

    Replace default font with smaller one

    First of all, thank you for this great work! I'd like to suggest a size optimization.

    While inspecting through the sources, I noticed the NotoSans Font is only used as a fallback for the Label component. Out of the approximately 16.8 MB the endroid/qr-code takes, 16MB alone are due to this font file.

    I think this is a bit too much, considering that :

    • Labels might not be the most common use-case?
    • Labels don't seem to work on SVG.
    • Custom Fonts can easily be configured through a FontInterface implementation.

    Suggestions:

    • Changing the fallback font to something smaller, like the open_sans.ttf ;
    • Skip including the NotoSans file entirely;
    • Maybe NotoSans could become a suggested package and, if detected, could then become the default font;

    Thank you for your time ;)

    enhancement next major 
    opened by pveiga2 2
  • Feature request: add Logo in EPS Output

    Feature request: add Logo in EPS Output

    Hello,

    Would it be possible to add the support of a logo with the writers "Eps" or "Svg"?

    I want to make a PDF with "SwissQR Invoice" and the print quality with the PNG is by far not perfect.

    With the svg we get a perfect quality, but we can not add a logo.

    Is this planned?

    Fabian

    enhancement contribute 
    opened by fajinfo 6
Releases(4.5.2)
  • 4.0.0(Feb 28, 2021)

    • Less dependencies and made validator optional
    • Cleaner separation between QR code, logo and label objects
    • Cleaned up writers
    • Use intermediate result object for greater flexibility
    • Builder registry for multiple configuration support
    • PHP 8 Support
    Source code(tar.gz)
    Source code(zip)
  • 3.9.0(Jul 10, 2020)

  • 3.5.0(Dec 9, 2018)

  • 3.2.5(Feb 12, 2018)

  • 2.4.0(Oct 22, 2017)

    Please note that the routing has moved from annotations to a routing file. Check the README to see how to update your routing to use the yml file.

    Source code(tar.gz)
    Source code(zip)
  • 2.2.0(May 30, 2017)

    • Registration of existing and new writers via WriterRegistry instead of hard coupling
    • Moved logic of managing writers outside QrCode class
    • Making more use of interfaces to improve extensibility
    • Removed magic writer detection and added the concept of a default writer
    • Updated documentation and tests

    Please note there are some BC breaks involved

    Source code(tar.gz)
    Source code(zip)
  • 2.0.0(Apr 17, 2017)

    • Feature: validation of generated QR codes
    • Feature: generate SVG / EPS file (with limited feature set)
    • Refactoring: drop Swetake QR generation in favor of BaconQrCode
    • Refactoring: separation of qr code and writer classes to decrease coupling and improve extensibility
    • Refactoring: guessing type based on file extension
    • Refactoring: error correction level and label alignment set up as enumerations
    • Refactoring: make twig extension independent of service container and move outside of bundle
    • Refactoring: make a distinction between qrcode_url and qrcode_path twig functions
    • Refactoring: use property accessor component to set defined options in the factory
    • Refactoring: logo and label placement
    • Refactoring: cleaned up exceptions
    • Refactoring: extended test suite
    • Support: no JPEG generation (can be added)
    • Support: alpha channels in back- and foreground colors not supported anymore (can be added)
    • Support: dropped PHP 5.4 + 5.5 and HHVM because of incompatibility
    Source code(tar.gz)
    Source code(zip)
  • 1.6.5(Apr 16, 2016)

  • 1.6.4(Apr 10, 2016)

    • Added support for SPD (Short Payment Descriptor)
    • Added support for text labels underneath image (allowing custom fonts)
    • Enabled foreground and background coloring
    • Enabled image padding
    Source code(tar.gz)
    Source code(zip)
Owner
Jeroen van den Enden
Symfony Certified Senior Software Engineer @ Netvlies Breda
Jeroen van den Enden
Multiavatar is a multicultural avatar generator.

Multiavatar Multiavatar is a multicultural avatar generator. In total, it is possible to generate 12,230,590,464 cryptographically unique avatars. Ini

Multiavatar 609 Dec 29, 2022
phpThumb() - The PHP thumbnail generator

phpThumb phpThumb() - The PHP thumbnail generator phpThumb() uses the GD library and/or ImageMagick to create thumbnails from images (GIF, PNG or JPEG

James Heinrich 292 Dec 17, 2022
This is a class of php QR Code, This library helps you generate QR codes in a jiffy.

This is a class of php QR Code, This library helps you generate QR codes in a jiffy.

null 59 Oct 5, 2022
An unofficial package maintained by Salla to help developers to implement ZATCA (Fatoora) QR code easily which required for e-invoicing

ZATCA (Fatoora) QR-Code Implementation An unofficial package maintained by Salla to help developers to implement ZATCA (Fatoora) QR code easily which

Salla 96 Dec 25, 2022
With the help of QR code technologies, digital entry passes can be created, which a user can show at the entry point to pass the door.

Navratri_Entry With the help of QR code technologies, digital entry passes can be created, which a user can show at the entry point to pass the door.

Kushang Shah 5 Aug 7, 2022
Laravel package a helper to Generate the QR code and signed it for ZATCA E-invoicing

Laravel ZATCA E-invoicing Introduction Laravel package a helper to Generate the QR code and signed it for ZATCA E-invoicing Installation To get the la

Ayman Alaiwah 8 Aug 17, 2022
BaconQrCode is a port of QR code portion of the ZXing library

QR Code generator Introduction BaconQrCode is a port of QR code portion of the ZXing library. It currently only features the encoder part, but could l

Bacon 1.5k Jan 6, 2023
EPC-QR-Code - REDAXO-Addon für GiroCode-kompatible EPC-QR-Codes

Generiert QR-Codes, die von GiroCode®️ kompatiblen Banking-Apps in einer Überweisung genutzt werden können.

alex+ Informationsdesign 4 Apr 12, 2022
Laravel CRUD Generator This Generator package provides various generators like CRUD, API, Controller, Model, Migration, View for your painless development of your applications.

Laravel CRUD Generator This Generator package provides various generators like CRUD, API, Controller, Model, Migration, View for your painless develop

AppzCoder 1.3k Jan 2, 2023