PHP Web interface for generating QR codes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
10 KiB

<?php // This file is part of LibreQR, which is distributed under the GNU AGPLv3+ license
use CodeItNow\BarcodeBundle\Utils\QrCode;
require "inc.php";
$params = array(
"txt" => "",
"redondancy" => DEFAULT_REDONDANCY,
"margin" => DEFAULT_MARGIN,
"size" => DEFAULT_SIZE,
$validFormSubmitted = false;
if (
AND isset($_POST['redondancy'])
AND isset($_POST['margin'])
AND isset($_POST['size'])
AND isset($_POST['bgColor'])
AND isset($_POST['mainColor'])
) {
if (strlen($_POST['txt']) >= 1 AND strlen($_POST['txt']) <= 4096)
$params['txt'] = $_POST['txt'];
exit("Wrong value for txt");
if ($_POST['redondancy'] === "low" OR $_POST['redondancy'] === "medium" OR $_POST['redondancy'] === "quartile" OR $_POST['redondancy'] === "high")
$params['redondancy'] = $_POST['redondancy'];
exit("Wrong value for redondancy");
if (is_numeric($_POST['margin']) AND $_POST['margin'] >= 0 AND $_POST['margin'] <= 1024)
$params['margin'] = $_POST['margin'];
else if (empty($_POST['margin']))
$params['margin'] = NULL;
exit("Wrong value for margin");
if (is_numeric($_POST['size']) AND $_POST['size'] >= 1 AND $_POST['size'] <= 4096)
$params['size'] = $_POST['size'];
else if (empty($_POST['size']))
$params['size'] = NULL;
exit("Wrong value for size");
if (preg_match("/^#[abcdefABCDEF0-9]{6}$/", $_POST['bgColor']))
$params['bgColor'] = substr($_POST['bgColor'], -6);
exit("Wrong value for bgColor");
if (preg_match("/^#[abcdefABCDEF0-9]{6}$/", $_POST['mainColor']))
$params['mainColor'] = substr($_POST['mainColor'], -6);
exit("Wrong value for mainColor");
$validFormSubmitted = true;
<!DOCTYPE html>
<html lang="<?= $locale ?>">
<meta charset="utf-8">
<title>LibreQR · <?= $loc['subtitle'] ?></title>
<meta name="description" content="<?= $loc['description'] ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="dark light">
<meta name="application-name" content="LibreQR">
<meta name="referrer" content="no-referrer">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src 'self' data:; style-src 'self'; manifest-src 'self'; form-action 'self';">
<link rel="manifest" href="manifest.php">
<link rel="search" type="application/opensearchdescription+xml" title="LibreQR" href="opensearch.php&#63;redondancy=<?= $params['redondancy'] ?>&amp;margin=<?= $params['margin'] ?>&amp;size=<?= $params['size'] ?>&amp;bgColor=<?= urlencode($params['bgColor']) ?>&amp;mainColor=<?= urlencode($params['mainColor']) ?>">
require_once "less.php/lib/Less/Autoloader.php";
$colorScheme['theme'] = $theme;
$options = array('cache_dir' => 'css/', 'compress' => true);
$cssFileName = Less_Cache::Get(array("style.less" => ""), $options, $colorScheme);
<link rel="stylesheet" media="screen" href="css/<?= $cssFileName ?>">
foreach($themeDimensionsIcons as $dimFav) { // Set all icons dimensions
echo ' <link rel="icon" type="image/png" href="themes/' . $theme . '/icons/' . $dimFav . '.png" sizes="' . $dimFav . 'x' . $dimFav . '">' . "\n";
<a id="linkTitles" href="./">
<div id="titles">
<h2><?= $loc['subtitle'] ?></h2>
<form method="post" action="./#output">
<div id="firstWrapper">
<div class="param" id="txtParam">
<summary><label for="txt"><?= $loc['label_content'] ?></label></summary>
<div class="helpText">
<?= $loc['help_content'] ?>
<textarea rows="8" required="" id="txt" placeholder="<?= $loc['placeholder'] ?>" name="txt"><?= htmlspecialchars($params['txt']) ?></textarea>
<div id="sideParams">
<div class="param">
<summary><label for="redondancy"><?= $loc['label_redondancy'] ?></label></summary>
<p class="helpText">
<?= $loc['help_redondancy'] ?>
<select id="redondancy" name="redondancy">
<option <?php if ($params['redondancy'] === "low") echo 'selected="" '; ?>value="low">L - 7%</option>
<option <?php if ($params['redondancy'] === "medium") echo 'selected="" '; ?>value="medium">M - 15%</option>
<option <?php if ($params['redondancy'] === "quartile") echo 'selected="" '; ?>value="quartile">Q - 25%</option>
<option <?php if ($params['redondancy'] === "high") echo 'selected="" '; ?>value="high">H - 30%</option>
<div class="param">
<summary><label for="margin"><?= $loc['label_margin'] ?></label></summary>
<p class="helpText">
<?= $loc['help_margin'] ?>
<input type="number" list="margins" id="margin" placeholder="<?= $loc['placeholder_pixels'] ?>" name="margin" min="0" max="1024" value="<?= htmlspecialchars($params['margin']) ?>">
<datalist id="margins">
<option value="16">
<option value="32">
<option value="64">
<option value="128">
<div class="param">
<summary><label for="size"><?= $loc['label_size'] ?></label></summary>
<p class="helpText">
<?= $loc['help_size'] ?>
<input type="number" list="sizes" id="size" placeholder="<?= $loc['placeholder_pixels'] ?>" name="size" min="1" max="4096" value="<?= htmlspecialchars($params['size']) ?>">
<datalist id="sizes">
<option value="128">
<option value="256">
<option value="512">
<option value="1024">
<div id="colors">
<div class="param">
<label for="bgColor"><?= $loc['label_bgColor'] ?></label>
<div class="inputColorContainer">
<input type="color" name="bgColor" id="bgColor" value="#<?= htmlspecialchars($params['bgColor']) ?>">
<div class="param">
<label for="mainColor"><?= $loc['label_mainColor'] ?></label>
<div class="inputColorContainer">
<input type="color" name="mainColor" id="mainColor" value="#<?= htmlspecialchars($params['mainColor']) ?>">
<div class="centered">
<input class="button" type="submit" value="<?= $loc['button_create'] ?>" />
if ($validFormSubmitted) {
$rgbBgColor = array(
'r' => hexdec(substr($params['bgColor'],0,2)),
'g' => hexdec(substr($params['bgColor'],2,2)),
'b' => hexdec(substr($params['bgColor'],4,2)),
require "barcode-generator/Utils/QrCode.php";
$qrCode = new QrCode();
if (!is_null($params['margin']))
'r' => hexdec(substr($params['mainColor'],0,2)),
'g' => hexdec(substr($params['mainColor'],2,2)),
'b' => hexdec(substr($params['mainColor'],4,2)),
$dataUri = $qrCode->getDataUri();
$qrSize = $qrCode->getSize() + 2 * $qrCode->getPadding();
<section id="output">
<div class="centered" id="downloadQR">
<a href="<?= $dataUri ?>" class="button" download="<?= htmlspecialchars($params['txt']); ?>.png"><?= $loc['button_download'] ?></a>
<div class="centered" id="showOnlyQR">
<a title="<?= $loc['title_showOnlyQR'] ?>" href="<?= $dataUri ?>"><img width="<?= $qrSize ?>" height="<?= $qrSize ?>" alt='<?= $loc['alt_QR_before'] ?><?= htmlspecialchars($params['txt']); ?><?= $loc['alt_QR_after'] ?>' id="qrCode"<?php
// Compute the difference between the QR code and theme background colors
$diffLight = abs($rgbBgColor['r']-hexdec(substr($colorScheme['bg-light'],-6,2))) + abs($rgbBgColor['g']-hexdec(substr($colorScheme['bg-light'],-4,2))) + abs($rgbBgColor['b']-hexdec(substr($colorScheme['bg-light'],-2,2)));
$diffDark = abs($rgbBgColor['r']-hexdec(substr($colorScheme['bg-dark'],-6,2))) + abs($rgbBgColor['g']-hexdec(substr($colorScheme['bg-dark'],-4,2))) + abs($rgbBgColor['b']-hexdec(substr($colorScheme['bg-dark'],-2,2)));
// Determine whether a CSS corner is needed to let the user see the margin of the QR code
$contrastThreshold = 64;
if ($diffLight < $contrastThreshold)
echo " class='needLightContrast'";
if ($diffDark < $contrastThreshold)
echo " class='needDarkContrast'";
?> src="<?= $dataUri ?>"></a>
<?php } ?>
<section id="info" class="metaText">
<?= $loc['metaText_qr'] ?>
<?php if ($customTextEnabled) { ?>
<section class="metaText">
<?= $customText ?>
<?php } ?>
<section class="metaText">
<?= $loc['metaText_legal'] ?>