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);
?>
@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