Dynamically generating a QR code using PHP
There are a number of open source libraries available online which can be used to generate a Quick Response(QR) Code. A good open source library for QR code generation in PHP is available in sourceforge. It just needs to be downloaded and copied in the project folder. This includes a module named “phpqrcode” in which there is a file named “qrlib.php”. This file must be included in the code to use a function named ‘png()’, which is inside QRcode class. png() function outputs directly a QR code in the browser when we pass some text as a parameter, but we can also create a file and store it.
Syntax:
QRcode::png($text, $file, $ecc, $pixel_Size, $frame_Size);
Parameters: This function accepts five parameters as mentioned above and described below:
- $text: This parameter gives the message which needs to be in QR code. It is mandatory parameter.
- $file: It specifies the place to save the generated QR.
- $ecc: This parameter specifies the error correction capability of QR. It has 4 levels L, M, Q and H.
- $pixel_Size: This specifies the pixel size of QR.
- $frame_Size: This specifies the size of Qr. It is from level 1-10.
Example 1: PHP program to generate QR Code.
<?php // Include the qrlib file include 'phpqrcode/qrlib.php'; // $text variable has data for QR $text = "GEEKS FOR GEEKS"; // QR Code generation using png() // When this function has only the // text parameter it directly // outputs QR in the browser QRcode::png($text); ?> |
Output:

Note: This output is directly generated in the browser. This code will not run on an online IDE because it can’t include ‘phpqrcode’ module.
Example 2: PHP program to generate QR Code and create file.
<?php // Include the qrlib file include 'phpqrcode/qrlib.php'; $text = "GEEKS FOR GEEKS"; // $path variable store the location where to // store image and $file creates directory name // of the QR code file by using 'uniqid' // uniqid creates unique id based on microtime $path = 'images/'; $file = $path.uniqid().".png"; // $ecc stores error correction capability('L') $ecc = 'L'; $pixel_Size = 10; $frame_Size = 10; // Generates QR Code and Stores it in directory given QRcode::png($text, $file, $ecc, $pixel_Size, $frame_size); // Displaying the stored QR code from directory echo "<center><img src='".$file."'></center>"; ?> |
Output:

Note: The output of both examples are different. In the first example, the output will display in default frame and pixel size which is generated directly on browser whereas the output of the second example is a ‘png’ file with pixel and frame size as 10 stored in a directory.
Recommended Posts:
- How to merge properties of two JavaScript objects dynamically?
- How to create an image element dynamically using JavaScript ?
- How to get the javascript function parameter names/values dynamically?
- How to change style attribute of an element dynamically using JavaScript ?
- Generating Random String Using PHP
- Mathematics | Generating Functions - Set 2
- Generating subarrays using recursion
- Generating OTP (One time Password) in PHP
- HTML | <code> Tag
- How to write a Pseudo Code?
- C# | How to get hash code for the specified key of a Hashtable
- Code introspection in Python
- HTML | DOM Code Object
- Making a QR code for a website
- How to share code between Node.js and the browser?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



