How To Generate QR Code In PHP Example

May 05, 2021 . Admin

Hi Dev,

In this blog, I will learn you generating a QR code using PHP.

There are a number of open source libraries available online which can be acclimated to engender 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 replicated in the project folder. This includes a module denominated “phpqrcode” in which there is a file denominated “qrlib.php”. This file must be included in the code to utilize a function denominated ‘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 engender a file and store it.

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
  // Include the qrlib file
  include 'phpqrcode/qrlib.php';
  // $text variable has data for QR 
  $text = "Welcome To";
  // QR Code generation using png()
  // When this function has only the
  // text parameter it directly
  // outputs QR in the browser



Example : 2
    //Include the necessary library for Ubuntu
    //Set the data for QR
    $text = "";
    //Set the filename with unique id
    $filename = uniqid().".png";
    //Set the error correction Level('L')
    $e_correction = 'L';
    //Set pixel size
    $pixel_size = 12;
    //Set the frame size
    $frame_size = 8;
    //Generates QR image
    QRcode::png($text, $filename, $e_correction, $pixel_size, $frame_size);
    //Display the QR image
    echo "";



I Hope It will help you..