Skip to content

zhkuskov/qr-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QR Code

By endroid

Latest Stable Version Build Status Total Downloads Monthly Downloads License

This library helps you generate QR codes in an easy way and provides a Symfony bundle for even faster integration.

Installation

Use Composer to install the library.

$ composer require endroid/qrcode khanamiryan/qrcode-detector-decoder:dev-master

Usage

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\SvgWriter;

// Create a QR code
$qrCode = new QrCode('Life is too short to be generating QR codes');
$qrCode->setSize(300);

// Set advanced options
$qrCode
    ->setQuietZone(2)
    ->setEncoding('UTF-8')
    ->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH)
    ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0])
    ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255])
    ->setLabel('Scan the code', 16, __DIR__.'/../font/open_sans.ttf', LabelAlignment::CENTER)
    ->setLogoPath(__DIR__.'/../logo/symfony.png')
    ->setLogoSize(150)
    ->setValidateResult(true)
;

// Output the QR code
header('Content-Type: '.$qrCode->getContentType(PngWriter::class));
$qrCode->writeString(PngWriter::class);

// Save it to a file (guesses writer by file extension)
$qrCode->writeFile(__DIR__.'/qrcode.png');

// Create a response object
$response = new Response(
    $qrCode->writeString(SvgWriter::class),
    Response::HTTP_OK,
    ['Content-Type' => $qrCode->getContentType(SvgWriter::class)])
;

// Work via the writer
$writer = new DataUriWriter($qrCode);
$dataUri = $writer->writeString();

QR Code

Symfony integration

Register the Symfony bundle in the kernel.

// app/AppKernel.php

public function registerBundles()
{
    $bundles = [
        // ...
        new Endroid\QrCode\Bundle\EndroidQrCodeBundle(),
    ];
}

The default parameters can be overridden via the configuration.

endroid_qr_code:
    size: 300
    quiet_zone: 2
    foreground_color: { r: 0, g: 0, b: 0 }
    background_color: { r: 255, g: 255, b: 255 }
    error_correction_level: high
    encoding: UTF-8
    label: Scan the code
    label_font_size: 20
    label_alignment: left
    label_margin: { b: 20 }
    logo_path: '%kernel.root_dir%/../vendor/endroid/qrcode/logo/symfony.png'
    logo_size: 150
    validate_result: true

Now you can retrieve the factory as follows and create a QR code.

$factory = $this->get('endroid.qrcode.factory');
$qrCode = $factory->create('Life is too short to be generating QR codes');

Add the following section to your routing to be able to handle QR code URLs. This step can be skipped when you only use data URIs to display your images.

EndroidQrCodeBundle:
    resource: "@EndroidQrCodeBundle/Controller/"
    type:     annotation
    prefix:   /qrcode

After installation and configuration, QR codes can be generated by appending the QR code text to the url followed by any of the supported extensions.

Twig extension

The bundle also provides a Twig extension for quickly generating a QR code URL, path or data URI. Optional parameters are extension, size, quiet zone and error correction level. When a parameter is omitted, the value in the bundle configuration is used. The default extension is .png.

<img src="{{ qrcode_path(message) }}" />
<img src="{{ qrcode_url(message, { extension: 'svg' }) }}" />
<img src="{{ qrcode_data_uri(message, { size: 150 }) }}" />

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.

About

QR Code Generator

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%