Commit aa7ed7fd authored by Dainis Abols's avatar Dainis Abols
Browse files

QR Code fix

parent e1336b85
......@@ -2,47 +2,76 @@
namespace Lu\LuApi\Generators;
use \PHPQRCode\QRcode;
use TYPO3\CMS\Core\Core\Environment;
/**
* Class QRCodeGenerator
* QR Code + Logo Generator
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 1.4.7
* @since 17.10.2018
* @author Dainis Abols <dainis.abols@lu.lv>
* @since 17.10.2018
* @copyright http://labs.nticompassinc.com
*
* @package Lu\LuApi\Generators
* @package Lu\LuApi\Generators
*/
class QRCodeGenerator
{
/**
* API keyword for url
* Max qrcode image size
* @var string
*/
static private string $size = '400x400';
/**
* Logo filename relative to path /LuApi/Resources/Public/Icons/
*
* @var string
*/
static private $apiKeyword = 'generate/';
static private string $logo = 'qr_logo.png';
/**
* Creates a visual QR Image for provided url
*
* @param string $url
*/
static public function create(string $url = null)
static public function create()
{
if ($url == null) {
// Fetch full uri request
$requestUri = parse_url($_SERVER["REQUEST_URI"]);
// Get only URL
$keyword = self::$apiKeyword;
$url = substr($requestUri['query'], strpos($requestUri['query'], $keyword) + strlen($keyword), strlen($requestUri['query']));
$url = urldecode($url);
}
// Set headers and print out the image
header('Cache-Control: no-cache, must-revalidate, s-maxage=0');
QRcode::png($url, false, 'QR_ECLEVEL_M', 10);
// Sdet headers
header('Content-type: image/png');
// Read url from request
$r = $_REQUEST['r'];
$request = explode('/', $r);
unset($request[0]);
unset($request[1]);
$url = implode('/', $request);
// Get QR Code image from Google Chart API
$QR = imagecreatefrompng('https://chart.googleapis.com/chart?cht=qr&chld=H|1&chs='.self::$size.'&chl='.urlencode($url));
// Add logo
$logoPath = Environment::getPublicPath().'/typo3conf/ext/lu_api/Resources/Public/Icons/'.self::$logo;
$logo = imagecreatefromstring(file_get_contents($logoPath));
// Set image sizes
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
// Set logo width (this is capped)
$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;
// Combine images and create output
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);
// Stop script, to prevent excessive output
exit();
}
}
......@@ -85,9 +85,9 @@
<b>{data.date}</b>
<br>{data.namesday}
</div>
<!-- <div class="qrcode">-->
<!-- <img src="{data.qrurl}"/>-->
<!-- </div>-->
<div class="qrcode">
<img src="{data.qrurl}"/>
</div>
</div>
</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment