QR Codes with PHP

APD Toronto
APD Toronto used Ask the Experts™
on
Hi Experts,

What is the quickest way I can produce a dynamic text QR code using PHP?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
IIRC the TCPDF library has a QRCode component.  JPGraph may have something, too.  

Google has a service that I've used with great success.
<?php // demo/qr_code.php

/**
 * GENERATE QR CODE IMAGES
 * SEE https://google-developers.appspot.com/chart/infographics/docs/overview
 * SEE https://google-developers.appspot.com/chart/infographics/docs/qr_codes
 * SAMPLE https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=Hello%20world
 * ACHTUNG http://www.mediapost.com/publications/article/160932/qr-malware-surfaces-on-apps.html
 */
error_reporting(E_ALL);

// THE DIRECTORY FOR THE QR CODES
$dir = 'storage';

// DETERMINE THE SIZE
$s = "144";
if (!empty($_GET["size"]))
{
    $s = strtoupper(trim($_GET["size"]));
    switch($s)
    {
        case "S" : $s =  72; break;
        case "M" : $s = 144; break;
        case "L" : $s = 288; break;
        case "G" : $s = 547; break; // LARGEST FROM TESTS ON 2011-10-20
    }
}
$chs = $s . 'x' . $s;

// IF THERE IS DATA
$q = NULL;
if (!empty($_GET["q"]))
{
    $q = $_GET["q"];

    // MAKE A FILE NAME (UNDERSCORES REPLACE INAPPROPRIATE CHARACTERS)
    $f = $q;
    $f = preg_replace('#[^ A-Z0-9/:?&\+\.]#i', NULL, $f);
    $f = str_replace(' ', '_', $f);
    $f = preg_replace('#[^A-Z0-9]#i', '_', $f);
    $f = preg_replace('#_+#', '_', $f);

    // CALL THE QR CODER
    $url
    = 'https://chart.googleapis.com/chart?'
    . 'cht=qr'          // CREATE A QR CODE
    . '&'
    . 'choe=ISO-8859-1' // ENCODING
    . '&'
    . "chs=$chs"        // PIXEL DIMENSIONS (SQUARE)
    . '&'
    . 'chld=Q'          // QUALITY ERROR RECOVERY IMPLIES UP TO 67 ALPHA-NUMERICS IN LEVEL 4
    . '&'
    . 'chl='            // THE URLENCODED STRING
    . urlencode($q);
    ;
    $qrc = file_get_contents($url);

    // WRITE THE IMAGE
    $lnk = $dir . DIRECTORY_SEPARATOR . "QR_CODE_$f" . '.png';
    file_put_contents($lnk, $qrc);

    // CONSTRUCT THE RESPONSE
    $txt = htmlentities($q);
$out = <<<OUT
<p>Voila! Here is the QR code for <b>$txt</b></p>
<p><img src="$lnk" title="$f" /></p>
<p>Right-click and save the image, or use this: <a target="_blank" href="$lnk">$f.png</a></p>
OUT;
    echo $out;
}

$form = <<<FORM
Enter up to 67 characters to see the QR code (maybe a URL?)
<form>
<input type="text"   name="q"    value="$q" size="67" style="font-family:Courier;" /><br/>
<input type="radio"  name="size" value="S" />Small (probably too small)<br/>
<input type="radio"  name="size" value="M" />Medium (default value; probably just right)<br/>
<input type="radio"  name="size" value="L" />Large<br/>
<input type="radio"  name="size" value="G" />Gigantic<br/>
<input type="submit"             value="Get QR code" />
</form>
FORM;

echo $form;

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
This site seems to treat QR codes a little like bit.ly treats URLs:
http://www.qr-code-generator.com/
Chris Harte2015 Top Expert (Most Article Points)
Commented:
Personally I would save myself the effort and use an existing generator

http://goqr.me/

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial