
Simple PHP script that generates a QR code using the popular library called PHP QR Code
. This library is open-source and easy to use. Follow these steps to create your QR code generator.
Step 1: Install the PHP QR Code Library
You can download the library from GitHub or directly using Composer if your project supports it.
If using Composer, run this command:
composer require endroid/qr-code
Step 2: Create the QR Code Generation Script
Create a new PHP file, for example, generate_qr.php
, and add the following code:
<?php require 'vendor/autoload.php'; // Ensure you have the autoload from Composer if using Composer use Endroid\QrCode\QrCode; use Endroid\QrCode\ErrorCorrectionLevel; use Endroid\QrCode\LabelAlignment; // Check if the form is submitted if (isset($_POST['text'])) { $text = $_POST['text']; // Get the text from the form // Create a QR code $qrCode = new QrCode($text); $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH()); $qrCode->setSize(300); $qrCode->setMargin(10); // Generate and save the QR code to a file header('Content-Type: image/png'); echo $qrCode->writeString(); // Output the QR code as a PNG image exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>QR Code Generator</title> </head> <body> <h1>QR Code Generator</h1> <form method="post" action=""> <label for="text">Enter text or URL:</label><br> <input type="text" id="text" name="text" required> <button type="submit">Generate QR Code</button> </form> </body> </html>
Step 3: Run the Script
- Save the
generate_qr.php
file to your web server’s document root directory. - Access the file through your web browser (e.g.,
http://your-server/generate_qr.php
). - Enter the text or URL you want to encode into a QR code and click the “Generate QR Code” button.
Additional Features
If you want to extend the functionality, consider adding:
- Download Button: Allow users to download the generated QR code as a PNG file.
- Customizable Size and Margin: Let users specify the QR code size and margin.
- Styling: Use CSS to improve the look of your form and output.
You have now created a simple QR code generator using PHP! This script allows users to input text or URLs, and it generates a corresponding QR code image. Make sure your server meets the requirements for running PHP scripts, and the GD library is enabled for image handling.