Generate QR Code In PHP (Simple Examples)

Welcome to a quick tutorial on how to generate QR code in PHP. Want to quickly promote your products and services? Share a link, email address, or phone number?

A simple way to generate QR codes in PHP is to:

  1. Use the QR Code library, an easy way is to use Composer to download it – composer require endroid/qr-code.
  2. Next, use the library to generate the QR code.
    • require "vendor/autoload.php";
    • use Endroid\QrCode\QrCode;
    • use Endroid\QrCode\Writer\PngWriter;
    • $qr = QrCode::create("https://site.com");
    • $writer = new PngWriter();
    • $writer->write($qr)->saveToFile("QR.PNG");

That should cover the basics, but if you want more examples – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP QR CODE GENERATION

All right, let us now go through some examples of QR Code generation.

 

EXAMPLE 1) SIMPLE PHP QR CODE

1-simple.php
<?php
// (A) LOAD QR CODE LIBRARY
require "vendor/autoload.php";
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;

// (B) CREATE QR CODE
$qr = QrCode::create("https://code-boxx.com");
$writer = new PngWriter();
$result = $writer->write($qr);

// (C) OUTPUT QR CODE
// (C1) SAVE TO FILE
$result->saveToFile(__DIR__ . "/qr.png");

// (C2) DIRECT OUTPUT
header("Content-Type: " . $result->getMimeType());
echo $result->getString();

// (C3) GENERATE DATA URI
// echo "<img src='{$result->getDataUri()}'>";

Using the library to create a QR code is straightforward as A-B-C:

  1. Load and use the library.
  2. Generate the QR code.
  3. Output the QR code, the library offers 3 different “modes” of output:
    • saveToFile() saves the QR code into an image file on the server.
    • getString() directly outputs the QR code in the raw data string. Good for you guys who want to generate unique discounts or tracking codes for users, just point an image tag to the QR Code PHP <img src="1-simple.php"/>.
    • Lastly, getDataUri() outputs the data URI string, for you guys who want to “directly embed” the image on the page itself <img src='{$result->getDataUri()}'/>.

 

 

EXAMPLE 2) QR CODE OPTIONS, LOGO, LABEL

2-options.php
<?php
// (A) LOAD QR CODE LIBRARY
require "vendor/autoload.php";
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\Label\Label;

// (B) CREATE QR CODE
$qr = QrCode::create("https://www.google.com/search?q=garbage+can")
  // (B1) CORRECTION LEVEL
  ->setErrorCorrectionLevel(new ErrorCorrectionLevelHigh())
  // (B2) SIZE & MARGIN
  ->setSize(300)
  ->setMargin(10)
  // (B3) COLORS
  ->setForegroundColor(new Color(255, 0, 0))
  ->setBackgroundColor(new Color(255, 255, 255));

// (B4) ATTACH LOGO
$logo = Logo::create(__DIR__ . "/logo.png")
  ->setResizeToWidth(100);

// (B5) ATTACH LABEL
$label = Label::create("LE GARBAGE CAN")
  ->setTextColor(new Color(0, 0, 0));

// (C) OUTPUT QR CODE
$writer = new PngWriter();
$result = $writer->write($qr, $logo, $label);
header("Content-Type: " . $result->getMimeType());
echo $result->getString();

Not going to explain line-by-line, this should be pretty self-explanatory – We can also set various options, add a logo, and even set a label on the QR code.

 

 

EXAMPLE 3) QR CODE DATA FORMATS

3-data-format.php
<?php
// (A) LOAD QR CODE LIBRARY
require "vendor/autoload.php";
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;

// (B) CREATE QR CODE
// (B1) WEBSITE
// $qr = QrCode::create("https://code-boxx.com");

// (B2) EMAIL
// $qr = QrCode::create("mailto:jon@doe.com");

// (B3) TELEPHONE NUMBER
// $qr = QrCode::create("tel:123456789");

// (B4) SMS
// $qr = QrCode::create("smsto:123456789:Message");

// (B5) VCARD
// https://www.qr-code-generator.com/solutions/vcard-qr-code/
$qr = QrCode::create("BEGIN:VCARD
VERSION:3.0
N:Doe;John;
TEL;TYPE=work,VOICE:123456789
EMAIL:john@doe.com
ORG:Code Boxx
TITLE:Crash Test Dummy
URL:https://code-boxx.com
VERSION:3.0
END:VCARD");

// (C) OUTPUT QR CODE
$writer = new PngWriter();
$result = $writer->write($qr);
header("Content-Type: " . $result->getMimeType());
echo $result->getString();

Lastly, just in case you have missed this one. QR Code can be used for many things other than sharing URL links – Email addresses, telephone numbers, SMS, VCards, Tweet, open an app (deep link), and so much more.

 

 

EXTRAS

That’s all for this guide, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this short guide. I hope it has helped your project, and if you have anything that you would like to add to this guide, please feel free to comment below. Good luck and happy coding!

10 thoughts on “Generate QR Code In PHP (Simple Examples)”

  1. Ignore last comment, added php to existing html so had to comment out header and use echo “getDataUri()}’>”; to get it to display below text input and button to ‘generate’ in a form. Works great now.

    Is there a way to wrap the label text? I have a long 60 char string that only shows the middle chars, beginning and end are not in image.

    1. I ended up reducing the font to a 7 to get the text string to fit. No option in the label code for wrapping.

  2. Worked once using my own logo and example 2, now all I get is a black screen with tiny square in center.
    Tried all sample code, refreshed cache. Reinstalled with composer remove and composer require.

    1. This was an issue reposting the header from the php code after sending header with html farther up in the code file. Changed header() to echo and it displays properly.

  3. Cheers for putting this up. Appreciate the simple to more complex examples.

    ‘vendor/autoload.php’ doesn’t seem to pull down with ‘composer require endroid/qr-code’
    Any thoughts where I’m going wrong? Any one else getting this to work? Getting issues?

    For me there’s 7 folders created with their contents, so the rest seems to be turning up!

    1. Ignore this. Some bizarre error at my end. Worked on the 4th time because reasons. Thanks again for the excellent tutorial. The advanced script you wrote is working perfectly for me. Respect for this.

Leave a Comment

Your email address will not be published. Required fields are marked *