How to generate QR Code easily with one line of php

QR Code

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

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:

  • UTF-8 [Default]
  • Shift_JIS
  • ISO-8859-1
chld=<error_correction_level>|<margin> Optional
  • error_correction_level – QR codes support four levels of
    error correction to enable recovery of missing, misread, or obscured
    data. Greater redundancy is achieved at the cost of being able to store
    less data. Here are the
    supported values:

    • L – [Default] Allows recovery of up to
      7% data loss
    • M – Allows recovery of up to 15% data loss
    • Q – Allows recovery of up to 25% data loss
    • H – Allows recovery of up to 30% data loss
  • margin – The width of the white border around the data portion
    of the code. This is in rows, not in pixels. The default value is

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.

Now try to generate few QR Codes using above function

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.


    Write a Reply or Comment

    Your email address will not be published. Required fields are marked *