Generate QR code using PHP

Today, we’ll explain to you how to generate QR code using PHP.

A QR code (Quick Response code) is a type of matrix barcode used mostly for online shopping and digital payment. It is an optical label which contains some amount of information about the product or item.

In this article, we’ll show you simple ways to generate QR code in PHP. Here we will use the QR code generator PHP library.

Table of contents

  1. Syntax
  2. Parameters
  3. Example 1: Generate QR code
  4. Example 2: Store generated QR code in directory

1. Syntax

2. Parameters

  • $text: This is the input parameter for which the QR code needs to generate. It is a mandatory parameter.
  • $filename: It specifies the path or location to save the generated QR code.
  • $err_correction: It specifies the error correction capability of the QR code. There are 4 different levels of error correction.
    • L[ow] – up to 7% damage
    • M[edium] – up to 15% damage
    • Q[uality] – up to 25% damage
    • H[igh] – up to 30% damage
  • $pixel_size: It specifies the pixel size of the QR code.
  • $frame_size: It specifies the complete size of the QR code.

3. Example 1: Generate QR code

Let’s create a QR code with minimum code. Refer to the following file structure for example.

File Structure

  • generate-qr-code-php
    • library
      • phpqrcode
    • images
    • index.php

Click here to download QR code generator PHP library and add it under library folder.

index.php

The following output will appear when you run scripts that have default frame and pixel size.

Output - Example 1 - Generate QR code using PHP - Clue Mediator
Output – Example 1 – Generate QR code using PHP – Clue Mediator

4. Example 2: Store generated QR code in directory

In this example, we will create a QR code and store it in a given directory. Using the uniqid() function, we will generate a unique name for the image based on microtime.

Let’s use the above file structure and update the following code in the index.php file.

index.php

Run the above code and check the output in the browser.

Output - Example 2 - Generate QR code using PHP - Clue Mediator
Output – Example 2 – Generate QR code using PHP – Clue Mediator

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

Demo & Source Code

GitHub Repository
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *