Qrcode Library¶
The library provides developers with the tools to generate Qr codes with ease. It is a total refactored version of the previous named yii2-qrcode-helper which was based on the ported PHP version of the libqrencode C library.
This new version is highly inspired by the great work of BaconQrCode, in fact, it uses a modified version of its code for the writers included on this package.
Getting Started¶
Supported PHP Versions¶
| Tag | PHP Version |
|---|---|
| 3.x | 7.3 - 8.1 |
| ^4.0 | 8.3 - 8.5 |
Server Requirements¶
- PHP >= 8.3
- GD
- FreeType
- Imagick (optional — only if you opt into the ImageMagick render backend; GD is the default)
The preferred way to install this extension is through composer.
Either run
or addUsage¶
The use of the library is quite easy when working as standalone. For example:
<?php
use Da\QrCode\QrCode;
$qrCode = (new QrCode('This is my text'))
->setSize(250)
->setMargin(5)
->setBackgroundColor(51, 153, 255);
// now we can display the qrcode in many ways
// saving the result to a file:
$qrCode->writeFile(__DIR__ . '/code.png'); // writer defaults to PNG when none is specified
// display directly to the browser
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();
?>
<?php
// or even as data:uri url
echo '<img src="' . $qrCode->writeDataUri() . '">';
?>
You can set the foreground color, defining RGBA values, where the alpha is optional.
$qrCode = (new QrCode('This is my text'))
->setForegroundColor(0, 0, 0);
// or, setting alpha as well
$qrCode = (new QrCode('This is my text'))
->setForegroundColor(0, 0, 0, 50);
Formats¶
In order to ease the task to write different formats into a QrCode, the library comes with a set of classes. These are:
- BookMarkFormat
- BtcFormat
- GeoFormat
- ICalFormat
- MailMessageFormat
- MailToFormat
- MeCardFormat
- MmsFormat
- PhoneFormat
- SmsFormat
- VCardFormat
- WifiFormat
- YoutubeFormat
Laravel¶
This library bundles a blade component and a file route to easily work with the Laravel framework.
Yii2¶
This library comes also with two special classes to specifically work with the Yii2 framework. These are:
Yii3 / PSR-15¶
For Yii3 — and any PSR-15 application (Mezzio, Slim, …) — the library ships a framework-agnostic request handler: