Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
?>
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jan 24, 2014

Hi,
How to use this sample?

@pazso

This comment has been minimized.

Copy link

@pazso pazso commented Mar 26, 2014

Rewrite this rows:
$data = [url]
$size = [x,y size]
$logo = [image url]

@terencechang

This comment has been minimized.

Copy link

@terencechang terencechang commented Apr 4, 2015

Very handy. Was using some PHP script that wasn't as easy as this to implement.

@anil1712

This comment has been minimized.

Copy link

@anil1712 anil1712 commented Apr 27, 2015

Yes, Its really useful and easy to integrate. Thanks.

@magebuzz

This comment has been minimized.

Copy link

@magebuzz magebuzz commented Jul 8, 2015

can we change the color of QR code?

@sdhp

This comment has been minimized.

Copy link

@sdhp sdhp commented Sep 7, 2015

Amazing snippet, has been very helpful to me, thank you!

@pdynarowski

This comment has been minimized.

Copy link

@pdynarowski pdynarowski commented Oct 22, 2015

with image this doesnt work

@mikenk2010

This comment has been minimized.

Copy link

@mikenk2010 mikenk2010 commented Nov 19, 2015

Great code, but one small thing is the logo is not stay in center.

@sandropeixoto

This comment has been minimized.

Copy link

@sandropeixoto sandropeixoto commented Nov 25, 2015

great code, fantastic! thanks!!!

@abuzant

This comment has been minimized.

Copy link

@abuzant 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

This comment has been minimized.

Copy link

@AnibalDuarte AnibalDuarte commented Apr 25, 2016

God bless you!

@fdorantesm

This comment has been minimized.

Copy link

@fdorantesm fdorantesm commented Aug 6, 2016

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

@FizieJame

This comment has been minimized.

Copy link

@FizieJame FizieJame commented Dec 5, 2016

Hi. How can add more data inside QR Code ?

@excel555

This comment has been minimized.

Copy link

@excel555 excel555 commented Jul 12, 2017

fxxx,I can't use google in china.

@sugoireed

This comment has been minimized.

Copy link

@sugoireed sugoireed commented Oct 12, 2017

Thank you very much. Worked like a charm.

@nirmalabis

This comment has been minimized.

Copy link

@nirmalabis nirmalabis commented Nov 6, 2017

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

@brainlet-ali

This comment has been minimized.

Copy link

@brainlet-ali brainlet-ali commented Jan 31, 2018

How to download the generated QR code to disk.

@tanmaybug

This comment has been minimized.

Copy link

@tanmaybug tanmaybug commented Aug 13, 2018

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

@littlechris84

This comment has been minimized.

Copy link

@littlechris84 littlechris84 commented Sep 25, 2018

Cant download the image

@Tumi-D

This comment has been minimized.

Copy link

@Tumi-D 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

This comment has been minimized.

Copy link

@yakubu234 yakubu234 commented Apr 1, 2021

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

This comment has been minimized.

Copy link

@yakubu234 yakubu234 commented Apr 1, 2021

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');

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