All code type classes live in QR_Code\Types, extend QR_Code\Util\AbstractGenerator and implement getCodeString(), which returns the string that gets encoded. The QR code under a type is generated on this server with the code shown in its card.
Code types
QR_Text
new QR_Text(string $data)
Encodes the string as is. Throws InvalidArgumentException when the string is empty or '0' (the check uses empty()).
(new QR_Text('API Documentation'))->png();
QR_Url
new QR_Url(string $url)
Encodes the URL. When it does not start with http:// or https://, http:// is prepended.
(new QR_Url('https://github.com/webpatser/qr-code'))->png();
QR_EmailMessage
new QR_EmailMessage(string $email, string $message, string $subject)
Encodes MATMSG:TO:{email};SUB:{subject};BODY:{message};;.
(new QR_EmailMessage(
'[email protected]',
'Generated from API docs',
'QR Code API Test',
))->png();
QR_Phone
new QR_Phone(string $phone)
Encodes TEL:{phone}.
(new QR_Phone('+1-555-API-DOCS'))->png();
QR_Sms
new QR_Sms(string $phone, string $text)
Encodes SMSTO:{phone}:{text}.
(new QR_Sms('+1-555-123-4567', 'API Documentation SMS'))->png();
QR_WiFi
new QR_WiFi(string $authentication, string $ssid, string $password, bool $hidden = false)
$authentication is the security type as a free string, for example WPA or WPA2. Encodes WIFI:T:{authentication};S:{ssid};P:{password}; followed by H:true; for a hidden network or ; otherwise.
use QR_Code\Types\QR_WiFi; $wifi = new QR_WiFi('WPA', 'MyHomeNetwork', 'secret123');
(new QR_WiFi('WPA', 'API-Demo', 'demo-password'))->png();
QR_meCard
new QR_meCard(string $name, string $address, string $phone, string $email)
QR_CalendarEvent
new QR_CalendarEvent( \DateTime $dateTimeStart, \DateTime $dateTimeEnd, string $summary, string $description = '', string $location = '')
Throws InvalidEventDateException when the end is not later than the start and EmptyEventSummaryException when the summary is blank.
QR_VCard
new QR_VCard(Person $person, array $phones = [], array $addresses = [])
$phones must contain only Phone objects and $addresses only Address objects, otherwise InvalidVCardPhoneEntryException or InvalidVCardAddressEntryException is thrown.
new Person(string $firstName, string $lastName, string $title, string $email, string $org = '', string $orgTitle = '')new Phone(string $type, string $number, bool $cellphone = false) // type: free string written as the vCard TYPE, for example HOME or WORKnew Address(string $type, bool $pref, string $street, string $city, string $state, string $zip, string $country) // type: free string written as the vCard TYPE, for example HOME or WORK
These three classes are in QR_Code\Types\vCard.
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]', 'Example Corp', 'Software Engineer'); $vcard = new QR_VCard( $person, [new Phone('WORK', '+1-555-123-4567'), new Phone('WORK', '+1-555-987-6543', true)], [new Address('WORK', true, '123 Business St', 'Tech City', 'TC', '12345', 'USA')]);
$person = new Person(
'API', 'Developer', 'Dr.', '[email protected]',
'QR Corp', 'Engineer',
);
(new QR_VCard($person))->png();
Configuration methods
Defined on AbstractGenerator. Every setter returns the generator.
| Method | Default | Notes |
|---|---|---|
setSize(int $size) |
3 | Pixels per module. Throws InvalidArgumentException outside 1 to AbstractGenerator::MAX_SIZE (100). The rendered image is capped at 1024 pixels, so large sizes are reduced to fit. |
setMargin(int $margin) |
4 | Margin in modules. |
setErrorCorrectionLevel(string $level) |
'L' |
'L', 'M', 'Q' or 'H'. |
setBackColor(int $color) |
QR_WHITE (0xFFFFFF) |
|
setForeColor(int $color) |
QR_BLACK (0x000000) |
|
setOutfile($outfile) |
false (no file) |
File to write; only string values are applied. Written to disk as given, so never pass unvalidated user input. |
setSaveAndPrint(bool $saveAndPrint) |
false |
Write the file and also return or send the data. |
Output methods
| Method | Without output file | With setOutfile() |
|---|---|---|
png(): string |
Returns data:image/png;base64,... |
Writes the file and returns the path (with setSaveAndPrint(true), returns the data URI) |
svg(): void |
Sends a Content-Type: image/svg+xml header and echoes the SVG |
Writes the file (with setSaveAndPrint(true), also echoes it) |
getCodeString(): string |
The string that is encoded |
eps() is not on the types, only on QR_Code.
Error correction levels
| Level | Recovery |
|---|---|
L |
about 7% |
M |
about 15% |
Q |
about 25% |
H |
about 30% |
QR_Code static helpers
QR_Code\QR_Code encodes a string without a type class. png(), svg(), eps() and text() take $level as 'L', 'M', 'Q', 'H' (lowercase too) or the constants QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_Q, QR_ECLEVEL_H. raw() takes only the integer constants.
QR_Code::png(string $text, $outfile = false, $level = QR_ECLEVEL_L, int $size = 3, int $margin = 4, bool $saveAndPrint = false, int $back_color = QR_WHITE, int $fore_color = QR_BLACK): stringQR_Code::svg(string $text, $outfile = false, $level = QR_ECLEVEL_L, int $size = 3, int $margin = 4, bool $saveAndPrint = false, int $back_color = QR_WHITE, int $fore_color = QR_BLACK): voidQR_Code::eps(/* same as svg(), plus */ bool $cmyk = false): voidQR_Code::text(string $text, $outfile = false, $level = QR_ECLEVEL_L, int $size = 3, int $margin = 4): arrayQR_Code::raw(string $text, $outfile = false, int $level = QR_ECLEVEL_L, int $size = 3, int $margin = 4)
QR_Code has no url() or enableCache() method. text() returns the code as an array of rows, not an image.
Utility classes
Tools
use QR_Code\Util\Tools; Tools::buildCache(); // fill the in-memory frame cache and write frame imagesTools::clearCache(); // reset Tools' own static frame list (not the encoder's frame cache)Tools::clearTemporaryQRCodes(); // remove every file whose name contains qr_code_ from TEMP_DIR
Tools::clearCache() resets only Tools' own static frame list, which the encoder does not use, so it does not clear the frame cache (Specifications::$frames). TEMP_DIR is the package's public/temp directory unless you define TEMP_DIR first.
ImageEngine
use QR_Code\Enums\ImageEngine; $engines = ImageEngine::getAvailable(); // subset of [Imagick, GD, None]$best = ImageEngine::detectBest();
Cases: GD, Imagick, None. Instance methods: isAvailable(), getPerformanceScore(), getSupportedFormats().
ArrayOptimizer
compressMatrix(array $matrix): string, decompressMatrix(string $compressed, int $size): array, validateMatrix(array $matrix): bool, rotateMatrix(array $matrix, int $degrees = 90): array, countBlackModules(array $matrix): int, batchProcess(array $matrices, callable $processor): array.
FastQRCode and UltraFastQRCode
Cached generators with QRConfig and ModernQRConfig. See Performance.
Error handling
use QR_Code\Types\QR_Text; try { $qr = new QR_Text(''); // throws: Text data cannot be empty} catch (\InvalidArgumentException $e) { echo 'Invalid input: '.$e->getMessage();}
png() rethrows encoding exceptions after logging them. svg() and eps() log them and return without output.
This is the API reference for QR Code v1.0.2.