Novis\Image\QrCode::svg
Makes a QR code as an SVG document: a text file that a browser shows as a sharp image at any size.
QrCode::svg takes a string, such as a link, and returns the QR code as an SVG document. The document is a string, 256 by 256 pixels, with a white border of 4 squares on every side. You can put it straight into an HTML page.
The options change the code. margin is the width of the white border, in squares. level is a QrCode::Level, and sets how much damage a scanner can repair.
Text too long for the level throws a LogicError.
Good to know: QrCode::svg has no size option. The browser scales the SVG to any size, and the squares stay sharp.
The examples below show a link as an SVG, a QR code inside an HTML page, and the border and level.
Parameters
$datastring$options?{ margin?: uint, level?: Level }default null
Return value
Returns string.
Examples
A link as an svg
<?nvs// Shows how to make a QR code for a link as an SVG document.use Novis\Image\Image;use Novis\Image\QrCode;
string $code = QrCode::svg('https://example.com/orders/1042');
// The result is a string that starts with an `<svg>` tag.echo Core\Str::slice($code, 0, 39), "\n";
// The SVG also opens as an image of 256 by 256 pixels.var $image = Image::open($code as bytes)->raw();echo $image->width, ' by ', $image->height, " pixels\n";<svg xmlns="http://www.w3.org/2000/svg"256 by 256 pixelsA qr code in html
<?nvs// Shows how to put a QR code into an HTML page, so a phone can open the page.use Novis\Image\QrCode;
string $link = 'https://example.com/blog/hello';string $code = QrCode::svg($link);
// The SVG document is a string, so it goes straight into the HTML.string $html = "<figure>{$code}<figcaption>Scan to read the post</figcaption></figure>";
// This prints the start of the page and checks that the SVG is inside it.echo Core\Str::slice($html, 0, 12), "\n";echo Core\Str::contains($html, '</svg><figcaption>') ? 'the code is in the page' : 'no code', "\n";<figure><svgthe code is in the pageBorder and level
<?nvs// Shows how to set the white border and the level of a QR code.use Novis\Image\QrCode;
// The `viewBox` of the SVG counts the squares across the code, with its border.// The default border is 4 squares, so this code is 29 squares wide.string $plain = QrCode::svg('Shop');echo Core\Str::slice($plain, 65, 19), "\n";
// `margin: 1` makes the border 1 square wide, so this code is 23 squares wide.// `level: QrCode::Level::High` lets a scanner read the code even when part of it is covered.string $strong = QrCode::svg('Shop', { margin: 1, level: QrCode::Level::High });echo Core\Str::slice($strong, 65, 19), "\n";
// A QR code contains at most a few thousand characters, so 5000 throw a `LogicError`.try { QrCode::svg(Core\Str::repeat('a', 5000)); echo "made a code\n";} catch (LogicError $e) { echo "too long: LogicError\n";}viewBox="0 0 29 29"viewBox="0 0 23 23"too long: LogicError