QR Code

QR Code in PHP: error correction levels, versions, encoding modes, code examples and SVG output

Denso Wave designed the QR Code in 1994 to track parts on a production line, and its three finder patterns are why a phone can read one at any angle. Forty versions, from 21 modules square to 177, and four error correction levels that trade capacity for damage tolerance: L recovers about 7 per cent of the symbol, H about 30.

Leave the version at 0 and the library picks the smallest symbol the data fits. QR Code is a registered trademark of DENSO WAVE INCORPORATED.

Type Token

ItemValue
Type tokenQRCODE
Enum caseBarcodeType::QRCODE
Family2D
StandardISO/IEC 18004
Class\Com\Tecnick\Barcode\Type\Square\QrCode

Input

ItemValue
Character setany byte sequence; the encoder switches between numeric, alphanumeric, byte and kanji modes
Lengthup to 7089 digits, 4296 alphanumeric characters or 2953 bytes, depending on version and error correction
CheckReed-Solomon error correction, computed by the library
Extra parameters7 (see below)

Extra Parameters

Appended to the type token after commas, as in QRCODE,H.

PositionMeaningValuesDefault
1error correction levelL, M, Q, HL
2encoding modeNL, NM, AN, 8B, KJ, ST8B
3version0 for automatic, or 1 to 400
4case sensitivity1 or 01
5random mask countnumber of masks to tryunset
6best mask search1 or 01
7default mask0 to 72

Examples

Digits at the default error correction level

<?php

require_once __DIR__ . '/vendor/autoload.php';

$barcode = new \Com\Tecnick\Barcode\Barcode();
$bobj = $barcode->getBarcodeObj(
    type: 'QRCODE',
    code: '0123456789',
    width: -4,
    height: -4,
    color: 'black',
    padding: [0, 0, 0, 0],
)->setBackgroundColor('white');

echo $bobj->getInlineSvgCode();

QR Code barcode encoding 0123456789

A URL at the highest error correction level

$bobj = $barcode->getBarcodeObj(
    type: 'QRCODE,H',
    code: 'https://tcpdf.org',
    width: -4,
    height: -4,
    color: 'black',
    padding: [0, 0, 0, 0],
)->setBackgroundColor('white');

QR Code barcode encoding https://tcpdf.org

A fixed version 4 symbol, larger than the data needs

$bobj = $barcode->getBarcodeObj(
    type: 'QRCODE,M,NL,4',
    code: 'tc-lib-barcode',
    width: -4,
    height: -4,
    color: 'black',
    padding: [0, 0, 0, 0],
)->setBackgroundColor('white');

QR Code barcode encoding tc-lib-barcode

Errors

These payloads raise \Com\Tecnick\Barcode\Exception:

PayloadMessage
``Empty input

See Also