fmassei/qr-code

fmassei QR Code

v0.2 2021-03-24 16:28 UTC

This package is auto-updated.

Last update: 2024-04-24 23:03:45 UTC


README

Latest Stable Version Build Status Total Downloads Monthly Downloads License

This library helps you create QR codes in SVG, PNG, and binary format.

  • Use the Imagick extension to render self-generated SVG images.
  • Logo and label placement
  • Can use of SVG frames
  • Common generation options
  • Matrix generated by bacon/bacon-qr-code
  • Simple internal structure

This library is based on the structure of endroid/qr-code, trying to overcome many deal-breaker characteristics present at the time of forking. Among the bigger changes:

  • Unified drawing (so results look the same in all formats)
  • Switch from GD to Imagick (for SVG support)
  • Switch from SimpleXMLElement to DOMDocument (more complex operations on SVG nodes)

Installation

Use Composer to install the library.

$ composer require fmassei/qr-code

Usage: generation and options

use fmassei\QrCode\QrCode;
use fmassei\QrCode\Logo;
use fmassei\QrCode\Label;
use fmassei\QrCode\Writer\PngWriter;

/* to create a code, make an instance of the QrCode class */
$qrCode = new QrCode("QR code data");

/* override some default settings */
$qrCode->size = 400;
$qrCode->foregroundColor = '#f00';
$qrCode->errorCorrectionLevel = QrCode::ERROR_CORRECTION_LEVEL_MEDIUM;

/* to set the optional logo and a label */
$qrCode->logo = Logo::fromPath(__DIR__.'/mylogo.png');
$qrCode->label = new Label("My first code!");

/* use a writer to generate the result */
$result = (new PngWriter())->write($qrCode);
        

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 <img> tag) */
$dataUri = $result->getDataUri();

QR code example

One liner

Everyone expects it, I guess.

echo (new PngWriter())->write(new QrCode($myData))->getString();

SVG frames

A frame is a template for outputting code, logo and label. The library recognizes any element inside the SVG frame with id equals to:

  • SVGFrame::IDQrCode (default "QrCode"): this element will be subsituted with the QR code matrix
  • SVGFrame::IDLogo (default "Logo"): this element will be substituted with the passed Logo image (if any). No other variable of the Logo object is used.
  • SVGFrame::IDLabel (default "Label"): the value of this node will be substituted with the label text. No other variable of the Label object is used.

An example of the SVG structure and usage is available in the tests.

SVGFrame example

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.