PHP QR Code Generator: Overview of the Best Libraries
QR codes have become an integral part of the modern web. They are used for quick access to links, transferring contact information, service authorization, and even payments. If you are developing in PHP and want to implement QR code generation on your website, you don't need to reinvent the wheel — there are proven libraries that handle this task in just a few lines of code.
In this article, we will look at three popular solutions: the classic phpqrcode, the modern endroid/qr-code, and the lightweight chillerlan/php-qrcode. You will learn how to install each one, generate a QR code, and display it on the screen or save it to a file.
1. phpqrcode Library (Classic Option)
This is one of the first libraries for generating QR codes in PHP. It requires no external dependencies, works out of the box, and is still used on many legacy projects. The main drawback is the lack of support for modern PSR and Composer standards (although it can be installed manually).
Installation
Download the archive from SourceForge and extract it into your project folder. Include the qrlib.php file:
require_once 'phpqrcode/qrlib.php';Example of Generating and Outputting PNG
<?phprequire_once 'phpqrcode/qrlib.php';
// Data to encode$text = 'https://pythonlib.ru';
// Path to save the file$filename = 'qrcode.png';
// Generate QR code with correction level L (7%) and size 10QRcode::png($text, $filename, QR_ECLEVEL_L, 10, 2);
// Output image to browserheader('Content-Type: image/png');QRcode::png($text, false, QR_ECLEVEL_L, 10, 2);?>The second parameter false means the image is output directly to the browser, not saved to a file.
2. endroid/qr-code Library (Modern and Flexible)
This is the most popular solution on Packagist. The library supports PSR-4, integrates easily with any framework (Laravel, Symfony), and allows you to customize colors, size, margins, and even add a logo.
Installation via Composer
composer require endroid/qr-codeBasic Generation Example
<?phprequire 'vendor/autoload.php';
use Endroid\\QrCode\\QrCode;use Endroid\\QrCode\\Writer\\PngWriter;
$qrCode = QrCode::create('https://pythonlib.ru') ->setSize(300) ->setMargin(10);
$writer = new PngWriter();$result = $writer->write($qrCode);
// Output to browserheader('Content-Type: '.$result->getMimeType());echo $result->getString();?>QR Code with Logo
<?phpuse Endroid\\QrCode\\Logo\\Logo;use Endroid\\QrCode\\Color\\Color;
$logo = Logo::create('path/to/logo.png') ->setResizeToWidth(60);
$qrCode = QrCode::create('https://pythonlib.ru') ->setSize(400) ->setForegroundColor(new Color(0, 123, 255)); // blue color
$writer = new PngWriter();$result = $writer->write($qrCode, $logo);
header('Content-Type: image/png');echo $result->getString();?>3. chillerlan/php-qrcode Library (Lightweight and Fast)
An excellent choice if you need minimal dependencies and high performance. The library supports all standard encoding modes, masks, and error correction levels. It is ideal for high-load projects.
Installation
composer require chillerlan/php-qrcodeGeneration Example
<?phprequire 'vendor/autoload.php';
use chillerlan\\QRCode\\QRCode;use chillerlan\\QRCode\\QROptions;
$options = new QROptions([ 'outputType' => QRCode::OUTPUT_IMAGE_PNG, 'eccLevel' => QRCode::ECC_L, Also in library
Working with YAML in Kotlin: Libraries, Examples, and Parsing
HTTP Client for Swift (iOS/macOS): Top Libraries 2024
Working with Kafka in JavaScript (Node.js): node-rdkafka library
ORM for PHP: Comparison of Doctrine, Eloquent, and Cycle — Code Examples
Redis client for Swift (iOS/macOS) – RediStack