Tuesday, January 3

QR code – How to generate QR code in php

How to generate QR code in PHP

In this post we will generate QR code (Quick Response Code) using php. QR code is used to read data with smart devices for easy.
Hear pass you uel and it will generate qr code of you url.

<?php
// include QR Generator class
include "QRGenerator.php";

// check for post method call.
if($_SERVER["REQUEST_METHOD"] === "POST")
{
    $set_url = $_POST['set_url'];
    $set_size = $_POST['set_size'];
    $example_1 = new QRGenerator($set_url,$set_size);
    $qr_img_1 = "<img src=".$example_1->generate().">";
    $html_content ='<form method="post" action="">
        <table>
            <tr>
                <td colspan="2">'.$qr_img_1.'</td>
            </tr>         
            <tr>
                <td>Text/URL</td>
                <td><input type="text" name="url" /></td>
            </tr>         
            <tr>
                <td>Size</td>
                <td><input type="text" name="size" /> <i>min: 100 and max: 800</i></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" name="generate" value="Generate" /></td>
            </tr>
        </table>
    </form>';
}
else
{
    $html_content ='<form method="post" action="">
    <table>
        <tr>
            <td>Text/URL</td>
            <td><input type="text" name="set_url" /></td>
        </tr>         
        <tr>
            <td>Size</td>
            <td><input type="text" name="set_size" /></td>
        </tr>
        <tr>
            <td rowspan="2"><i>min: 80 and max: 800</i></td>
        </tr>
        <tr>          
            <td>
                <input type="submit" name="generate" value="Generate" />
            </td>
            <td>
                <input type="reset" name="reset" value="reset" />
            </td>
        </tr>
    </table></form>';
    $example_1 = new QRGenerator('http://onlinecode.org',100);
    $qr_img_1 = "<img src=".$example_1->generate().">";
     
    $example_2 = new QRGenerator('https://in.yahoo.com/',100);
    $qr_img_2 = "<img src=".$example_2->generate().">";

    $example_3 = new QRGenerator('https://www.google.com/',100,'ISO-8859-1');
    $qr_img_3 = "<img src=".$example_3->generate().">";

    $example_4 = new QRGenerator('https://www.bing.com',100,'ISO-8859-1');
    $qr_img_4 = "<img src=".$example_4->generate().">";

    $html_content .='
    <table>
        <tr>
            <td>'.$qr_img_1.'</td>
            <td>'.$qr_img_2.'</td>
        </tr>         
        <tr>
            <td>'.$qr_img_3.'</td>
            <td>'.$qr_img_4.'</td>
        </tr>
    </table>';
}
$pre = 1;
$page_title = "How to generate QR code in PHP | onlinecode";
$page_heading = "How to generate QR code in PHP example - onlinecode";
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>
            <?php
                // add page title
                echo $page_title;
            ?>
        </title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <style type="text/css">
          img {border-width: 0}
          * {font-family:'Lucida Grande', sans-serif;}
        </style>
    </head>
    <body>
        <div>
            <h2>
                <?php
                    // add page heading
                    echo $page_heading;
                ?>
            </h2>
            <p>
                <!-- add this tag after last badge tag -->
                <script type="text/javascript">
                    (function() {
                        var qr_var = document.createElement('script');
                        // assign javascript
                        qr_var.type = 'text/javascript';
                        // assign async
                        qr_var.async = true;
                        // assign javascript                    
                        qr_var.src = 'https://apis.google.com/js/plusone.js';
                        // get script
                        var get_script = document.getElementsByTagName('script')[0];
                        get_script.parentNode.insertBefore(qr_var, get_script);
                    })();
                </script>
        </div>
        <hr />     
        <p>
        <?php
            if(!isset($pre)) {
        ?>
            <pre>
                <?php
                    print_r($html_content);
                ?>
            </pre>
        <?php } else  { ?>
            <pre>
                <?php
                    print_r($html_content);
                ?>
            </pre>   
        <?php  } ?>
        </p>
    </body>
</html>

class of QRGenerator

 <?php
// assign class QRGenerator
class QRGenerator {
    // assign protected variable
    protected $qr_size;
    protected $qr_data;
    protected $qr_encoding;
    protected $qr_errorCorrectionLevel;
    protected $qr_marginInRows;
    protected $qr_debug;
  
    // assign construct
    public function __construct($qr_data='http://onlinecode.org',$qr_size='100',$qr_encoding='UTF-8',$qr_errorCorrectionLevel='L',$qr_marginInRows=4,$qr_debug=false)
    {        
        $this->qr_data = urlencode($qr_data);
        $this->qr_size = ($qr_size>80 && $qr_size<800)? $qr_size : 100;
      
        $this->qr_encoding = ($qr_encoding == 'Shift_JIS' || $qr_encoding == 'ISO-8859-1' || $qr_encoding == 'UTF-8') ? $qr_encoding : 'UTF-8';
      
        $this->qr_errorCorrectionLevel = ($qr_errorCorrectionLevel == 'L' || $qr_errorCorrectionLevel == 'M' || $qr_errorCorrectionLevel == 'Q' || $qr_errorCorrectionLevel == 'H') ?  $qr_errorCorrectionLevel : 'L';
      
        $this->qr_marginInRows=($qr_marginInRows>0 && $qr_marginInRows<10) ? $qr_marginInRows:4;
        $this->qr_debug = ($qr_debug==true)? true:false;   
    }
    // function generate
    public function generate()
    {        
        $QRLink = "https://chart.googleapis.com/chart?cht=qr&chs=".$this->qr_size."x".$this->qr_size.               
                   "&chl=" . $this->qr_data .
                   "&choe=" . $this->qr_encoding .
                   "&chld=" . $this->qr_errorCorrectionLevel . "|" . $this->qr_marginInRows;
        if ($this->qr_debug)
        {
            echo $QRLink;
        }
        return $QRLink;
    }
}
?>

 

No comments:

Post a Comment

10 Best Chatting Apps In India

  The modern world is all about Internet. There was a time when people use to pay high telephone bills to stay in touch with their friends a...