Novis\Image\Barcode::render
Novis\Image\Barcode::render(
string $data,
Symbology $symbology,
?{ height?: uint, module?: uint, margin?: uint, text?: bool, format?: Format } $options = null,
): bytesMakes a barcode image: lines of bars that a scanner reads, or a square DataMatrix code.
Barcode::render takes a string and a Barcode::Symbology, which is the type of barcode. It returns the barcode as an image file. By default the file is a PNG, and the bars are 80 pixels high.
Code128 takes any plain ASCII text, such as a tracking number. Ean13 takes the 13 digits printed on a product.
The options change the image. height is the height of the bars in pixels. module is the width of the thinnest bar in pixels. margin is the white border on each side, counted in thin bars. text: true prints the data under the bars. format is the file Format.
A wrong check digit, or a character the type cannot contain, throws a LogicError.
Good to know: render does not add a check digit. Give all the digits, with the check digit last.
The examples below show a shipping label, an EAN-13 with its number, and the errors.
Parameters
$datastring$symbologySymbology$options?{ height?: uint, module?: uint, margin?: uint, text?: bool, format?: Format }default null
Return value
Returns bytes.
Examples
A shipping label
<?nvs// Shows how to make a Code 128 barcode of a tracking number for a shipping label.use Novis\Image\Barcode;use Novis\Image\Image;
bytes $label = Barcode::render('SHIP-2024-0042', Barcode::Symbology::Code128);
// The result is a PNG file. The bars are 80 pixels high, with a white border around them.var $info = Image::info($label);echo $info->width, ' by ', $info->height, " pixels\n";echo Image::mime($info->format), "\n";418 by 120 pixelsimage/pngA product ean 13
<?nvs// Shows how to make the EAN-13 barcode of a product, with its number printed under the bars.use Novis\Image\Barcode;use Novis\Image\Format;use Novis\Image\Image;
// `module: 3` makes the thinnest bar 3 pixels wide.// `margin: 5` adds a white border 5 thin bars wide on each side.bytes $code = Barcode::render('4006381333931', Barcode::Symbology::Ean13, { height: 60, module: 3, margin: 5, text: true, format: Format::Webp,});
// This prints the size of the image and the WebP MIME type.var $info = Image::info($code);echo $info->width, ' by ', $info->height, " pixels\n";echo Image::mime($info->format), "\n";315 by 114 pixelsimage/webpA wrong check digit
<?nvs// Shows the errors `render` throws when the data does not fit the type of barcode.use Novis\Image\Barcode;
// The last digit of an EAN-13 is a check digit. For these 12 digits it is 1, so 2 throws a `LogicError`.try { Barcode::render('4006381333932', Barcode::Symbology::Ean13); echo "made a barcode\n";} catch (LogicError $e) { echo $e->message, "\n";}
// Code 128 contains only plain ASCII characters, so `ö` throws a `LogicError`.try { Barcode::render('Größe 42', Barcode::Symbology::Code128); echo "made a barcode\n";} catch (LogicError $e) { echo $e->message, "\n";}the check digit of the EAN-13 4006381333932 is 1, and the data ends in 2Code 128 cannot encode the character 'ö' at position 3