Generate QR code before outputting anything
This commit is contained in:
parent
d0f211f61e
commit
a706b97a1c
98
index.php
98
index.php
@ -38,7 +38,7 @@ $params = array(
|
|||||||
"fgColor" => DEFAULT_FGCOLOR,
|
"fgColor" => DEFAULT_FGCOLOR,
|
||||||
);
|
);
|
||||||
|
|
||||||
$validFormSubmitted = false;
|
$qrCodeAvailable = NULL;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
isset($_POST['txt'])
|
isset($_POST['txt'])
|
||||||
@ -49,6 +49,8 @@ if (
|
|||||||
AND isset($_POST['fgColor'])
|
AND isset($_POST['fgColor'])
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
$qrCodeAvailable = true;
|
||||||
|
|
||||||
if (strlen($_POST['txt']) >= 1 AND strlen($_POST['txt']) <= 4096) {
|
if (strlen($_POST['txt']) >= 1 AND strlen($_POST['txt']) <= 4096) {
|
||||||
$params['txt'] = $_POST['txt'];
|
$params['txt'] = $_POST['txt'];
|
||||||
} else {
|
} else {
|
||||||
@ -96,6 +98,48 @@ if (
|
|||||||
}
|
}
|
||||||
|
|
||||||
$validFormSubmitted = true;
|
$validFormSubmitted = true;
|
||||||
|
|
||||||
|
$rgbBgColor = array(
|
||||||
|
'r' => hexdec(substr($params['bgColor'],0,2)),
|
||||||
|
'g' => hexdec(substr($params['bgColor'],2,2)),
|
||||||
|
'b' => hexdec(substr($params['bgColor'],4,2)),
|
||||||
|
);
|
||||||
|
|
||||||
|
$qrCode = Builder::create()
|
||||||
|
->data($params['txt']);
|
||||||
|
if (!is_null($params['margin']))
|
||||||
|
$qrCode->margin($params['margin']);
|
||||||
|
if (!is_null($params['size']))
|
||||||
|
$qrCode->size($params['size']);
|
||||||
|
|
||||||
|
if ($params['redundancy'] === "high")
|
||||||
|
$qrCode->errorCorrectionLevel(new ErrorCorrectionLevelHigh());
|
||||||
|
else if ($params['redundancy'] === "quartile")
|
||||||
|
$qrCode->errorCorrectionLevel(new ErrorCorrectionLevelQuartile());
|
||||||
|
else if ($params['redundancy'] === "medium")
|
||||||
|
$qrCode->errorCorrectionLevel(new ErrorCorrectionLevelMedium());
|
||||||
|
else if ($params['redundancy'] === "low")
|
||||||
|
$qrCode->errorCorrectionLevel(new ErrorCorrectionLevelLow());
|
||||||
|
|
||||||
|
$qrCode
|
||||||
|
->backgroundColor(new Color(
|
||||||
|
$rgbBgColor['r'],
|
||||||
|
$rgbBgColor['g'],
|
||||||
|
$rgbBgColor['b']
|
||||||
|
))
|
||||||
|
->foregroundColor(new Color(
|
||||||
|
hexdec(substr($params['fgColor'],0,2)),
|
||||||
|
hexdec(substr($params['fgColor'],2,2)),
|
||||||
|
hexdec(substr($params['fgColor'],4,2))
|
||||||
|
));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $qrCode->build();
|
||||||
|
} catch (Exception $ex) {
|
||||||
|
http_response_code(500);
|
||||||
|
$qrCodeAvailable = false;
|
||||||
|
error_log("LibreQR encountered an error while generating a QR code: " . $ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
@ -205,51 +249,7 @@ foreach($themeDimensionsIcons as $dimFav) // Set all icons dimensions
|
|||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
if ($validFormSubmitted) {
|
if ($qrCodeAvailable) {
|
||||||
|
|
||||||
$rgbBgColor = array(
|
|
||||||
'r' => hexdec(substr($params['bgColor'],0,2)),
|
|
||||||
'g' => hexdec(substr($params['bgColor'],2,2)),
|
|
||||||
'b' => hexdec(substr($params['bgColor'],4,2)),
|
|
||||||
);
|
|
||||||
|
|
||||||
$qrCode = Builder::create()
|
|
||||||
->data($params['txt']);
|
|
||||||
if (!is_null($params['margin']))
|
|
||||||
$qrCode->margin($params['margin']);
|
|
||||||
if (!is_null($params['size']))
|
|
||||||
$qrCode->size($params['size']);
|
|
||||||
|
|
||||||
if ($params['redundancy'] === "high")
|
|
||||||
$qrCode->errorCorrectionLevel(new ErrorCorrectionLevelHigh());
|
|
||||||
else if ($params['redundancy'] === "quartile")
|
|
||||||
$qrCode->errorCorrectionLevel(new ErrorCorrectionLevelQuartile());
|
|
||||||
else if ($params['redundancy'] === "medium")
|
|
||||||
$qrCode->errorCorrectionLevel(new ErrorCorrectionLevelMedium());
|
|
||||||
else if ($params['redundancy'] === "low")
|
|
||||||
$qrCode->errorCorrectionLevel(new ErrorCorrectionLevelLow());
|
|
||||||
|
|
||||||
$qrCode
|
|
||||||
->backgroundColor(new Color(
|
|
||||||
$rgbBgColor['r'],
|
|
||||||
$rgbBgColor['g'],
|
|
||||||
$rgbBgColor['b']
|
|
||||||
))
|
|
||||||
->foregroundColor(new Color(
|
|
||||||
hexdec(substr($params['fgColor'],0,2)),
|
|
||||||
hexdec(substr($params['fgColor'],2,2)),
|
|
||||||
hexdec(substr($params['fgColor'],4,2))
|
|
||||||
));
|
|
||||||
|
|
||||||
try {
|
|
||||||
$result = $qrCode->build();
|
|
||||||
} catch (Exception $ex) {
|
|
||||||
http_response_code(500);
|
|
||||||
echo "<p><strong>" . $loc['error_generation'] . "</strong></p></body></html>";
|
|
||||||
error_log("LibreQR encountered an error while generating a QR code: " . $ex);
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
$dataUri = $result->getDataUri();
|
$dataUri = $result->getDataUri();
|
||||||
|
|
||||||
$qrSize = $params['size'] + 2 * $params['margin'];
|
$qrSize = $params['size'] + 2 * $params['margin'];
|
||||||
@ -278,7 +278,11 @@ if ($validFormSubmitted) {
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<?php } ?>
|
<?php
|
||||||
|
} else if ($qrCodeAvailable === false) {
|
||||||
|
echo " <p><strong>" . $loc['error_generation'] . "</strong></p></body></html>";
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
<footer>
|
<footer>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user