Skip to content

Novis\Image\Barcode::svg

Novis\Image\Barcode::svg(
    string $data,
    Symbology $symbology,
    ?{ height?: uint, module?: uint, margin?: uint, text?: bool } $options = null,
): string

Makes a barcode as an SVG document: a text file that a browser shows as a sharp image at any size.

Barcode::svg takes a string and a Barcode::Symbology, which is the type of barcode. It returns the SVG document as a string. You can put this string straight into an HTML page.

The options are the same as for Barcode::render, without format. height is the height of the bars. module is the width of the thinnest bar. margin is the white border on each side, counted in thin bars. text: true adds the data under the bars as a <text> element.

A wrong check digit throws a LogicError. A character the type cannot contain also throws a LogicError.

Good to know: the SVG has a width and a height in pixels. A printed label stays sharp because the browser draws the bars, not a picture of them.

The examples below show a barcode inside an HTML page, an EAN-13 with its number, and the errors.

Parameters

  • $datastring
  • $symbologySymbology
  • $options?{ height?: uint, module?: uint, margin?: uint, text?: bool }default null

Return value

Returns string.

Examples

A barcode in html

<?nvs
// Shows how to put a Code 128 barcode into an HTML page as an SVG image.
use Novis\Image\Barcode;
string $code = Barcode::svg('SHIP-2024-0042', Barcode::Symbology::Code128);
// The SVG document is a string, so it can go straight into the HTML.
string $html = "<div class=\"label\">{$code}</div>";
// This prints the start of the page and the start of the SVG document.
echo Core\Str::slice($html, 0, 19), "\n";
echo Core\Str::slice($code, 0, 39), "\n";
echo Core\Str::endsWith($html, '</svg></div>') ? 'the page is complete' : 'the page is cut', "\n";
Output
<div class="label">
<svg xmlns="http://www.w3.org/2000/svg"
the page is complete

An ean 13 with its number

<?nvs
// Shows how to make an EAN-13 barcode as SVG, with the number printed under the bars.
use Novis\Image\Barcode;
use Novis\Image\Image;
// `text: true` adds the number as a `<text>` element under the bars.
string $code = Barcode::svg('4006381333931', Barcode::Symbology::Ean13, { height: 50, text: true });
echo Core\Str::contains($code, '>4006381333931</text>') ? 'the number is under the bars' : 'no number', "\n";
// The SVG also opens as an image. This prints its size in pixels.
var $image = Image::open($code as bytes)->raw();
echo $image->width, ' by ', $image->height, " pixels\n";
Output
the number is under the bars
230 by 106 pixels

Data the type cannot contain

<?nvs
// Shows the errors `svg` throws when the data does not fit the type of barcode.
use Novis\Image\Barcode;
// Code 39 contains only digits, capital letters, the space and `-.$/+%`.
// The small letters in `order` throw a `LogicError`.
try {
Barcode::svg('order-42', Barcode::Symbology::Code39);
echo "made a barcode\n";
} catch (LogicError $e) {
echo $e->message, "\n";
}
// An EAN-8 has exactly 8 digits, so 7 digits throw a `LogicError`.
try {
Barcode::svg('9638507', Barcode::Symbology::Ean8);
echo "made a barcode\n";
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
Code 39 cannot encode the character 'o' at position 1
EAN-8 takes 8 digits, its check digit last, and was given "9638507"