Skip to content

QR Code Renderer: 5 Outputs from One Matrix

4 min read Notes

QR Code Renderer: 5 Outputs from One Matrix

Every QR library eventually builds the same grid and then throws it away, because the only public function returns a PNG. phpQRcode keeps the matrix and puts a qr code renderer on top of it, so the same encoded grid becomes SVG, HTML, ASCII, PNG or raw data.

Code: phpQRcode, MIT licensed.

PHP 7.4 to 9.0, globus-studio/phpqrcode on Packagist, no runtime dependencies, ext-gd only for PNG.

The object API exposes the grid

use GlobusStudio\QRCode\QRCode;
use GlobusStudio\QRCode\ErrorCorrection\ErrorCorrectionLevel;
use GlobusStudio\QRCode\Renderer\SvgRenderer;

$qr = new QRCode('https://example.com', ErrorCorrectionLevel::H);
$svg = $qr->render(new SvgRenderer(['size' => 6]));
$matrix = $qr->getMatrix();

Encoding happens once. Rendering is a separate decision, and a batch that needs both a PNG for email and an SVG for print does not encode twice.

getMatrix() is the escape hatch that most wrappers deny: a caller can draw the grid into a PDF, a canvas, or a template that the library has never heard of.

Five renderers, one contract

SvgRenderer produces XML, HtmlRenderer a table, StringRenderer ASCII or UTF-8 blocks, PngRenderer binary data through GD, RawRenderer JSON or an integer grid.

Each takes options for size, margin, colours and a fixed mask. A renderer that only knows how to draw the grid keeps the encoder honest and keeps the interfaces small.

Batch generation, end to end

A label run looks like this: read the rows, build one QRCode per payload with level H, render a SvgRenderer and write the file. Encoding per row is the cost, and a fixed mask makes it predictable.

Two things to keep out of the loop: creating the renderer and re-reading options. Build them once, reuse them, and the batch time is dominated by your storage rather than by the encoder.

Memory is the other limit. A hundred thousand SVG strings held in an array will exhaust a default memory_limit; write each file as it is produced instead.

The mask option is the batch switch

Auto mask selection tests all eight patterns and picks the lowest penalty, at about 180 ms per code. A fixed mask between 0 and 7 brings it to about 11 ms.

That is a code-level decision with a business consequence: a label job of fifty thousand codes finishes in minutes instead of hours, and a scanner still reads the result. The penalty is a slightly worse pattern, which matters for a damaged print, not for a clean one.

Error correction as an enum

L recovers about 7% of the data, M 15%, Q 25%, H 30%. Higher levels produce a denser code for the same payload.

ErrorCorrectionLevel::H in a call site is documentation. A magic number in a config array is not, and the enum costs nothing at runtime.

What the test suite covers

186 tests, 2209 assertions, coverage near 100%, PHPStan at the maximum level, and CI across PHP 7.4 to 9.0.

The version matrix is the useful part. A QR encoder touches integer arithmetic and string handling, and those are exactly the places where a PHP version changes behaviour quietly.

Where it stops

Reading QR codes, logo embedding and vCard or WiFi payload helpers are outside the package. A logo consumes error correction, and choosing the payload format belongs to the application that knows what the label means.

Printing is the last check nobody automates: scan the first label with a phone at the distance a customer would use, before the run starts, and scan it again after the printer has done a hundred.

Related: python readability package for another small package with strict typing, and native file input for the same preference for platform behaviour.

Custom work

Need an API that does not exist yet?

Describe the task and the response format. We will estimate the endpoint, the integration and the maintenance.

Start a conversation