Generating and Decoding QR Codes in PHP
This article explains how to generate QR codes using the PHP QRCode library and how to decode them with PHP's image functions and the ZXing library, providing step‑by‑step code examples for creating, saving, and reading QR code images.
With the widespread use of smartphones, QR codes have become a practical way to transmit information, and this article shows how to generate and decode QR codes using PHP functions.
1. Generate QR Code
In PHP you can use the QRCode class from the open‑source qrcode.php library. After including the library, the QRCode::png() function creates a QR code image.
require 'qrcode.php';
$text = 'https://example.com'; // QR code content
$filename = 'qrcode.png'; // output file name
QRCode::png($text, $filename); // generate QR code imageThe code first includes qrcode.php , defines the QR code content in $text and the output file name in $filename , then calls QRCode::png() to produce the image.
2. Decode QR Code
Decoding a QR code in PHP can be done with the imagecreatefrompng() function to load the image and the ZXing library’s decode() method to read the embedded data.
require 'qrcode.php';
require 'zxing.php';
$filename = 'qrcode.png'; // QR code image path
$im = imagecreatefrompng($filename); // create image resource
$decoded_text = ZxingDecoder::decode($im); // decode QR code
imagedestroy($im); // free resource
echo $decoded_text; // output decoded resultThe script includes both qrcode.php and zxing.php , loads the PNG file, decodes it with ZxingDecoder::decode() , releases the image resource, and prints the decoded text.
These examples demonstrate the basic steps for generating and decoding QR codes in PHP, which can be further customized for real‑world applications.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.