Migrating from PHP

laravel-go-barcode Alpha Laravel for Go

This page lists each milon/barcode call with its Go equivalent, and the places where the Go port behaves differently.

In PHP the size, colour and text settings are positional arguments of each call. In Go they are options of a generator (barcode.New1D, barcode.New2D) or of one call (With, or the trailing options of the laravelbarcode functions). In the table, gen is a barcode.Generator1D or Generator2D, and ctx is a request context in a Laravel for Go application.

Linear barcodes (DNS1D)

PHPGo
DNS1D::getBarcodeSVG($code, 'C39', 2, 30, 'black', true)gen.SVG(code, barcode.C39) with BarWidth(2), Height(30), ForegroundCSS("black"), ShowCode(true)
getBarcodeSVG(..., inline: true)InlineSVG(true), or the barcode1D template function
DNS1D::getBarcodeHTML($code, 'C39')gen.HTML(code, barcode.C39), or barcode1DHTML in a template
DNS1D::getBarcodePNG($code, 'C39', ..., $bgcolor) (base64)gen.Base64(code, barcode.C39, barcode.PNGFormat); raw bytes with gen.PNG; Background(color.NRGBA{…}) for $bgcolor
DNS1D::getBarcodeJPG($code, 'C39') (base64)gen.Base64(code, barcode.C39, barcode.JPEGFormat); raw bytes with gen.JPEG
DNS1D::getBarcodePNGPath($code, 'C39', ..., $filename)laravelbarcode.StoreAs1D(ctx, filename, code, barcode.C39, barcode.PNGFormat), or Store1D without a filename; the path is Stored.Path
DNS1D::getBarcodePNGUri($code, 'C39')laravelbarcode.Store1D(ctx, code, barcode.C39, barcode.PNGFormat); the URI is Stored.URI
getBarcodeJPGPath, getBarcodeJPGUriThe same functions with barcode.JPEGFormat
DNS1D::setPadding($p)Padding(p)
The type string, such as 'c128'A constant such as barcode.C128, or barcode.ParseType1D("c128")

Matrix barcodes (DNS2D)

PHPGo
DNS2D::getBarcodeSVG($code, 'QRCODE', 3, 3, 'black')gen.SVG(code, qr) with ModuleSize(3, 3), ForegroundCSS("black")
DNS2D::getBarcodeHTML($code, 'QRCODE')gen.HTML(code, qr), or barcode2DHTML in a template
DNS2D::getBarcodePNG($code, 'QRCODE') (base64)gen.Base64(code, qr, barcode.PNGFormat); raw bytes with gen.PNG; barcode2DSrc gives a data URI in a template
DNS2D::getBarcodePNGPath($code, 'DATAMATRIX', ..., $filename)laravelbarcode.StoreAs2D(ctx, filename, code, barcode.DataMatrix(), barcode.PNGFormat), or Store2D
DNS2D::getBarcodePNGUri($code, 'DATAMATRIX')laravelbarcode.Store2D(ctx, code, barcode.DataMatrix(), barcode.PNGFormat); the URI is Stored.URI
DNS2D::setPadding($p)Padding(p)
DNS2D::setLogo($path, $widthPercent)LogoBytes(data, fraction) or Logo(img, fraction), with the file read by your code
'QRCODE,H'barcode.QR(barcode.QRErrorCorrection(barcode.QRHigh)), or barcode.ParseType2D("QRCODE,H")
'DATAMATRIX'barcode.DataMatrix()
'PDF417,3,5'barcode.PDF417(barcode.AspectRatio(3), barcode.PDF417ErrorCorrection(barcode.PDF417EC5))
'PDF417,,,2,0,1,report.pdf' (Macro PDF417)barcode.PDF417(barcode.Macro(2, 0, 1, barcode.FileName("report.pdf")))

Laravel

PHPGo
BarcodeServiceProviderlaravelbarcode.New(...) in the application's Packages
The DNS1D and DNS2D facadeslaravelbarcode.From1D(ctx) and From2D(ctx), or Response1D and Response2D for a route
config('barcode.store_path'), setStorPath($path)laravelbarcode.StoreDir(dir), plus StoreURL(base) for the public URI
{!! DNS1D::getBarcodeHTML(...) !!} in Blade{{ barcode1DHTML "code" "C39" }} in a Go template
InvalidBarcodeExceptionbarcode.ErrUnsupportedType, ErrInvalidCode or ErrInvalidOption, matched with errors.Is
WrongCheckDigitException*barcode.CheckDigitError (errors.As), which wraps ErrCheckDigit

The defaults match PHP's: bar width 2, height 30, text shown in SVG and hidden in HTML, PNG and JPEG, module size 3 for SVG and PNG and 10 for HTML, black on a transparent PNG.

Output parity

  • SVG and HTML of linear symbols match the PHP output byte for byte, except in the cases listed below.
  • PNG and JPEG are compared as decoded images: size, colours, bar positions and scanlines. The compressed bytes and the font of the printed text are the port's own.
  • QR Code, Data Matrix and PDF417 may choose a different module grid from PHP (the QR mask, the compaction modes, the symbol size). Given the same grid, SVG and HTML match PHP's byte for byte. In the tests, separate decoder paths read the Go symbols back and must return the input.
  • Stored files get the same name and URI as PHP's for ASCII codes. A stored matrix PNG draws each module exactly; PHP's file variant draws one extra pixel on each module's far edges.

Differences

Each of these is deliberate. design/SPEC.md in the repository, section 9, specifies them in full.

Data encoding

  • Codabar keeps start and stop letters you supply. It uses A for a missing letter at either end and rejects letters A through D inside the data. PHP adds A at both ends, so A1234B becomes AA1234BA.
  • Code 39 Full ASCII (C39E, C39E+) and Code 93 (C93) preserve lowercase letters and encode the vertical tab (0x0B). PHP uppercases the input and rejects the vertical tab.
  • Code 128 control runs. In automatic C128, a run of two or more bytes that needs the other subset (for example several control bytes while Subset B is active) uses a Code A or Code B latch. A Shift covers exactly one byte. PHP can put one Shift before a whole run, so a scanner reads different bytes: AB, the control bytes 0x03 0x04 0x04 and B (41 42 03 04 04 42) decodes as 41 42 03 64 64 42 from PHP's symbol and as the input from the port's.
  • Empty 2 of 5. S25, S25+, I25 and I25+ need at least one digit. PHP draws a symbol from empty input.
  • Stricter input. Over-long EAN2, EAN5, EAN8, EAN13, UPCA and UPCE input is rejected (PHP cuts it); letters in POSTNET, PLANET and IMB and punctuation in RMS4CC and KIX are rejected; PHARMA and PHARMA2T enforce the standard's range; GS1-128 input must be well formed with printable ASCII values. PHP cuts or drops such data, and never returns for some of it.
  • QR payload \0. The two bytes backslash and zero are encoded and decode back. PHP rejects them.

Type strings

  • The QRCODE level is case-insensitive; an unknown level is an error. PHP encodes a lowercase or padded level, such as l or H, as level L.
  • The PDF417 aspect ratio, error correction level and every Macro field are validated. The Macro file ID is a number from 0 to 899. Macro PDF417 follows the standard's control block, which standard decoders read back.

Rendering

  • A negative padding is ErrInvalidOption. PHP uses 0.
  • Text that does not fit in the height is ErrInvalidOption; PHP draws digits over the bars or bars with a negative height. With the default height of 30, a retail SVG with its digits needs Height(31) or more.
  • Label text is escaped and CSS colours are validated before they go into SVG or HTML. PHP inserts both as given.
  • Limits. Input length, canvas size, module and font size, logo size and type strings have limits. PHP has none.
  • Logos come from memory, never from a path or URL. A logo that cannot be decoded is an error (PHP ignores it), and a logo that does not fit is scaled down.

Laravel and stored files

  • Configuration is code (laravelbarcode.New), not a published config file. The store directory must exist; there is no fallback to the temporary directory.
  • Generators are values. Padding, logo and store settings belong to a generator or the package options, never to a shared instance that one call changes for the next.
  • Default file names match PHP's slug for ASCII text. Other letters are transliterated only when they are in Latin-1 or Latin Extended-A (é to e, ß to ss, Ø to o, Ł to l, Œ to oe); letters of other scripts are dropped, so the slug of Straße 界 is strasse. Laravel's slug helper transliterates more scripts, so those names can differ from PHP's. Give such codes a custom name with StoreAs1D or StoreAs2D.
  • Writes are confined and atomic. A custom name keeps only its last path segment, unsafe names are rejected, and each file is written to a temporary file and renamed into place.
  • Errors are typed. The check-digit error always names both digits; PHP's message leaves out a digit that is 0 and swaps the two.