PHP

Simple PHP script that generates a QR code

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-xhr="#">
        <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

  1. Save the generate_qr.php file to your web server’s document root directory.
  2. Access the file through your web browser (e.g., http://your-server/generate_qr.php).
  3. 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.

Victoria

Im just a girl who hanging around with her friends ;)

Recent Posts

PHP functions for working with MySQL: a detailed description

PHP provides a set of functions to work with MySQL databases. As of the more…

2 days ago

PHP pagination script from MySQL

Below is a PHP script that demonstrates how to implement pagination when displaying records from…

2 days ago

MySQL database table search script in PHP

Below is a simple PHP script that demonstrates how to search a MySQL database table…

2 days ago

Step-by-Step Guide to Moving Your WordPress Site to a New Domain

Transferring a WordPress site to a new domain involves several detailed steps to ensure that…

4 days ago

5 examples of creating client-server applications in C++

Creating client-server programs in C++ helps in understanding network programming and communication between applications over…

6 days ago

5 bookmark script options for wordpress

Creating a bookmark system for WordPress can range from simple solutions to more complex ones.…

6 days ago