欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Dynamically generating a QR code with PHP

shiping1 的头像
 

I'm trying to generate QR codes on my website. All they have to do is have a URL in them, which a variable on my site will provide. What would be the easiest way to do this?

shareimprove this question
 
4 
don't re-invent the wheel! below answers are perfect –  Jordan Arseno May 9 '11 at 22:58
1 
Akor: could you please un-accept my answer? Google has deprecated the API upon which my answer relies, therefore it serves little purpose from now on. –  David Thomas Mar 10 at 22:42

It's worth adding that, in addition to the QR codes library posted by @abaumg, Google provides a QR Codes API QR Codes APImany thanks to @Toukakoukan for the link update.

To use this , basically: 正确答案 

https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fwww.google.com%2F&choe=UTF-8
  • 300x300 is the size of the QR image you want to generate,
  • the chl is the url-encoded string you want to change into a QR code, and
  • the choe is the (optional) encoding.

The link, above, gives more detail, but to use it just have the src of an image point to the manipulated value, like so:

<img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fwww.google.com%2F&choe=UTF-8" title="Link to Google.com" />

Demo:

shareimprove this answer
 
3 
Are there any limitations for using the Google API?. I mean limitations as: amount of requests in a given time, etc. –  Lobo Sep 27 '12 at 15:11
3 
It should be noted that this has been deprecated. Google plans to continue support until 2015. So as always, caveat emptor. –  blainarmstrong Jan 10 '13 at 1:26
3 
@Lobo According to their documentation: "There's no limit to the number of calls per day you can make to the Google Chart API. However, we reserve the right to block any use that we regard as abusive.". – Alexandru Guzinschi Jan 11 '14 at 17:32
1 
I find answer by @abaumg better as it does not depends 3rd parties like google... –  arod Jan 25 '14 at 21:21
2 
have they come up with any alternatives ? I read Google is sunsetting charts API (QR Code) around April 2015. –  CodeMonkey Mar 3 '14 at 22:53
 

The easiest way to generate QR codes with PHP is the phpqrcode library.

shareimprove this answer
 
1 
+1 this answers the question more closely, you can generate the QR code on your own, without being limited by the Google limits enforced for requests. This will also make sure that your website works, even when the google endpoint contract changes. –  Sunny R Gupta May 7 '13 at 8:58
   
Fair warning: It looks like this library may be "dead" - it hasn't been updated by the author since October 2010 and has a couple of patches in the issue tracker that users of the library should consider applying before using the library. –  CubicleSoft Jun 29 '13 at 17:00
   
but how to convert it into mvc standard any ideas !!!! –  saurabh2836 Jun 19 '14 at 10:52

The phpqrcode library is really fast to configure and the API documentation is easy to understand.

In addition to abaumg's answer I have attached 2 examples in PHP fromhttp://phpqrcode.sourceforge.net/examples/index.php

1. QR code encoder

first include the library from your local path

include('../qrlib.php');

then to output the image directly as PNG stream do for example:

QRcode::png('your texte here...');

to save the result locally as a PNG image:

$tempDir = EXAMPLE_TMP_SERVERPATH;

$codeContents = 'your message here...';

$fileName = 'qrcode_name.png';

$pngAbsoluteFilePath = $tempDir.$fileName;
$urlRelativeFilePath = EXAMPLE_TMP_URLRELPATH.$fileName;

QRcode::png($codeContents, $pngAbsoluteFilePath); 

2. QR code decoder

See also the zxing decoder:

http://zxing.org/w/decode.jspx

Pretty useful to check the output.

3. List of Data format

A list of data format you can use in your QR code according to the data type :

  • Website URL: http://stackoverflow.com (including the protocole http://)
  • email address: mailto:name@example.com
  • Telephone Number: +16365553344 (including country code)
  • SMS Message: smsto:number:message
  • MMS Message: mms:number:subject
  • YouTube Video: youtube://ID (may work on iPhone, not standardized)

more data type on http://blog.thenetimpact.com/2011/07/decoding-qr-codes-how-to-format-data-for-qr-code-generators/

shareimprove this answer
 
   
this library doesnt seem to work for variety of inputs, small & simple text are okay though –  Abhishek K Mar 17 '14 at 12:55
   
Can you please provide an example? –  RafaSashi Mar 17 '14 at 13:34
   
nothing much, when i do $codeContents = "www.example.com" its ok but if i do $codeContents="www.example.com/some/view/aUdv4rP4BXXLQdpXZC6Gs5C6AfyJM4uB6ntVttI‌​r1B0=" it did not show correct image. –  Abhishek K Mar 18 '14 at 5:30
   
I cannot reproduce the error. It is working fine with QRcode::png('www.example.com/some/view/aUdv4rP4BXXLQdpXZC6Gs5C6AfyJM4uB6ntVttI‌​‌​r1B0='); maybe it has something to do with the headers –  RafaSashi Mar 18 '14 at 8:42
1 
Can i use php-qrcode library for commercial purpose like to develope a website which requires qr code in some of its part? –  Suneeta Singh Mar 21 '14 at 12:44

I have been using google qrcode api for sometime, but I didn't quite like this because it requires me to be on the Internet to access the generated image.

I did a little comand-line research and found out that linux has a command line tool qrencode for generating qr-codes.

I wrote this little script. And the good part is that the generated image is less than 1KB in size. Well the supplied data is simply a url.

$url = ($_SERVER['HTTPS'] ? "https://" : "http://").$_SERVER['HTTP_HOST'].'/profile.php?id='.$_GET['pid'];
$img = shell_exec("qrencode --output=- -m=1 $url");

$imgData = "data:image/png;base64,".base64_encode($img);

Then in the html I load the image:

<img class="emrQRCode" src="<?=$imgData ?>" />

You just need to have installed it. [most imaging apps on linux would have installed it under the hood without you realizing.

shareimprove this answer
 

I know the question is how to generate QR codes using PHP, but for others who are looking for a way to generate codes for websites doing this in pure javascript is a good way to do it. The jquery-qrcodejquery plugin does it well.

shareimprove this answer
 



普通分类: