Tuesday 17 September 2024

Generate a Dynamic QR Code in PHP

Generate a Dynamic QR Code in PHP

Introduction


In today's digital age, QR codes have become a ubiquitous tool for quickly sharing information, URLs, contact details, and more. They provide a fast and easy way to encode data in a scannable format that smartphones and other devices can read. Whether it's for marketing campaigns, product packaging, or event tickets, generating dynamic QR codes is an essential feature for many web applications.

n this article, we’ll walk you through how to generate a dynamic QR code in PHP using the powerful Endroid QR code library. With step-by-step guidance, you’ll learn how to create a simple PHP application that generates QR codes based on user input.

Let’s dive into it!

Step 1: Setting Up the Project


To get started, you'll need to set up a PHP environment. You can use tools like XAMPP or MAMP to run a local server. Once your environment is ready, create a new project folder and open it in your favorite code editor.

Make sure you have Composer installed, as we’ll be using it to manage our dependencies.

Step 2: Install the Endroid QR Code Library


We’ll be using the Endroid/qr-code package to generate QR codes in PHP. This library offers extensive customization options for generating QR codes with different sizes, colors, and error correction levels.

To install the package, run the following command in your project directory:

	
		composer require endroid/qr-code
	

This command will download and install the QR code library into your project’s vendor folder.

Step 3: Create the HTML Form


	
		<form method="post">
			<div class="mb-3">
				<label>Enter Content</label>
				<input type="text" name="content" class="form-control" />
			</div>
			<div class="mb-3">
				<input type="submit" name="create" class="btn btn-primary" value="Generate" />
			</div>
			<?php echo $image_code; ?>
		</form>
	

This form includes:

  • A text input field for entering the content.
  • A submit button to trigger the QR code generation.
  • A placeholder to display the generated QR code.




Step 4: Handle Form Submission and Generate QR Code


Next, we’ll write the PHP script that processes the form data, generates the QR code, and displays it to the user. Open your index.php file and add the following code:

index.php
	
	<?php

	require 'vendor/autoload.php';

	use Endroid\QrCode\QrCode;
	use Endroid\QrCode\Writer\PngWriter;

	$image_code = '';

	if(isset($_POST['create'])){
		if($_POST['content'] !== ''){
			$temporary_directory = 'temp/';
			$file_name = md5(uniqid()) . '.png';
			$file_path = $temporary_directory . $file_name;

			$qr_Code = new QrCode(trim($_POST['content']));
			$qr_Code->setSize(300);

			$writer = new PngWriter();
			$result = $writer->write($qr_Code);

			$result->saveToFile($file_path);
			$image_code = '<div class="text-center"><img src="'.$file_path.'" /></div>';
		}
	}

	?>
	

This script does the following:

  • Includes the Endroid QR code library using Composer’s autoload.php.
  • Captures the form input using the $_POST array.
  • Generates a unique file name for the QR code image.
  • Uses the QrCode class to create the QR code with the user’s content.
  • Saves the generated QR code as a PNG file in the temp/ folder.
  • Displays the QR code on the page after it’s generated.

Step 5: Display the Generated QR Code


After the form is submitted and the QR code is generated, it will be displayed below the form. The image is dynamically loaded from the temporary folder, allowing users to see their custom QR code based on the content they entered.

You can try generating QR codes for different types of content, such as URLs, text, or even custom messages. The flexibility of the Endroid library allows you to tweak various aspects of the QR code, such as its size and appearance.

Step 6: Run the Project


To see the QR code generator in action:

  1. Open the project in your browser by navigating to the local server URL, like http://localhost/your-project-folder/index.php.
  2. Enter some content into the form—this could be a URL like https://www.example.com or any custom text.
  3. Click the ‘Generate’ button, and the page will refresh, displaying the dynamically generated QR code.

You can scan the QR code using any QR code scanner or your smartphone to see the content it represents.

Conclusion


Congratulations! You’ve just learned how to generate a dynamic QR code in PHP using the Endroid library. This process can be extremely useful in building web applications that require QR code functionality, such as event registrations, contact sharing, and more.

Feel free to expand this project by adding more customization, such as setting colors, error correction levels, or even integrating with databases to store generated QR codes.

We hope this guide was helpful. Stay tuned for more PHP tutorials, and don’t forget to share your thoughts in the comments below!





0 comments:

Post a Comment