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 Arsenault May 09 '11 at 22:58
-
7Akor: could you please un-accept my answer? Google has [deprecated the API](https://developers.google.com/chart/infographics/docs/qr_codes?csw=1) upon which my answer relies, therefore it serves little purpose from now on. – David Thomas Mar 10 '15 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:


- 3,782
- 4
- 16
- 33
-
Original link to the API has broken as the [page has now moved here](http://code.google.com/apis/chart/infographics/docs/qr_codes.html) – Sam Martin Sep 05 '11 at 07:27
-
5Are 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
-
@Lobo: not of which I'm currently aware, no. Though this may change over time, of course. – Sep 27 '12 at 15:13
-
17It 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 01:26
-
5@Lobo According to their [documentation](https://developers.google.com/chart/image/docs/making_charts): "*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
-
3I find answer by @abaumg better as it does not depends 3rd parties like google... – arod Jan 25 '14 at 21:21
-
2have they come up with any alternatives ? I read Google is sunsetting charts API (QR Code) around April 2015. – CodeMonkey Mar 03 '14 at 22:53
-
1
-
@mmcrae: so I see, thank you. I've notified the OP (in a comment to the question) asking to be unaccepted, to allow this question to retain use for the future. – Mar 10 '15 at 22:44
-
you could also just delete your answer, or edit it with a disclaimer / alternative answer, no? – Don Cheadle Mar 11 '15 at 15:32
-
@mmcrae: I'm not sure I could, the likely way I'd go, from my current answer, would be to recommend a library, which [abaumg](http://stackoverflow.com/a/5943375/82548) has already done; there may be a *better* library, now, after all this time, but I've not yet come across one. Though my research has been extremely casual. And recommending an external resource is now considered off-topic for Stack Overflow, so I'd rather not run that risk in addition to the risk of posting a likely-duplicate answer. – Mar 11 '15 at 15:43
-
Do you think this answer is contributing something to the discussion? To me, it seems not, as it is suggesting to use something that should not be used. Unless I'm mistaken, and you can actually use the Google QR Code API without issues. If I knew my answer was no longer helpful or was misguided, I would remove it to save others the time of discovering the issues themselves. – Don Cheadle Mar 11 '15 at 15:53
-
@mmcrae: no, I can't; not while it's accepted. That's why I've requested to be unaccepted, so that I *can* delete it. I've also flagged for moderator attention, so that they might be able to take action on my behalf (overriding the "you can't delete this accepted answer" rule). – Mar 11 '15 at 15:54
-
@anon gotchya :) didn't know about that rule. I wonder if that's a limitation on the way they designed the application / database for SO... – Don Cheadle Mar 11 '15 at 16:03
-
2@anon I've flagged this as "for moderator intervention" to see if a mod can perhaps un-accept the answer (it was a valid and relevant answer at the time of posting but is no longer so). This SO question comes up as the second result for "php qr code library" (how I got here) so I think it's important that the question remain up-to-date even if the OP is no longer paying attention. – Doktor J Jul 24 '15 at 04:43
-
-
-
1This API has now been turned off by Google and can no longer be used. – Xxmarijnw Mar 19 '19 at 11:27
-
10
-
-
1
-
1
-
Oct 2022, still working... but it timesout after 4 calls in quick succession – Adam Collingburn Oct 20 '22 at 07:33
The easiest way to generate QR codes with PHP is the phpqrcode library.

- 2,083
- 1
- 16
- 24
-
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. – Sunny R Gupta May 07 '13 at 08:58
-
2Fair 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
-
-
@CubicleSoft i think developer took your comment seriously and updated right away – mokNathal Oct 12 '15 at 12:05
-
@mokNathal - I'm not seeing anything different here. The most recent release available on the Downloads page is still from 2010. – CubicleSoft Oct 12 '15 at 15:48
-
-
I wanted to use it WITHOUT composer but it did not work in Ubuntu although it works in Windows. I was pleased to see Composer support is a fairly recent addition and that by downloading the last commit before Composer support was added I was able to forget about Composer and get the library working in seconds. https://github.com/khanamiryan/php-qrcode-detector-decoder/tree/cda63b7f4a8cd84d72c41b25c74284b56dc7f2cc It works very well now... – Tarik Dec 13 '20 at 21:21
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()));

- 7,543
- 4
- 37
- 51
-
**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? – Nov 28 '16 at 07: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. – clemlatz Nov 29 '16 at 13:37
-
-
-
1Sure :) I used implode() in php to combine all data, then explode() if needed. Thats it sir :) – Dec 02 '16 at 08:48
-
-
The code triggers fatal error, saying: "Call to a member function setSize() on null" – Codemole Apr 04 '19 at 04:26
-
In my case, I just needed to write a qr code image into the pdf. When I used header, it was directly displayed the qr code and not executed the next line of codes. There was no error or exception too. After removing the header it worked as expected. – TS Guhan Jan 08 '20 at 10:33
-
this is not working. can you please update the code with latest library – Jigs Parmar Mar 31 '23 at 12:12
-
@JigsParmar please refer to the library documentation for up-to-date examples https://github.com/endroid/QrCode – clemlatz Apr 01 '23 at 17:27
-
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)

- 9,839
- 6
- 34
- 62

- 16,483
- 8
- 84
- 94
-
this library doesnt seem to work for variety of inputs, small & simple text are okay though – duckduckgo Mar 17 '14 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. – duckduckgo Mar 18 '14 at 05: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 – RafaSashi Mar 18 '14 at 08:42
-
thanks! looks like error was due to unsupported string being passed, I was using
src to store string. after I wrapped src parameters with urlencode() it works! I suspected because somebody here reported the library was not maintained since a while. – duckduckgo Mar 18 '14 at 09:18
-
you are welcome. the basic features from this library are reliable but I doubt you can efficiently create fancy qrcode :) – RafaSashi Mar 18 '14 at 10:30
-
2Can 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
-
Good question. PHP QR Code is open source (LGPL) library. You can have a look at the terms if you want http://opensource.org/licenses/LGPL-3.0 – RafaSashi Mar 21 '14 at 18:53
qrcode-generator on Github. Simplest script and works like charm.
Pros:
- No third party dependency
- No limitations for the number of QR code generations

- 2,192
- 21
- 37
-
-
@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) – alex Jan 10 '17 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.

- 9,686
- 2
- 32
- 35
-
I am using the Jquery plugin https://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. – Avatar Jul 11 '17 at 14:40
-
It's [possible](https://stackoverflow.com/questions/3906142/how-to-save-a-png-from-javascript-variable) to download a JS-generated data image 100% in the client. – Quinn Comendant Jul 12 '17 at 03:22
-
Sorry for not being clear. I meant "save serverside" - not possible with only Javascript. – Avatar Jul 12 '17 at 05: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.

- 4,321
- 28
- 38

- 6,509
- 4
- 30
- 34
-
escapeshellarg explained here (for those wondering) - http://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. – Natalie Adams Oct 17 '15 at 02:54
-