From a706b97a1c807b9e8f9adabbc0cad381e8d20783 Mon Sep 17 00:00:00 2001 From: Miraty Date: Tue, 7 Jun 2022 00:47:10 +0200 Subject: [PATCH] Generate QR code before outputting anything --- index.php | 114 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 55 deletions(-) diff --git a/index.php b/index.php index 9d35407..709ef01 100755 --- a/index.php +++ b/index.php @@ -38,7 +38,7 @@ $params = array( "fgColor" => DEFAULT_FGCOLOR, ); -$validFormSubmitted = false; +$qrCodeAvailable = NULL; if ( isset($_POST['txt']) @@ -49,6 +49,8 @@ if ( AND isset($_POST['fgColor']) ) { + $qrCodeAvailable = true; + if (strlen($_POST['txt']) >= 1 AND strlen($_POST['txt']) <= 4096) { $params['txt'] = $_POST['txt']; } else { @@ -96,6 +98,48 @@ if ( } $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,64 +249,20 @@ foreach($themeDimensionsIcons as $dimFav) // Set all icons dimensions 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 "

" . $loc['error_generation'] . "

"; - error_log("LibreQR encountered an error while generating a QR code: " . $ex); - exit(); - } - +if ($qrCodeAvailable) { $dataUri = $result->getDataUri(); $qrSize = $params['size'] + 2 * $params['margin']; ?> -
-
- -
+
+
+ +
- -
+ +
- +" . $loc['error_generation'] . "

"; +} +?>