Generate QR Code In PHP

We'll use a PHP class to generate QR codes. You can find the class on Github. 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. [code lang="php"] <?php include "src/QrCode.php"; [/code] 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: [code lang="php"] <?php include "src/QrCode.php"; header("Content-type:image/png"); [/code] 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. [code lang="php"] <?php include "src/QrCode.php"; header("Content-type:image/png"); $qr = new \Endroid\QrCode\QrCode(); [/code] 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.


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. [code lang="php"] $qr->setText("Some Text"); // or we can give url to parameter. $qr->setText(""); [/code] That was our first method we had to know. Second method is setSize method.


Actually method name explain itself. We use set size method to set the size of QR code :D We set size as pixel. For example setSize(200) means set the size to 200 pixels. We use this method as in example. [code lang="php"] $qr->setSize(250); [/code] We just set the size to 250 pixels. Our last method is render() method.


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. [code lang="php"] <?php include "src/QrCode.php"; header("Content-type:image/png"); $qr = new \Endroid\QrCode\QrCode(); $qr->setText(""); $qr->setSize(250); echo $qr->render(); [/code] 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. [code lang="html"] <img src="qr-code.php"> [/code] 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.

Yorum Yok

Yorum Bırakın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir