"Master PHP QR Code Generation: Step-by-Step Example"

Sandra Jun 29, 2026

Effortlessly Generate QR Codes with PHP: A Step-by-Step Guide

QR And Bar Code Generator
QR And Bar Code Generator

In the digital age, Quick Response (QR) codes have become an indispensable tool for instant data access. PHP, a popular server-side scripting language, can help you generate QR codes seamlessly. Let's dive into a practical example of creating a PHP QR code generator.

Create Professional QR Codes for FREE — No Catch, No Limits
Create Professional QR Codes for FREE — No Catch, No Limits

Understanding QR Codes and Endroid/QrCode Library

QR codes are two-dimensional barcodes that can store a significant amount of data. For PHP, we'll use the Endroid/QrCode library, which simplifies QR code generation. First, you need to install it via Composer, the PHP dependency manager.

5 Free Open Source QR Code Creator for Text, URLs, Contacts
5 Free Open Source QR Code Creator for Text, URLs, Contacts

Installing Endroid/QrCode via Composer

Open your terminal and run the following command to install the library:

QR Code Generator
QR Code Generator

composer require endroid/qr-code

Once installed, you can start generating QR codes.

Generating a Simple QR Code

Let's create a basic QR code containing a URL. Create a new PHP file (e.g., `generate_qr_code.php`) and add the following code:

the qr code generator app on an iphone
the qr code generator app on an iphone

```php writeFile(__DIR__.'/qrcode.png'); // Display it in the browser header('Content-Type: '.$qrCode->getContentType()); echo $qrCode->writeString(); ```

This script generates a QR code containing the URL https://www.example.com and saves it as a PNG file named qrcode.png in the same directory. It also outputs the QR code image directly to the browser.

Customizing QR Code Appearance

The Endroid/QrCode library allows you to customize the appearance of your QR codes. You can change the size, padding, error correction level, and even add a logo.

Free QR Code Generator: Create Everlasting Plain Text QRs
Free QR Code Generator: Create Everlasting Plain Text QRs

Changing Size and Padding

To change the size and padding of the QR code, modify the script as follows:

100% Free SEO Tools - SmallSEOTools.com
100% Free SEO Tools - SmallSEOTools.com
Free Advanced Barcode Generator Tool - Create custom barcodes
Free Advanced Barcode Generator Tool - Create custom barcodes
Free QR codes | Free QR Code Generator | QR Codes with a Logo | Make a QR Code | Dynamic QR Codes
Free QR codes | Free QR Code Generator | QR Codes with a Logo | Make a QR Code | Dynamic QR Codes
Generador de código QR sin conexión con logotipo | ventanas 10/11
Generador de código QR sin conexión con logotipo | ventanas 10/11
a qr code with an image of a bitcoin on it and the words $
a qr code with an image of a bitcoin on it and the words $
Free QR Code Generator — No Sign-Up, Customizable
Free QR Code Generator — No Sign-Up, Customizable
15 Free QR Code Generators (Apps & Websites) | Freeappsforme - Free apps for Android and iOS
15 Free QR Code Generators (Apps & Websites) | Freeappsforme - Free apps for Android and iOS
Top QR Code Generators for Business: Create Custom Codes Easily!
Top QR Code Generators for Business: Create Custom Codes Easily!
QR Code Generator with Logo | Create Free Custom QR Codes Online
QR Code Generator with Logo | Create Free Custom QR Codes Online
Best Free QR Code Generator Online
Best Free QR Code Generator Online
3 Best Free QR Code Generators for Wedding Invitations
3 Best Free QR Code Generators for Wedding Invitations
QR Code Generators | EntheosWeb
QR Code Generators | EntheosWeb
QR Code Generator – Create Custom QR Codes Online Free
QR Code Generator – Create Custom QR Codes Online Free
QR code generator
QR code generator
QR Code Generator - Free Online No-Signup
QR Code Generator - Free Online No-Signup
Free QR Code Generator — Create Custom QR Codes in 10 Seconds
Free QR Code Generator — Create Custom QR Codes in 10 Seconds
Image QR Code Generator | How to Make a Custom QR Code | QR Codes | Convert Image to QR Code | DIY
Image QR Code Generator | How to Make a Custom QR Code | QR Codes | Convert Image to QR Code | DIY
QR Stuff : Créez et personnalisez vos QR codes facilement
QR Stuff : Créez et personnalisez vos QR codes facilement
Web page
Web page
Free QR Code Generator — No Signup
Free QR Code Generator — No Signup

```php $qrCode = new QrCode('https://www.example.com'); $qrCode->setSize(300); // Set width and height to 300 pixels $qrCode->setPadding(10); // Set padding to 10 pixels ```

Adding a Logo

To add a logo to the QR code, use the setLogoPath method:

```php $qrCode->setLogoPath(__DIR__.'/logo.png'); // Set the path to your logo file ```

Generating QR Codes for Different Data Types

The Endroid/QrCode library supports various data types, such as text, URL, email, phone number, SMS, and vCard. Here's an example of generating a vCard QR code:

```php $qrCode = new QrCode('BEGIN:VCARD VERSION:3.0 N:Gump;Forrest FN:Forrest Gump TEL;TYPE=HOME,VOICE:11095551234 END:VCARD', QrCode::MODE_VCARD); $qrCode->setSize(300); $qrCode->setPadding(10); ```

This script generates a vCard QR code containing contact information for Forrest Gump.

Error Correction Levels

QR codes have four error correction levels (L, M, Q, H) that determine the amount of data that can be recovered if the code is damaged or partially obscured. You can set the error correction level using the setErrorCorrectionLevel method:

```php $qrCode->setErrorCorrectionLevel(QrCode::ERROR_CORRECTION_L); // Set error correction level to L ```

By default, the error correction level is set to 'M'.

Conclusion

In this article, we explored how to generate QR codes using PHP and the Endroid/QrCode library. We covered creating simple QR codes, customizing their appearance, and generating codes for different data types. With this knowledge, you can now integrate QR code generation into your PHP projects seamlessly.