Friday, April 1, 2011

QR Code - PHP

Hi,
Quick Response code are getting really popular. It was introduced by Toyota. Its more commonly used in Japan. For those who are wondering what a QR Code is, this is how it looks like:


The above is the QR Code of "http://php-drops.blogspot.com/". It can contain website URLs, text or other data. It is similar to bar code, but more portable in various devices. The beauty of this is, all you need to decrypt the code is a smart phone with a camera. You need to have a reader in it. Take a picture of the above and you will get the details of the code in your mobile on the go. Here is a video I found in Youtube which demo's how to use it:




Now creating QR Code is not a problem what so ever. You need to import a class, "qrlib.php" and use the following function in the class, QRcode::png($data,$png_file_path). Click here to download the PHP package for generating a QR Code. There are many other classes, but you don't need to bother about them, all you have to do is to create a php file, "example.php" and add the following code:

<?php
session_start();
require("qrlib.php");
$filename="temp".session_id().".png";
QRcode::png("http://php-drops.blogspot.com/", $filename,'S',6,1);
echo '<img src="'.$filename.'" />';
?>

The output will be as:


That's it. Download qr_code.zip


21 comments:

  1. How would I pass a variable into:
    QRcode::png("http://php-drops.blogspot.com/", $filename,'S',6,1);

    IE:
    QRcode::png($var, $filename,'S',6,1);


    Reason I ask is to pull data from MySQL and populate the QR Code with several variables.

    ReplyDelete
  2. You pretty much gave the solution yourself. Fetch the data or collection of data in a variable, $var and pass it as parameter.

    e.g.
    $arr=mysql_fetch_array(mysql_query("select * from doctors where id=1"));
    $var="Name: {$arr['name']},Country: {$arr['country']},Gender: {$arr['gender']}";

    QRcode::png($var, $filename,'S',6,1);

    ReplyDelete
    Replies
    1. If one of those variables is a website will the QR reader know to go to that website? Also, how do you add things like Title or is that pulled from the web page itself?

      Delete
  3. I want to do something similar to this but I need to fill the variable on the form once a user enters text in another field on the page.

    Or just create a random number so the new png file is unique for each visitor to the site.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. When the file is saved it ends up with a 1 on the front of it. How do I get rid of that?

    ReplyDelete
  6. If we don't want to generate a qr image file physically, how can we generate a qr barcode on the fly, and have a variable holding the barcode and passing around the php code?

    Thanks.

    ReplyDelete
    Replies
    1. $filename="temp".session_id().".png";
      QRcode::png("http://php-drops.blogspot.com/", $filename,'S',6,1);

      Run the above. $filename holds the path of the image, you can use it and pass it as and when needed.

      Delete
  7. hello,I'm pretty new to QR codes however I have to make a generator!This blog has helped me much but when I run the above code, I don't get the QR code but instead some strange characters and symbols, with a "PNG" as the first string. Can anyone help me with that? Thank you.

    ReplyDelete
    Replies
    1. It is still working for me. Can you re download the code and check. Also the folder needs to have write permission in case you are not in windows environment.

      Delete
    2. I am a mac user so I guess I have to have write permissions, that was the first warning it gave me. Let me do that. Thanks for your help

      Delete
    3. sorry to bother again, i now have read and write permissions on the folder as well as enclosed files but i still have this warning message: "Warning: imagepng() [function.imagepng]: Unable to open 'temp2aec16b7c8a838e2e0ed7d69f7ee69f3.png' for writing: Permission denied in .../qrimage.php on line 43". should I migrate to Windows or there's a way to solve this? Thanks for your help.

      Delete
    4. This is definitely a permission issue. try doing

      chmod -r 777 to the directory

      also check the owner of the file. I mean the "chown" command. I am working on Linux. It works fine, without permission issue.

      Best of luck

      Delete
  8. sorry for replying late. Thanks a million, I will update you on the progress.

    ReplyDelete
    Replies
    1. Thanks a lot, it worked. I will for sure mention you in my report...:)

      Delete
  9. how to convert png format to eps format

    ReplyDelete
  10. I really need help right now. I need to generate qr code image from the mysql data. I've tried the code above but I didn't get nothing. The data in mysql is from a submitted form. Once the submit button is pressed the database is filled and qrcode need to be generated from those data. How could I do this one? Sorry if I'm asking too much but I really lost right now.

    ReplyDelete
  11. I want to know how to store the decoded data from QRcode to mysql.

    ReplyDelete