ben blogs

wherever you go, there you are

Handy QR Codes

Put together a silly little php script to build an SVG QR code dynamically. This started a while ago as a quick way to get a printable QR code for each pinball machine that would shoot you to an issue report form with the name of the game prefilled.

I previously generated a bunch of PNGs with qrencode(1) and used php to list them on the web but that requires ssh access to generate new ones.

For some reason the old one is a fish shell script (I use it as my interactive shell but don’t usually write scripts with it).

for pin in $(cat coinslot.txt)
    echo https://tcpinball.org/report-issue/tcs/\?game=$(string escape --style=url $pin) \
    | qrencode -t png -o qr/tcs/$(string escape --style=url $pin).png
end

for pin in $(cat rightbrain.txt)
    echo https://tcpinball.org/report-issue/rbb/\?game=$(string escape --style=url $pin) \
    | qrencode -t png -o qr/rbb/$(string escape --style=url $pin).png
end

Source here: qr.fish.

Here’s the new script. It uses chillerlan/php-qrcode to build the QR code.

<?php
declare(strict_types=1);
use chillerlan\QRCode\{QRCode, QROptions};
use chillerlan\QRCode\Output\QRMarkupSVG;
require_once 'vendor/autoload.php';

if (empty($_GET["q"])) {
    echo '<form method=GET><input name=q /></form>';
} else {
    $options = new QROptions();
    $options->outputInterface = QRMarkupSVG::class;
    $options->outputBase64 = false;

    if (PHP_SAPI !== 'cli') {
        header('Content-type: image/svg+xml');
        header('Content-Disposition: filename=qr.svg');
    }

    echo (new QRCode($options))->render(html_entity_decode($_GET["q"]));
}
die();

Example image from this script:


Leave a Reply

Your email address will not be published. Required fields are marked *

To respond on your own website, make a post that contains the link to this post and enter the URL of your response. Want to update or remove your response? Update or delete your post and re-enter your post’s URL again. (Learn More)