~/docs
Resonate
Resonate
1.x 1.0.2, current
QR Code Generator
Toggle sidebar
QR Code Generator
webpatser/qr-code 1.0.2

QR Code Generator 1.x

Quick Start

Generate your first PNG and SVG QR codes and configure size, margin, colors and error correction.

Every QR code type extends AbstractGenerator, so all types share the same output and configuration methods.

Generate a PNG

use QR_Code\Types\QR_Text;
 
$qr = new QR_Text('Hello, World!');
 
$dataUri = $qr->png(); // data:image/png;base64,...

png() returns a data URI when no output file is set. Use it in HTML as <img src="<?= $dataUri ?>">.

Generated on this server

Both codes are generated with png() when this page renders.

QR code: Text
Text
PNG

Plain text, default settings.

$dataUri = (new QR_Text('Hello, World!'))->png();
QR code: URL
URL
PNG

A URL, default settings.

$dataUri = (new QR_Url('https://github.com/webpatser/qr-code'))->png();

Use the static helper

QR_Code::png() and QR_Code::svg() encode a string directly:

use QR_Code\QR_Code;
 
// Save to a file
QR_Code::png('Hello World!', 'qrcode.png');
QR_Code::svg('Hello World!', 'qrcode.svg');
 
// Get a base64 data URL (no file)
$dataUrl = QR_Code::png('Hello World!');

Warning: The $outfile argument (and setOutfile() on the types) is written to disk as given. Validate or whitelist the path when it is derived from untrusted input, to avoid directory traversal.

Generate SVG and EPS

svg() and eps() return nothing. Without an output file they send the vector data to the output, preceded by a header: svg() sends Content-Type: image/svg+xml and eps() sends Content-Type: application/postscript; with setOutfile() they write a file.

use QR_Code\Types\QR_Url;
 
$qr = new QR_Url('https://github.com/webpatser/qr-code');
 
// Write to a file
$qr->setOutfile('qrcode.svg')->svg();
 
// Capture the SVG as a string
ob_start();
(new QR_Url('https://github.com/webpatser/qr-code'))->svg();
$svg = ob_get_clean();

Note: svg() still sends the Content-Type: image/svg+xml header when the output is buffered, so in a web request this capture changes the content type of an HTML response. Reset the header, or render the module matrix from QR_Code::text() with GdRenderer::renderSVG(), which returns the SVG as a string and sends no header:

use QR_Code\Enums\ImageEngine;
use QR_Code\QR_Code;
use QR_Code\Renderer\GdRenderer;
 
$matrix = array_map(str_split(...), QR_Code::text('Hello World!', false, 'M'));
 
// engine, pixels per module, margin, background, foreground
$svg = (new GdRenderer(ImageEngine::GD, 4, 4, 0xFFFFFF, 0x000000))->renderSVG($matrix);

eps() exists on QR_Code only: QR_Code::eps('Hello World!', 'qrcode.eps').

Size, margin, colors and error correction

Setters return the generator, so they chain.

use QR_Code\Types\QR_Text;
 
$qr = (new QR_Text('Configured QR code'))
->setSize(6) // pixels per module, 1 to 100, default 3
->setMargin(2) // modules around the code, default 4
->setErrorCorrectionLevel('H') // 'L' (default), 'M', 'Q' or 'H'
->setBackColor(0xFFFFFF)
->setForeColor(0x000000);
 
$dataUri = $qr->png();

setSize() throws an InvalidArgumentException outside 1 to 100. The rendered image is capped at 1024 pixels, so large sizes are reduced to fit.

Try the settings

Type a text or URL and change the settings. The QR code is generated on this server with the PHP shown under it, as PNG through QR_Text::png() or as SVG through GdRenderer::renderSVG().

Text or URL
Module size Error correction L (7%) M (15%) Q (25%) H (30%)
Foreground
Background
Format PNG SVG
Generated QR code
PHP for this QR code
use QR_Code\Types\QR_Text;

$dataUri = (new QR_Text('https://github.com/webpatser/qr-code'))
    ->setSize(6)
    ->setErrorCorrectionLevel('M')
    ->setForeColor(0x000000)
    ->setBackColor(0xFFFFFF)
    ->png();

Types in one view

use QR_Code\Types\QR_EmailMessage;
use QR_Code\Types\QR_Phone;
use QR_Code\Types\QR_Sms;
use QR_Code\Types\QR_Text;
use QR_Code\Types\QR_Url;
use QR_Code\Types\QR_WiFi;
 
$text = new QR_Text('Hello, World!');
$url = new QR_Url('https://example.com');
$mail = new QR_EmailMessage('[email protected]', 'Message body', 'Subject');
$phone = new QR_Phone('+1-555-123-4567');
$sms = new QR_Sms('+1-555-123-4567', 'Hello from QR code!');
$wifi = new QR_WiFi('WPA', 'NetworkName', 'password123');

Each type, generated

Type Code Result
QR_Text
Any string.
(new QR_Text('Text'))->png(); QR code: QR_Text
QR_Url
A web address.
(new QR_Url('https://example.com'))->png(); QR code: QR_Url
QR_EmailMessage
Address, body and subject.
(new QR_EmailMessage('[email protected]', 'Hello', 'Subject'))->png(); QR code: QR_EmailMessage
QR_Phone
A phone number to call.
(new QR_Phone('+1-555-123-4567'))->png(); QR code: QR_Phone
QR_WiFi
Authentication type, network name and password.
(new QR_WiFi('WPA', 'MyNetwork', 'password123'))->png(); QR code: QR_WiFi
Location
A geo: URI, encoded with QR_Text.
(new QR_Text('geo:40.7128,-74.0060'))->png(); QR code: Location

QR_VCard, QR_meCard and QR_CalendarEvent are covered in Examples.

Next Steps