SID Tag Generator
Tags for any page can be created using the tag generator URI at "https://wiki.techtangents.net/php/sid-tag.php". There are several parameters for configuring the tag type and attributes.
The code for the script can be found below:
<?php
# Static Imports
$font_ttf = "./NimbusSansNarrow-Bold.ttf";
$logo = "./logo-mono-title-bottom.png";
# Parameters
# page : Title of the page to make a tag for
# r : Rotate image 90 degrees
# layout (tag, square, shipping) : The format and layout of the image
# serial : A serial number for a specific item example
# Determine Layout from GET
if(isset($_GET['layout'])) {
$layout = $_GET['layout'];
}else{
$layout = "tag";
}
# Setup Layout
if ($layout == "tag")
{
$dim = array(
"width" => 600,
"height" => 1200,
"qr-size" => 450,
"qr-x" => 75,
"qr-y" => 675,
"logo-x" => 60,
"logo-y" => 600-140,
"logo-scale" => 1,
"line-a-x" => 0,
"line-a-y" => 599,
"line-b-x" => 599,
"line-b-y" => 599,
);
$header_size = 80;
$text_padding = 160;
}
if ($layout == "square")
{
$dim = array(
"width" => 600,
"height" => 600,
"qr-size" => 300,
"qr-x" => 150,
"qr-y" => 150,
"logo-x" => 0,
"logo-y" => 600-122,
"logo-scale" => 1,
"line-a-x" => 0,
"line-a-y" => 599,
"line-b-x" => 599,
"line-b-y" => 599,
);
$header_size = 60;
$text_padding = 80;
}
if ($layout == "shipping")
{
$dim = array(
"width" => 1200,
"height" => 1800,
"qr-size" => 1050,
"qr-x" => 75,
"qr-y" => 450,
"logo-x" => 50,
"logo-y" => 1800-160,
"logo-scale" => 1,
"line-a-x" => 0,
"line-a-y" => 0,
"line-b-x" => 0,
"line-b-y" => 0,
);
$header_size = 200;
$text_padding = 200;
}
function calculateTextBox($text,$fontFile,$fontSize,$fontAngle) {
$rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
return array(
"left" => abs($minX) - 1,
"top" => abs($minY) - 1,
"width" => $maxX - $minX,
"height" => $maxY - $minY,
"box" => $rect
);
}
# Fetch data from page
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTMLFile("https:#wiki.techtangents.net/wiki/".$_GET['page']);
$h1 = $doc->getElementsByTagName("title")->item(0)->textContent;
$h1 = str_replace(" - Tech Tangents","",$h1);
# Determine if linking to example serial
if(isset($_GET['serial'])) {
$qrcode = $doc->getElementByID("QRCode-sn".$_GET['serial']);
}else{
$qrcode = $doc->getElementByID("QRCode");
}
$data = $qrcode->getElementsByTagName("img")[0]->attributes[0]->value;
$data = str_replace("data:image/png;base64,", "", $data);
# Generate QR image from data
$data = base64_decode($data);
$qr = imagecreatefromstring($data);
$qr = imagescale($qr,$dim['qr-size'],$dim['qr-size'],IMG_NEAREST_NEIGHBOUR);
# Create base image
$bg = imagecreate($dim['width'], $dim['height']);
# Set the background
imagecolorallocate($bg, 255, 255, 255);
# Copy the QR Code
imagecopy($bg,$qr,$dim['qr-x'],$dim['qr-y'],0,0,$dim['qr-size'],$dim['qr-size']);
# Calculate Title size
$the_box = calculateTextBox($h1, $font_ttf, $header_size, 0);
$imgWidth = $the_box["width"] + $text_padding;
if ($imgWidth < $dim['width'])
{
$imgWidth = $dim['width'];
}
$imgHeight = $the_box["height"] + $text_padding/2;
# Draw line to fold
$label_s_draw = imagecolorallocate($bg, 0, 0, 0);
imageline($bg,$dim['line-a-x'],$dim['line-a-y'],$dim['line-b-x'],$dim['line-b-y'],$label_s_draw);
# Add logo
$im_logo = imagecreatefrompng($logo);
imagecopy($bg,$im_logo,$dim['logo-x'],$dim['logo-y'],0,0,600*$dim['logo-scale'],122*$dim['logo-scale']);
# Add Inverted text with background
$label = imagecreate($imgWidth,$imgHeight);
imagecolorallocate($label, 0, 0, 0);
$label_bg = imagecolorallocate($label, 0, 0, 0);
$label_text = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, $header_size, 0, $text_padding/2, $header_size+$text_padding/4, $label_text, "./NimbusSansNarrow-Bold.ttf", $h1);
$label = imagescale($label,$dim['width'],$imgHeight);
imagecopy($bg,$label,0,0,0,0,$imgWidth,$imgHeight);
# Final stage rotate option
if(isset($_GET['r'])) {
$bg = imagerotate($bg, 90, 0);
}
if ($bg !== false) {
header('Content-Type: image/png');
imagepng($bg);
imagedestroy($im);
imagedestroy($bg);
}
else {
echo 'An error occurred.';
}
?>