Loading...

PHP QR code generator with examples

PHP QR code generator with examples

PHP, an acronym for Hypertext Preprocessor is one of the most widely used scripting languages. It is easy-to-learn, free, and open-source licensed software. A PHP file has the extension .php and it can contain HTML, CSS, JavaScript as well as PHP code. More than 70% of the internet comprises PHP-powered backend websites. In this article, we are going to learn how to generate a PHP QR code. 

QR code

In the modern world, we can find QR codes everywhere. QR codes can be termed machine-readable code consisting of an array of black and white squares. It is primarily used for storing URLs or other information to be read using a smartphone camera. One of the key factors for India’s digital transactions is because of QR codes. QR codes have helped businesses, and users transfer money to each other using UPI instantly. The use case of the application is plenty, but we’ll be not dwelling on it. 

Requirements

Before moving on to the implementation, let’s take a look at the requirements for this tutorial.

PHP: PHP must be installed in your OS.

Xampp: Xampp is used to serve webpages on the internet. You can install it from here

Get Started

phpqrcode

There are many ways to generate QR codes using PHP, but the easiest way is by using the PHP QR Code library(phpqrcode). First, download the zip file from the official site

Implementation

Now, create a folder named php-qr inside the folder xampp/htdocs/.

Now, paste the phpqrcode zip file here and extract it. Make a new file named index.php, here’s where our server code starts. In the index.php file, let’s code the following:

<?php echo "Hello Codedamn!"; ?>
Code language: PHP (php)

Now, let’s start our server and check whether everything is working fine or not. Search xampp on your local computer and open it. Now, start the apache server on your machine.

Open any web browser(Google Chrome/Brave/Opera), in the URL type localhost/php-qr.  

You might be able to view the text “Hello Codedamn!”. If it’s visible, we’re good to go! Now, make another folder named qrcodes to store QR codes in png format.

Write the following PHP code in the index.php file.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>QR Code</title> </head> <body> <?php include 'phpqrcode/qrlib.php'; $text = "codedamn.com"; QRcode::png($text, 'qrcodes/image.png'); ?> <img src="qrcodes/image.png" alt=""> </body> </html>
Code language: PHP (php)

In the above code, I have initialized the document with HTML code. In the HTML body, I have included PHP to add dynamic properties to the website. 

First, include the library in the following manner: include phpqrcode/qrlib.php;

Now, we have included a variable $text equal to “codedamn.com”. 

Finally, we can generate QR in the following manner QRcode::png($text, $file, $ecc, $pixel_size, $frame_size);

  1. $text: The text parameter contains the data to be stored in the QR code.
  2. $file: This parameter contains the file location to save the generated QR code.
  3. $ecc: This parameter refers to the error correction capability of QR. The values that can be used are L, M, Q, and H, according to your needs.
  4. $pixel_size: It refers to the pixel size of the QR code.
  5. $frame_size: It refers to the size of the QR code. The values could vary from 0 to 10.

Note: The $text parameter is compulsory. 

In the above code, we had the text as "codedamn.com" and the file location to be qrcodes/image.png. Finally, we can display the image using the same file location using HTML <img> tags. 

You directly display the QR code without storing the QR image by neglecting the $file parameter as follows. QRcode::png($text);

Conclusion

As we saw, generating a PHP QR code for your text is a simple process using the phpqrcode library. QR codes are really powerful and you could implement them in your next project. I hope you learned how to implement a QR code generator through this tutorial. You can check out more articles similar to this on the Codedamn platform. Thank you for reading 🙂

Sharing is caring

Did you like what Fidal Mathew wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far