Skip to content

Instantly share code, notes, and snippets.

@NTICompass
Created October 13, 2011 03:10
Show Gist options
  • Star 96 You must be signed in to star a gist
  • Fork 52 You must be signed in to fork a gist
  • Save NTICompass/1283249 to your computer and use it in GitHub Desktop.
Save NTICompass/1283249 to your computer and use it in GitHub Desktop.
QR Code + Logo Generator
<?php
/**
* QR Code + Logo Generator
*
* http://labs.nticompassinc.com
*/
$data = isset($_GET['data']) ? $_GET['data'] : 'http://labs.nticompassinc.com';
$size = isset($_GET['size']) ? $_GET['size'] : '200x200';
$logo = isset($_GET['logo']) ? $_GET['logo'] : FALSE;
header('Content-type: image/png');
// Get QR Code image from Google Chart API
// http://code.google.com/apis/chart/infographics/docs/qr_codes.html
$QR = imagecreatefrompng('https://chart.googleapis.com/chart?cht=qr&chld=H|1&chs='.$size.'&chl='.urlencode($data));
if($logo !== FALSE){
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
// Scale logo to fit in the QR Code
$logo_qr_width = $QR_width/3;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
imagecopyresampled($QR, $logo, $QR_width/3, $QR_height/3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
}
imagepng($QR);
imagedestroy($QR);
?>
@abuzant
Copy link

abuzant commented Apr 13, 2016

A Tutorial for thos who need to understand the contrast issues/login behind QRCodes can be found here:
How to put any logo on your QRCode?

@AnibalDuarte
Copy link

God bless you!

@fdorantesm
Copy link

Thanks, works for me. If you want to change qr's color you must to change rgb of google image after createimagefrompng.

@FizieJame
Copy link

Hi. How can add more data inside QR Code ?

@excel555
Copy link

fxxx,I can't use google in china.

@sugoireed
Copy link

Thank you very much. Worked like a charm.

@nirmalabis
Copy link

When using it with logo QR Code does not scan.
Please suggest..

@brainlet-ali
Copy link

How to download the generated QR code to disk.

@tanmaybug
Copy link

with image this doesn't work..please help...

@littlechris84
Copy link

Cant download the image

@Tumi-D
Copy link

Tumi-D commented Jan 27, 2021

can we change the color of QR code?

Yes, we can. After imagecreatefrompng()

imagetruecolortopalette($QR,true, 255);
$index = imagecolorclosest($QR, 0, 0, 0); // GET BLACK COLOR REVERSE 255 to  GET WHITECOLOR 
imagecolorset($QR, $index, 0, 150, 255); // SET COLOR TO BLUE

reference

@yakubu234
Copy link

yes we can download the QR code to disk
declare a variable before line 31 as $targetPath follow the guide bellow .
$targetPath = "qrdone/";
if (! is_dir($targetPath)) {
mkdir($targetPath, 0777, true);
}
$targetPath = $targetPath."YAk".time().".png";
then rewrite the function as imagepng($QR,$targetPath);

remeber your $targetPath is the full path with the folder name.

NOTE: for you to download you have to comment the header('Content-type: image/png'); line

@yakubu234
Copy link

did you have issues running the code on local server telling you the content type consist of invalid characters? here is a guide from this link https://stackoverflow.com/questions/22561073/headercontent-type-image-png-not-working-anymore that helps.
Goes thus
Your PHP script is emitting a UTF-8 byte order mark (EF BB BF) before outputting the PNG image content. This marker is probably causing PHP to default the content type to text/html. Your subsequent call to set the header is ignored because PHP has already sent the BOM.

The BOM has probably been placed in your PHP script just before the opening tag by your text editor, because it's saving the file in UTF-8 format. Change the format in your editor to ANSI/ASCII so that the BOM is not written.

Alternatively, you could try calling PHP's ob_clean() function to clear the output buffer before changing the header.

so kindly add ob_clean(); immediately before the header('Content-type: image/png');

hence as this
ob_clean();
header('Content-type: image/png');

@EliteEwok
Copy link

Thank you good Sir!

@mardiyansah
Copy link

thank you..very nice

@MysticWeb
Copy link

Awesome job. Is there a way I can add a Title (at the bottom)?

@NTICompass
Copy link
Author

NTICompass commented Mar 23, 2022

@MysticWeb You'll need to give it a font file, but you can try to use imagettftext to add text to the image. You might want to resize the output image so the text doesn't cover the QR code.

@MysticWeb
Copy link

@NTICompass
image
I have tried the following code (image attached) but still no writing is showing up

@NTICompass
Copy link
Author

NTICompass commented Mar 23, 2022

@MysticWeb imagettftext doesn't require imageloadfont. You just want

imagettftext($im, 20, 0, 10, 20, $black, 'fonts/Ubuntu-Regular.ttf', $text);

There is also imagestring which has a different set of parameters this does require imageloadfont):

imagestring($im, $font, 10, 20, $text, $black);

@MysticWeb
Copy link

MysticWeb commented Mar 23, 2022

@NTICompass Still none of these are working. I even tried moving the font file to the local directory. Still no luck. Here is my page code. I have commented out the QR generation itself incase the other image was somehow going behind it. The QR all works perfectly

image
image
image
image
image

@NTICompass
Copy link
Author

@MysticWeb I can try to have a look at this later. I haven't really thought about this code in a while, but I'm sure I can help when I am not busy at work :)

@MysticWeb
Copy link

@NTICompass Thank you very much. Sorry to bother you (at work) and I do appreciate your time and the fact this was written a long time ago

@melhergui
Copy link

Everything is working like a charm, thank you!

@LaurCzT
Copy link

LaurCzT commented Feb 21, 2023

Thanks for your code. :D i manage to build a simple PHP CMS using a bootstrap admin template to manage my QR codes based on your code and idea from @MysticWeb using MySQL 👍
but i skipped the idea of creating the qr .png file on host since you still need internet and google to use your code

@AzizanNur
Copy link

how to image qrcode this, to use in sending email? can you help me?

@AnibalDuarte
Copy link

how to image qrcode this, to use in sending email? can you help me?

@AzizanNur you can get the imagepng($QR) result, base64 it then use as raw content on img src.

@phpgenefied
Copy link

thanks for sharing this. worked without any issue

@nichtsooft
Copy link

Thanks for that lovely snippet! 🙏🏻

I wonder if there's a way to output a SVG-file?
Has anyone an idea? 🤔

@NTICompass
Copy link
Author

@nichtsooft The GD extension doesn't have built-in SVG export support, but there are probably external libraries that support this.

Though, since this code uses chart.googleapis.com to get QR images, converting to an SVG won't really give you what you expect.

I'd suggest finding an API/library that can generate QR code SVGs directly.

@nichtsooft
Copy link

nichtsooft commented Jun 3, 2023

@nichtsooft The GD extension doesn't have built-in SVG export support, but there are probably external libraries that support this.

Though, since this code uses chart.googleapis.com to get QR images, converting to an SVG won't really give you what you expect.

I'd suggest finding an API/library that can generate QR code SVGs directly.

Thank you for the clarification!!! 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment