QR Codes with PHP and Google Charts

Looking for a simple tutorial on generating QR codes using PHP and Google Charts? Read on...

I have occasionally seen QR Codes around the place, more often than not on real estate signs, though I have never had too much of a need to look into them further - that is until a recent client request to have QR Codes dynamically generated.

For those who don't know and haven't had a look at the above link, QR Codes are essentially a 2 dimensional bar code, allowing you to store more data in them than a normal bar code.

People can then scan the code or take a photo of it and use a decoder (either on their smart phone or there are some good decoders on the internet) to find out a wealth of information from the code.

It makes sense to have QR codes on real estate signs, as a quick scan/photo will give the potential buyer all the information they need to know about that place, without having to search high and low on the internet to find it.

So...how do you generate these codes?

I came across this tutorial which at first glance looked good, but upon reading through the code found several major problems, ranging from spelling errors to undefined variables.

So, credit to them for giving me a head start, but here is a working, updated version of the code you need to get your QR codes generating.

First things first, create a function to do all the hard work...

<?php

    function generateQRwithGoogle($chl,$dims ='150',$EC_level='L',$margin='0') 
    {
        $chl = urlencode($chl); 
        
        $qr = '<img src="https://chart.apis.google.com/chart?chs=' . $dims . 
        'x' . $dims . '&cht=qr&chld=' . $EC_level . '|' . $margin .
        '&chl=' . $chl . '" alt="QR code" widthHeight="' . $dims . 
        '" widthHeight="' . $dims . '"/>';
        
        return $qr;
    }
?>

Second, set some content. This could be either a hard coded string, or if you want to be dynamic then you could use a posted variable or database value.

<?php $qr_content = $_POST['qr_content']; ?>

Finally, run the function to generate the code:

<?php $qr = generateQRwithGoogle($qr_content); ?>

Below is a one page script that will take content from a form input and convert it into a QR code.

<?php

    function generateQRwithGoogle($chl,$dims ='150',$EC_level='L',$margin='0') 
    {
        $chl = urlencode($chl); 
        
        $qr = '<img src="https://chart.apis.google.com/chart?chs=' . $dims . 
        'x' . $dims . '&cht=qr&chld=' . $EC_level . '|' . $margin .
        '&chl=' . $chl . '" alt="QR code" widthHeight="' . $dims . 
        '" widthHeight="' . $dims . '"/>';
        
        return $qr;
    }

    $qr = null;
    
    if(isset($_POST['qr_content']))
    {
        $qr_content = $_POST['qr_content'];
        
        $qr = generateQRwithGoogle($qr_content);
    }

?>

<html>
    <head>
        <title>QR Code Generator</title>
    </head>
    
    <body>

        <?php if($qr): ?>
            <div class="qr_code">
            
                <?php echo $qr; ?>
            
            </div>
        <?php endif; ?>

        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

            <div class="input">
            
                <label for="qr_content">Enter Content to Encode</label><br />
                <textarea name="qr_content" id="qr_content"></textarea>
            
            </div>
            
            <input type="submit" value="Generate QR Code" />

        </form>

    </body>
</html>

You should end up with something like the following, which is simply the URL  https://www.templemantwells.com.au turned into a code:

Hope that helps, have fun creating your QR codes!