Generate QR Code In PHP

We’ll use a PHP class to generate QR codes. You can find the class on Github. https://www.github.com/endroid/qrcode

Download the class by clicking “Clone or Download” button then click the “Download Zip” button. If you downloaded the zip file, extract it and copy src directory to your working directory. I’ll use htdocs/qrcode directory. If you copied the file to your working directory, create an index.php file in this directory and include src/QrCode.php file through include command.

<?php
include "src/QrCode.php";

Then we have to change Content-type header to image/png because the class returns a png formatted data. The browser sets content type to text/html by default. If you don’t change the content-type header, browser will try print image data like a text data and the output will look like that.

screen-shot-2016-10-30-at-13-14-12

You can change Content-type header by using header() function in PHP. Change your index.php:

<?php
include "src/QrCode.php";

header("Content-type:image/png");

We just set the content-type header to image/png. And the next step is creating an instance of the class. QrCode class has own namespace so we can’t use “new QrCode()” directly. If we want to create an instance we need to write the namespace before the class name. Let’s change index.php again.

<?php
include "src/QrCode.php";

header("Content-type:image/png");
$qr = new \Endroid\QrCode\QrCode();

We just created an instance of the class and now we’re ready to create our QR code. We need to know some methods in the class. First one is setText method.

setText($text)

This method takes one parameter: “A text to convert to QR code.”. The parameter could be plain text or any url etc.
We use the setText method this way.

$qr->setText("Some Text");
// or we can give url to parameter.
$qr->setText("http://zahidefe.net");

That was our first method we had to know. Second method is setSize method.

setSize($size)

Actually method name explain itself. We use set size method to set the size of QR code 😀 We set size as pixel. For example setSize(200) means set the size to 200 pixels. We use this method as in example.

$qr->setSize(250);

We just set the size to 250 pixels.

Our last method is render() method.

render()

Before using render method, you have to set text if you didn’t set it yet. if we already set the text and set the size, now we can create and get the QR code by using render method. render method returns QR code. If we write “echo $qr->render();” we could see the QR code. Let’s change index.php file last time.

<?php
include "src/QrCode.php";

header("Content-type:image/png");
$qr = new \Endroid\QrCode\QrCode();
$qr->setText("http://www.zahidefe.net");
$qr->setSize(250);

echo $qr->render();

If you run this page you’ll see QR code like this.

screen-shot-2016-10-30-at-14-26-41

If you want to use this QR code in HTML you have to make some changes.

Use Generated QR Code In HTML

Copy all of the code in index.php to qr-code.php file.

And just add this line to index.php file.

<img src="qr-code.php">

If you run the index.php page you’ll see your QR code as image in HTML.

That’s all for this lesson. If you have any question about the lesson you can ask me through comments. If you leave me your email address and question I can answer your question. And if you liked the lesson please click the “Thank You” button. (It’s not an ad, just thanked meter 🙂 ). Have a great day.

Lessons In English, PHP Mustafa Zahid Efe Yorum Yapılmamış