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?
-
10don't re-invent the wheel! below answers are perfect– Jordan ArsenoMay 9, 2011 at 22:58
-
5Akor: 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 ThomasMar 10, 2015 at 22:42
7 Answers
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:
-
5Are there any limitations for using the Google API?. I mean limitations as: amount of requests in a given time, etc.– LoboSep 27, 2012 at 15:11
-
16It should be noted that this has been deprecated. Google plans to continue support until 2015. So as always, caveat emptor. Jan 10, 2013 at 1:26
-
5@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.". Jan 11, 2014 at 17:32
-
3I find answer by @abaumg better as it does not depends 3rd parties like google...– arodJan 25, 2014 at 21:21
-
10
The easiest way to generate QR codes with PHP is the phpqrcode library.
-
10+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. May 7, 2013 at 8:58
-
1Fair 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. Jun 29, 2013 at 17:00
-
-
@CubicleSoft i think developer took your comment seriously and updated right away Oct 12, 2015 at 12:05
-
@mokNathal - I'm not seeing anything different here. The most recent release available on the Downloads page is still from 2010. Oct 12, 2015 at 15:48
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
from http://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)
-
this library doesnt seem to work for variety of inputs, small & simple text are okay though Mar 17, 2014 at 12:55
-
-
nothing much, when i do $codeContents = "www.example.com" its ok but if i do $codeContents="www.example.com/some/view/aUdv4rP4BXXLQdpXZC6Gs5C6AfyJM4uB6ntVttIr1B0=" it did not show correct image. Mar 18, 2014 at 5:30
-
I cannot reproduce the error. It is working fine with QRcode::png('www.example.com/some/view/aUdv4rP4BXXLQdpXZC6Gs5C6AfyJM4uB6ntVttIr1B0='); maybe it has something to do with the headers Mar 18, 2014 at 8:42
-
2Can i use php-qrcode library for commercial purpose like to develope a website which requires qr code in some of its part? Mar 21, 2014 at 12:44
The endroid/QrCode library is easy to use, well maintained, and can be installed using composer. There is also a bundle to use directly with Symfony.
Installing :
$ composer require endroid/qrcode
Usage :
<?php
use Endroid\QrCode\QrCode;
$qrCode = new QrCode();
$qrCode
->setText('Life is too short to be generating QR codes')
->setSize(300)
->setPadding(10)
->setErrorCorrection('high')
->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
->setLabel('Scan the code')
->setLabelFontSize(16)
->setImageType(QrCode::IMAGE_TYPE_PNG)
;
// now we can directly output the qrcode
header('Content-Type: '.$qrCode->getContentType());
$qrCode->render();
// or create a response object
$response = new Response($qrCode->get(), 200, array('Content-Type' => $qrCode->getContentType()));
-
Iwazaru sir, how can I use
setText()
if I have MULTIPLE data? For example, I have$a = 1
,$b = 2
,$c = 3
?. I need to use more than ONE sentence or String. How can I do that?– user6690322Nov 28, 2016 at 7:42 -
As the
setText
method only accepts one parameters I guess you should concatenate your variables into one before passing it. But I'm not sure I'm understanding your problem correctly, maybe you should open an issue in the library's repo.– IwazaruNov 29, 2016 at 13:37 -
Iwazaru I've got the answer sir..– user6690322Dec 2, 2016 at 3:42
-
-
1Sure :) I used implode() in php to combine all data, then explode() if needed. Thats it sir :)– user6690322Dec 2, 2016 at 8:48
qrcode-generator on Github. Simplest script and works like charm.
Pros:
- No third party dependency
- No limitations for the number of QR code generations
-
-
@praba230890 could you provide some more examples with this script? i am trying to get some contact information in a QR code 300x300 px I have installed/downloaded the script on my localhost and the sample_html.php works. But the other two not. my japanese isn't that good (ok i will use google translate)– alexJan 10, 2017 at 18:54
-
-
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-qrcode jquery plugin does it well.
-
I am using the Jquery plugin github.com/lrsjng/jquery-qrcode for a while now, also pretty. However, output is only binary. So if you need to generate and save an image as PNG, it will not help too much. That's why most developers reading this question need PHP.– AvatarJul 11, 2017 at 14:40
-
It's possible to download a JS-generated data image 100% in the client. Jul 12, 2017 at 3:22
-
Sorry for not being clear. I meant "save serverside" - not possible with only Javascript.– AvatarJul 12, 2017 at 5:38
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 '.escapeshellarg($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.
-
escapeshellarg explained here (for those wondering) - stackoverflow.com/a/22739494/195722 . Personally, I wouldn't shell out to a command each time (especially for a public facing generation) and I would definitely use regex to make sure what they passed in was a string that I was expecting. Oct 17, 2015 at 2:54
-