~/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

Examples

Copyable examples for every QR code type, from text and URLs to Wi-Fi, vCard and calendar events.

Each example uses a class in QR_Code\Types. The png() call returns a data URI unless you set an output file with setOutfile(). The QR codes in the "Generated" sections are produced by the package on this server when the page renders, with the code shown next to each one.

Text

use QR_Code\Types\QR_Text;
 
$dataUri = (new QR_Text('QR Code Generator for PHP!'))->png();

QR_Text throws an InvalidArgumentException for an empty string or '0'.

URL

A URL without http:// or https:// gets http:// prepended.

use QR_Code\Types\QR_Url;
 
$url = new QR_Url('https://example.com');
 
$dataUri = $url->setSize(8)->setMargin(2)->png();

Email message

The constructor order is address, body, subject. The encoded string uses the MATMSG format.

use QR_Code\Types\QR_EmailMessage;
 
$subject = 'QR Code Message';
$body = 'This email was created from a QR Code!';
 
$email = new QR_EmailMessage($to, $body, $subject);
 
$email->setOutfile('email-qr-code.png')->png();

Generated: text, URL and email

QR code: Text
Text
PNG
(new QR_Text('Hello from QR Code!'))->png();
QR code: URL
URL
PNG
(new QR_Url('https://github.com/webpatser/qr-code'))->png();
QR code: Email message
Email message
PNG
(new QR_EmailMessage(
    '[email protected]',
    'Hello from QR!',
    'QR Code Test',
))->png();

Phone and SMS

use QR_Code\Types\QR_Phone;
use QR_Code\Types\QR_Sms;
 
$phone = new QR_Phone('+55 31 1234-5678');
 
$phone->setErrorCorrectionLevel('M')
->setMargin(2)
->setSize(5)
->png();
 
$sms = new QR_Sms('+55 (31) 1234-5678', 'Text to send!');
 
$sms->png();

Wi-Fi network

The constructor order is authentication type as a free string (for example WPA or WPA2), SSID, password, and an optional hidden flag.

use QR_Code\Types\QR_WiFi;
 
$authenticationType = 'WPA2';
$ssId = 'MySuperSSID';
$ssIdisHidden = false;
$password = 'Y0uC4n7f1nd7h3p4ssw0rd';
 
$wifi = new QR_WiFi($authenticationType, $ssId, $password, $ssIdisHidden);
 
$wifi->png();

vCard

Phone takes a type (a free string written as the vCard TYPE, for example HOME or WORK), a number and an optional cell phone flag. Address takes a type, a preferred flag, street, city, state, zip and country.

use QR_Code\Types\QR_VCard;
use QR_Code\Types\vCard\Address;
use QR_Code\Types\vCard\Person;
use QR_Code\Types\vCard\Phone;
 
$person = new Person('John', 'Doe', 'Mr.', '[email protected]');
 
$phone1 = new Phone('HOME', '+1 001 555-1234');
$phone2 = new Phone('WORK', '+1 001 555-4321');
$phone3 = new Phone('WORK', '+1 001 9999-8888', $isCellPhone = true);
 
$address = new Address('HOME', $isPreferredAddress = true, '1234 Main st.', 'New York', 'NY', '12345', 'USA');
 
$vCard = new QR_VCard($person, [$phone1, $phone2, $phone3], [$address]);
 
$vCard->setOutfile('contact.svg')->svg();

Person also accepts an organization and an organization title as fifth and sixth arguments.

meCard

use QR_Code\Types\QR_meCard;
 
$card = new QR_meCard('John Doe', '1234 Main st.', '+1 001 555-1234', '[email protected]');
 
$card->png();

Generated: phone, SMS, vCard and meCard

QR code: Phone
Phone
PNG
(new QR_Phone('+1-555-123-4567'))->png();
QR code: SMS
SMS
PNG
(new QR_Sms('+1-555-123-4567', 'Hello from QR Code!'))->png();
QR code: vCard
vCard
PNG
$person = new Person(
    'John', 'Doe', 'Mr.', '[email protected]',
    'Example Corp', 'Developer',
);

(new QR_VCard($person))->png();
QR code: meCard
meCard
PNG
(new QR_meCard(
    'John Doe',
    '1234 Main st.',
    '+1 001 555-1234',
    '[email protected]',
))->png();

Calendar event

The end must be later than the start, and the summary cannot be empty. Otherwise the constructor throws InvalidEventDateException or EmptyEventSummaryException.

use QR_Code\Types\QR_CalendarEvent;
 
$startDateAndTime = new \DateTime('next saturday 7pm');
$endDateAndTime = new \DateTime('next saturday 11pm');
 
$summary = 'Dinner date with Janet Doe';
$description = 'Dinner at Per Se (French Restaurant)';
$location = 'Time Warner Center, 10 Columbus Cir, New York, NY 10023, USA';
 
$event = new QR_CalendarEvent($startDateAndTime, $endDateAndTime, $summary, $description, $location);
 
$event->setOutfile('event.svg')->svg();

Any other string

QR_Code::png() encodes any string, so other formats work by writing the code string yourself.

use QR_Code\QR_Code;
 
QR_Code::png('TEL:+55 31 1234-5678', 'phone.png');

Generated: Wi-Fi, location and calendar event

A location is a geo: URI encoded as plain text.

QR code: Wi-Fi network
Wi-Fi network
PNG
(new QR_WiFi('WPA', 'MyHomeWiFi', 'SecretPassword123'))->png();
QR code: Location
Location
PNG
(new QR_Text('geo:40.7128,-74.0060?q=New+York+City'))->png();
QR code: Calendar event
Calendar event
PNG
(new QR_CalendarEvent(
    new \DateTime('2026-12-05 19:00'),
    new \DateTime('2026-12-05 23:00'),
    'Dinner date with Janet Doe',
))->png();

Custom colors

Colors are integers in 0xRRGGBB form.

use QR_Code\Types\QR_Text;
 
$greenQR = new QR_Text('Green QR Code!');
$greenQR->setSize(6)
->setBackColor(0xE8F5E8) // light green background
->setForeColor(0x2D5016); // dark green foreground
 
$dataUri = $greenQR->png();

Generated: colour variants

Keep enough contrast between foreground and background, and a light background behind a dark foreground, so scanners can read the code.

QR code: Green
Green
PNG
(new QR_Text('Green QR Code!'))
    ->setSize(6)
    ->setBackColor(0xE8F5E8)
    ->setForeColor(0x2D5016)
    ->png();
QR code: Blue
Blue
PNG
(new QR_Text('Blue QR Code!'))
    ->setSize(6)
    ->setBackColor(0xE6F3FF)
    ->setForeColor(0x1E3A8A)
    ->png();
QR code: Purple
Purple
PNG
(new QR_Text('Purple QR!'))
    ->setSize(6)
    ->setBackColor(0xF3E8FF)
    ->setForeColor(0x581C87)
    ->png();
QR code: High contrast
High contrast
PNG
(new QR_Text('High Contrast!'))
    ->setSize(6)
    ->setBackColor(0xFFFF00)
    ->setForeColor(0xFF0000)
    ->png();
QR code: Large, error correction H
Large, error correction H
PNG
(new QR_Text('This is a large, beautiful green QR code with custom styling!'))
    ->setSize(10)
    ->setMargin(8)
    ->setErrorCorrectionLevel('H')
    ->setBackColor(0xDCFCE7)
    ->setForeColor(0x166534)
    ->png();

Sizes and formats

setSize() sets the pixels per module of the PNG. For SVG as a string, render the module matrix with GdRenderer::renderSVG() (see Quick Start).

Generated: sizes, PNG and SVG

QR code: PNG, default size
PNG, default size
PNG

3 pixels per module.

$demoQR = new QR_Text('Format Comparison Demo');

$demoQR->png();
QR code: PNG, size 8
PNG, size 8
PNG

8 pixels per module.

$demoQR->setSize(8)->png();
QR code: PNG, size 2
PNG, size 2
PNG

2 pixels per module.

$demoQR->setSize(2)->png();
QR code: SVG as a string
SVG as a string
SVG

The module matrix from QR_Code::text(), drawn by GdRenderer::renderSVG().

use QR_Code\Enums\ImageEngine;
use QR_Code\QR_Code;
use QR_Code\Renderer\GdRenderer;

$matrix = array_map(
    str_split(...),
    QR_Code::text('Format Comparison Demo'),
);

$svg = (new GdRenderer(ImageEngine::GD, 3))
    ->renderSVG($matrix);