How to generate QR Code in PHP

There are accessible a few sites like http://www.qr-code-generator.com, http://www.goqr.me, and www.qrstuff.com, etc for online QR code generation sites, and  you're feeling that however we tend to build it victimization PHP script. We can also generate QR code generator script with the help of PHP QR code libraries or using Google’s API chart. The fundamental distinction that in the event that you assemble QR code with PHP QR code library, the whole control remain adjacent to you, interestingly with Google outline API, you have to utilize an outsider site. In this article I will be more concentrate on the utilization of QR CODE PHP Library.

LIVE DEMO

QR code Generate using PHP QR CODE library

As indicated by authority site PHP QR code library is open source (LGPL) library for creating QR Code, 2-dimensional standardized tag. In view of lib qrlib code C library, gives API to making QR Code standardized identification pictures (PNG, JPEG on account of GD2). You can arrange the code according to your prerequisite.

In this demo we make dynamic qrcode generator, we can change size, content and size of the standardized identification. We can change the conduct of QR code . You can use below types of string for code generation:

  • Website url
  • Mobile number
  • SMS
  • Name of card on visting card
  • Amount
  • Plain Text

The QR generation code steps are given below:

  • Download QR lib (QR Library)
  • Include that library

You can make one directory for save image other below code for generate folder. We can configure different parameter like error corresponding level, matrix point size etc. The value of matrix level sizes are 1 to 10.And error corresponding level are L, M, Q and H.·         We can also save image permanent or temporarily. The QR code function is details below. The parameter descriptions are below:

QRcode::png(‘Qr Test’, $file-name, $errorLevel, $matrixSize, 2);    

String $text text which is provided string to encode.

String $outfile (optional) output file name, if false outputs to browser with required headers.

The third parameter determines the level of mistake amendment for band-produced code transmitted as a solitary line. Here you characterize what number of codewords (8-bits per codeword) can be reestablished in the harmed picture of QR code with the revision calculation Reed-Solomon mistake. More elevated amounts of blunder amendment prompts to lower thickness code information of a given size.

The fourth parameter determines the span of every square in the code (in px). Every square in the code (additionally called "pixel" or "module") is 4 × 4px defaults. The fifth parameter determines the extent of the white fringe around the code, measured in the span of "module" (for our situation - 16px on every side of the module 4 × 4px).

The next parameter is Integer $margin (optional) code margin (silent zone) in ‘virtual’ pixels. The last parameter isBoolean $saveandprint (optional) if true code is outputed to browser and saved to file, otherwise only saved to file. It is effective only if $outfile is specified.

The full source code are given below:

if (!file_exists($PNG_TEMP_DIR))
    mkdir($PNG_TEMP_DIR);
$errorCorrectionLevel = 'L';
        
if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L', 'M', 'Q', 'H')))
    $errorCorrectionLevel = $_REQUEST['level'];
    $matrixPointSize = 1;
    
if (isset($_REQUEST['size']))
    $matrixPointSize = min(max((int) $_REQUEST['size'], 1), 10);

if (isset($_REQUEST['data'])) {
    if (trim($_REQUEST['data']) == '')
        die('data cannot be empty! back');
    // user data
    if ($_REQUEST['frm_type'] == '1') {
        $filename = $PNG_TEMP_DIR . '' . Date("Y-m-dhsi") . '.png';
        $data_val1 = $_REQUEST['data'];
        QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2);
    } else if ($_REQUEST['frm_type'] == '2') {
        $data_val2 = $_REQUEST['data'];
        $filename2 = $PNG_TEMP_DIR . '' . Date("Y-m-dhsi") . '.png';
        QRcode::png($_REQUEST['data'], $filename2, $errorCorrectionLevel, $matrixPointSize, 2);
    } else if ($_REQUEST['frm_type'] == '3') {
        $data_val3 = $_REQUEST['data'];
        $filename3 = $PNG_TEMP_DIR . '' . Date("Y-m-dhsi") . '.png';
        QRcode::png($_REQUEST['data'], $filename3, $errorCorrectionLevel, $matrixPointSize, 2);
    } else if ($_REQUEST['frm_type'] == '4') {
        $data_val4 = $_REQUEST['data'];
        $filename4 = $PNG_TEMP_DIR . '' . Date("Y-m-dhsi") . '.png';
        QRcode::png($_REQUEST['data'], $filename4, $errorCorrectionLevel, $matrixPointSize, 2);
    } else if ($_REQUEST['frm_type'] == '5') {
        $data_val5 = $_REQUEST['data'];
        $filename5 = $PNG_TEMP_DIR . '' . Date("Y-m-dhsi") . '.png';
        QRcode::png($_REQUEST['data'], $filename5, $errorCorrectionLevel, $matrixPointSize, 2);
    } else {

        $data_val1 = $data_val2 = $data_val3 = $data_val4 = $data_val5 = "";
    }

Note Down that you should remove space from starting and ending syntax. The below code use for generate QR code.

 

If you wish to display the demo then click on the below link.If you like this post, then give the comment below.

 

LIVE DEMO

Let's Think together, Say Something !