This tutorial will teach you how to build a QR code generator with just HTML and PHP. The version of the QR code generator we will make here is for encoding URLs, but can very easily be modified to encode plain text, phone numbers, GEO codes, SMS messages and more. The buildQRcode class allows for several different encoding methods, but the front end I am building here is for encoding URLs, so if you have a need to encode something else ,simply make the changes to the simple front end script that apply. You can determine those changes from examining the buildQRcode class in buildQRcode.php which you can download using the link provided below. Let's get started...
Preview of QR code URL Encoder
Click to view working example
Example QR Code Encoder Output
This was generated using the code below:
Instructions for building a simple QR code generator:
First, download the buildQRcode.php file from my server here: Download from Jafty.com.
Next build the front end PHP script using both PHP and HTML like I have done below:
<h1>QR Code Generator</h1>
<form id="theform" name="theform" method="post" action="">
URL to Encode: <input type="text" id="jafty" name="jafty" value="http://jafty.com/blog" />
<br /><br />
QR Code Image Size:<select id="sz" name="sz">
<option value=100>100px by 100px</option>
<option value=150>150px by 150px</option>
<option value=200>200px by 200px</option>
<option value=250>250px by 250px</option>
<option value=300>300px by 300px</option>
<option value=400 selected>400px by 400px</option>
<option value=500>500px by 500px</option>
<input style="text-align:center;" type="submit" id="sbmt" name="sbmt" value="Generate QR Code" />
//proces when temp card button is hit:
echo "<p align='center'>Your URL Data is encoded in the below QR code:</p>";
//include QR code class
//create buildQRcode instance
$qr = new buildQRcode();
$encodedData = $_POST['jafty'];
$qr->link($encodedData);//change link to text to make this into a text encoder.
//specify size and get link to image
$QRsz = $_POST['sz'];
$QRsrc = $qr->get_link($QRsz);
echo "<p style='text-align:center'><img id='QRimg' src='".$QRsrc."' border='0'/></p>";
}//end if form submitted.
There you have it, let's look at what we did. I wont bother explaining the file you downloaded, but the above code is what's important for you to build your own qr code generator.
First I developed the HTML code at the top of the file. A simple form to collect the URL and size of the image to generate, bare essentials.
In the PHP code after the block of HTML, the first line following the opening PHP tag is to determine if a user submitted our form. The next line of PHP after the comments is to include our class that's in the downloadable file from above. After that, we create our buildQRcode instance and set the encodedData variable to the value of the URL field in the form . Then we call a function from the buildQRcode class, set the size and get the link. Finally we echo an img tag to show the QR code image in our webpage. It seems pretty simple from this point of view because all of the hard work is done in the include file, but that's a whole different tutorial. Have fun playing with your new QR code generator!
Scanning the Generated QR Code
when you build your encoder and test it, you can either use an app on your iPhone or iPad to read it or if you don't have that luxury, use an online QR code scanner like the one at: