Generating QR Codes in PHP: phpqrcode Library and Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Generating QR Codes in PHP with the phpqrcode Library



QR codes have become an integral part of the modern web. They are used for quickly navigating to links, transferring contact information, connecting to Wi-Fi, and much more. In this article, we will explore the popular PHP library phpqrcode, which allows you to easily and quickly generate QR codes on the server side. This is an ideal solution for an educational website where you need to visually demonstrate working with QR codes.



What is phpqrcode?

phpqrcode is a lightweight yet powerful library for generating QR codes in pure PHP. It does not require installing additional extensions (e.g., GD or Imagick), although it supports them if available. The library can create QR codes of various versions (from 1 to 40), with different error correction levels (L, M, Q, H), and save the result in PNG, GIF, or JPEG. The main advantage is ease of use: just include one file and call one function.



Installation and Setup

You can download the library from the official repository on GitHub or SourceForge. To install, simply copy the phpqrcode.php file into your project. An alternative method is via Composer:



composer require phpqrcode/phpqrcode


After installation, include the library in your PHP script:



<?php// If installed manuallyinclude 'phpqrcode.php';

// Or via Composerrequire 'vendor/autoload.php';?>


Basic Example: Generating a QR Code

The simplest example is to create a QR code containing a text string or URL. This is done using the static method QRcode::png():



<?php$text = "https://pythonlib.ru";$filename = "qrcode.png"; // If not specified, outputs the image directly$errorCorrectionLevel = 'L'; // Error correction level: L, M, Q, H$matrixPointSize = 10; // Point size (1-10)

// Generate and save to fileQRcode::png($text, $filename, $errorCorrectionLevel, $matrixPointSize, 2);

echo "QR code saved as $filename";?>


If the second parameter is omitted, the library will output the PNG image directly to the browser:



<?phpheader('Content-Type: image/png');QRcode::png('https://pythonlib.ru', false, 'H', 8, 2);?>


Customizing Appearance: Color and Size

A standard QR code is black and white. To make it colored or change the background, use the advanced features. The library allows you to set colors via an array:



<?php$text = "Hello, PythonLib!";$filename = "color_qr.png";

// Parameters: [R, G, B] for modules and background$config = [ 'module' => [52, 73, 94], // Dark blue 'background' => [236, 240, 241] // Light gray];

QRcode::png($text, $filename, 'M', 10, 2, false, $config);?>


For precise size adjustment, use the $matrixPointSize parameter (from 1 to 10). The higher the value, the larger the QR code. You can also set $quietZone (the white border around the code) — the last parameter in the png() method.



Generating a QR Code for Wi-Fi

One popular task is creating a QR code for connecting to Wi-Fi. The data format must comply with the specification:



<?php$ssid = "MyHomeNetwork";$password = "secret123";$encryption = "WPA"; // WPA, WEP, or empty for open network

$wifiString = "WIFI:T:$encryption;S:$ssid;P:$password;;";

QRcode::png($wifiString, "wifi_qr.png", 'Q', 10, 2);echo "Wi-Fi QR code is ready!";?>


Creating a QR Code for vCard (Contacts)

A QR code can contain a vCard business card. This is convenient for quickly adding a contact to a phone:



<?php$vcard = "BEGIN:VCARD";$vcard .= "VERSION:3.0";$vcard .= "FN:Ivan Petrov";$vcard .= "TEL:+7-123-456-78-90";$vcard .= "EMAIL:ivan@exampl                    

Recommendations