Skip to content

Generating a QR Code for the Current URL with PHP and Google Charts

The whole idea is to make it easy for smart phone users to get complicated URL from your blog without having to type it manually. This way, they can easily bookmark or share it by simply pointing a phone’s camera to it and using a bar code reader application.

QR Codes storing addresses and URLs may appear in magazines, on signs, buses, business cards, or on just about any object that users might need information about. Users with a camera phone equipped with the correct reader application can scan the image of the QR Code to display text, contact information, connect to a wireless network, or open a web page in the phone’s browser. This act of linking from physical world objects is known as a hardlink or physical world hyperlinks.

Google’s mobile Android operating system supports the use of QR codes by nativelly including the barcode scanner (ZXing) on some models and the browser supports URI redirection, which allows QR Codes to send metadata to existing applications on the device. Nokia’s Symbian operating system is also provided with a barcode scanner, which is able to read QR Codes, while mbarcode is a QR code reader for the Maemo operating system.

Google has a nice API inside its Google charts that we are going to use in today’s article. We will write a simple function that you can easily use to generate the QR code as an image. Thus, our function will return an image.

And here it is:

[code lang=”php”]
function generateQR($url, $width = 150, $height = 150) {
$url = urlencode($url);
$image = ”;

return $image;

If you are wondering what will this function generate, you can see how the QR code for this URL looks like here.

Function is receiving 3 parameters, first is the actual URL we would like to generate a QR code for and other two are width and height of the resulting image.

And that is it. Very simple and effective. All you have to do is call this function whenever and wherever you need a QR code for the URL.

But wait, this is not very clever. Do you really want to query and generate the image every time a user comes to your URL? Of course not. So here is a prototype of a function that should enable you to save the generated image to your server, and then use it as you would use any other image:

[code lang=”php”]
function saveImage($your_url, $width, $height, $file) {

$your_url = urlencode($your_url);
$url = ‘’.$width.’x’.$height.’&cht=qr&chl=’.$your_url’;

// initialize cURL settings
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// fetch raw data
$rawdata = curl_exec($ch);

// convert it to a GD image and save
$img = imagecreatefromstring($rawdata);
/* your image saving functions come here */

And now you learned how to get an image using cURL from Google Charts. So how to use this? After you are done with the article writing, I would call this function which would generate the QR code and save it to server. Then just include this image in the article using standard img tag.

So, you have got your own QR code generator and can customize it any way you want.


2 thoughts on “Generating a QR Code for the Current URL with PHP and Google Charts”

  1. Pingback: Generating a QR Code for the Current URL with PHP and Google Charts – CodeForest « QR Code Fun

Comments are closed.