How to generate QR Code easily with one line of php
In this article your will learn how to generate QR Code easily with PHP. In today’s era where most of the technology running over mobile devices, reading long URL, email or other strings to send to some one else is difficult, that’s why we use QR Codes, as it is machine readable it can not be read by humans normally.
we can write our own code to draw such encrypted image to encode our strings, but that will be something like re-inventing of a wheel when we are having and easy API by google which has the simplest possible implementation.
Google provide a URL, We can write it with different parameters and assign it to src attribute of an img tag and we will have our QRCODE ready as below.
How to generate QR Code
copy the following code and paste in your php or html page and see how it looks
1 2 3 4 5 |
<img alt="QR Code" title="Coding sips" src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http://www.codingsips.com&choe=UTF-8" /> |
The following are different parameter explanations from Google documentation
Parameter | Required or Optional | Description |
---|---|---|
cht=qr | Required | Specifies a QR code. |
chs=<width>x<height> | Required | Image size. |
chl=<data> | Required | The data to encode. Data can be digits (0-9), alphanumeric characters, binary bytes of data, or Kanji. You cannot mix data types within a QR code. The data must be UTF-8 URL-encoded. Note that URLs have a 2K maximum length, so if you want to encode more than 2K bytes (minus the other URL characters), you will have to send your data using POST. |
choe=<output_encoding> | Optional | How to encode the data in the QR codes. Here are the available values:
|
chld=<error_correction_level>|<margin> | Optional |
|
QR Code supports Max String lengths
according to wikipedia
the following are max character lengths for different type of strings.
- Numeric only Max. 7,089 characters (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
- Alphanumeric Max. 4,296 characters (0–9, A–Z [upper-case only], space, $, %, *, +, -, ., /, : )
- Binary/byte Max. 2,953 characters (8-bit bytes) (23624 bits)
- Kanji/Kana Max 1,817 characters
PHP Easy Implementation
To reuse and generate the QR Code in our project easily with different strings and sizes, I wrote a PHP function using which we can easily output QR Code without writing that lengthy Google URL and difficult parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function makeQr($string, $size='200'){ //google api url $src = 'https://chart.googleapis.com/chart?'; //size of qr code $src .='chs='.$size.'x'.$size; $src .='&cht=qr'; //url-encoded string you want to change into a QR code $src .='&chl='.$string; //encoding (optional) $src .='&choe=UTF-8'; //make image echo '<img src="'.$src.'" title="'.$string.'" />'; } |
Now try to generate few QR Codes using above function
1 2 |
//generate a qr code makeQr('http://www.codingsips.com'); |
1 2 |
//generate 1 more qr code makeQr('I am Alam, PHP Developer'); |
1 2 |
//generate another this time with 300 size makeQr('alamnaryab@gmail.com',300); |
Create dynamic QR Code with Live demonstration Live QR Code
now it is more easy to generate QR Code with this function.
Hope this helped, let me know if you have any problem any updates on this API.
Comments