How to create QR code by using Google Chart API in PHP
The full form of QR code is Quick Response code. It is a two dimensional code designed specifically to be read by smartphones. It is a type of matrix barcode. The code represents a picture where black squares of various sizes are arranged in a square pattern on a white background. This code can be used to encode any information such as text, a URL, or other data. Here, I will show you how to create your own QR code for any data by using Google Chart API.
Google Chart API allows us to retreive corresponding QR code for our desired data by making URL GET or POST request to its infographics server. The URL to which the request is to be made is https://chart.googleapis.com/chart. We need to pass some GET query parameters to this root URL in order to retreive QR code correctly. These parameters are as follows:
- chs - This parameter defines the size of the image returned in pixels. The format should be <width>x<height>.
- cht - This defines the type of the image to be returned. It should be set to qr for QR code.
- chl - This parameter specifies the data to be encoded.
Create a new PHP file and name it as qrcode.php and add the following code to it:
<?php function printQRCode($url, $size = 100) { return '<img src="http://chart.apis.google.com/chart?chs=' . $size . 'x' . $size . '&cht=qr&chl=' . urlencode($url) . '" />'; } ?> <html> <head> <title>QR Code Test</title> <body> <?php echo printQRCode("http://www.botskool.com"); ?> </body> </html>
The output will be something like this:
Now, open your smartphone and try to scan this QR code. You will be re-directed to the homepage of bOtskOOl. You can encode your own data in a similar fashion.