How To 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!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Create QR Code Useful Bits & Links
The End

 

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

  • A copy of the PHP QR Code Library is not included in the zip file. As in the introduction, get the latest version from Github or use Composer to pull – composer require endroid/qr-code.
  • Make sure that extension=gd (or gd2 prior to PHP8) is enabled in php.ini.

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

PHP QR CODE GENERATION

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

 

1) SIMPLE PHP QR CODE GENERATION

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. Doh.
  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()}'/>.

 

 

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://code-boxx.com")
  // (B1) CORRECTION LEVEL
  ->setErrorCorrectionLevel(new ErrorCorrectionLevelHigh())
  // (B2) SIZE & MARGIN
  ->setSize(300)
  ->setMargin(10)
  // (B3) COLORS
  ->setForegroundColor(new Color(0, 0, 255))
  ->setBackgroundColor(new Color(255, 255, 255));

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

// (B5) ATTACH LABEL
$label = Label::create("CODE BOXX")
  ->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.

 

 

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.

 

 

USEFUL BITS & LINKS

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

 

INFOGRAPHIC CHEAT SHEET

How To Generate QR Code In PHP (Click To Enlarge)

 

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!

2 thoughts on “How To Generate QR Code In PHP – Simple Examples”

  1. 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 *