Current Location: Home> Latest Articles> Quick Guide to Creating a Simple QR Code Generator with PHP

Quick Guide to Creating a Simple QR Code Generator with PHP

M66 2025-06-24

How to Create a Simple QR Code Generator Using PHP

QR codes are widely used in modern life because they can quickly transmit information and improve user experience. This article will show you how to create a simple QR code generator using PHP, helping you generate QR code images effortlessly.

1. Prepare Environment and Dependencies

Before getting started, please make sure you have the following installed:

  1. PHP: Ensure your PHP version is up to date. You can check it by running php -v in the command line.
  2. Composer: PHP dependency manager, make sure it's installed and accessible via command line.
  3. QR Code Library: We will use the widely adopted endroid/qr-code library, which can be installed via Composer.

Run the following command in your terminal to install the QR Code library:

<span class="fun">composer require endroid/qr-code</span>

2. Write the QR Code Generation Code

Once installation is complete, create a file named qrcode.php and follow these steps:

Include the QR Code Library

<?php
require_once __DIR__ . '/vendor/autoload.php';
<p>use Endroid\QrCode\QrCode;<br>

Create a QR Code Instance and Configure Parameters

$qrCode = new QrCode('Hello, world!');  // Set the content of the QR code
$qrCode->setSize(300);                     // Set the size of the QR code
$qrCode->setMargin(10);                    // Set the margin around the QR code
$qrCode->setEncoding('UTF-8');             // Set the encoding type

Output the QR Code Image

header('Content-Type: ' . $qrCode->getContentType());  // Set the response content type
echo $qrCode->writeString();                              // Output the QR code image

3. Run the QR Code Generator

After saving the file, run the following command in your terminal:

<span class="fun">php qrcode.php > qrcode.png</span>

This command runs the PHP script and outputs the generated QR code image to a file named qrcode.png.

4. Check the QR Code Result

Open the qrcode.png file to view the generated QR code image. You can also embed this QR code image into your webpages for users to scan.

Summary

This article showed how to quickly create a simple QR code generator using PHP and a popular QR code library. By configuring content, size, and encoding, you can easily generate QR codes tailored to your needs. We hope this tutorial helps you master the essential steps to generate QR codes and boosts your project development efficiency.