Options and styles

laravel-go-qrcode Alpha Laravel for Go

Pass options to qrcode.New, Generator.With, laravelqr.New, laravelqr.Response or the view functions. They apply in order, so a later option wins.

Size and margin

qrcode.Size(300)  // width and height in pixels, 1–4096; default 100
qrcode.Margin(2)  // quiet zone in modules on each side; default 0

Scanners read codes more reliably with a margin of at least four modules. PNG output needs at least one pixel per module, so a long text at a small size returns ErrInvalidOption.

Error correction

qrcode.ErrorCorrection(qrcode.High)
LevelRestores aboutPHP
qrcode.Low (default)7%'L'
qrcode.Medium15%'M'
qrcode.Quartile25%'Q'
qrcode.High30%'H'

Use High when you add a logo: the logo covers part of the code.

Colors

qrcode.Foreground(color.NRGBA{R: 30, G: 64, B: 175, A: 255})
qrcode.Background(color.NRGBA{R: 255, G: 255, B: 255, A: 255})

Colors are image/color NRGBA values. A is opacity from 0 (transparent) to 255 (opaque). SVG and PNG support transparency; EPS returns ErrInvalidOption for any color that isn't opaque.

Module style

qrcode.ModuleStyle(qrcode.Dot, 0.5)
StyleDrawsAmount
qrcode.Square (default)Full squaresAccepted from 0 to below 1
qrcode.DotA circle per moduleDiameter, above 0 and below 1
qrcode.RoundConnected groups with rounded cornersRoundness, above 0 and below 1

PHP's default amount is 0.5.

Eyes

The three finder patterns in the corners are the eyes.

qrcode.EyeStyle(qrcode.EyeCircle)
qrcode.EyeColor(qrcode.TopLeft, outer, inner)
ShapeDraws
qrcode.EyeFollowsModule (default)The module style
qrcode.EyeSquareA square ring and a square center
qrcode.EyeCircleA square ring and a round center

EyeColor sets one eye: qrcode.TopLeft, qrcode.TopRight or qrcode.BottomLeft. The first color is the outer ring, the second the center. Call it once per eye you want to color.

Gradient

qrcode.Gradient(
	color.NRGBA{R: 30, G: 64, B: 175, A: 255},
	color.NRGBA{R: 124, G: 58, B: 237, A: 255},
	qrcode.Diagonal,
)

The gradient replaces the foreground color. Eyes colored with EyeColor keep their own colors.

TypeRuns
qrcode.VerticalTop to bottom
qrcode.HorizontalLeft to right
qrcode.DiagonalTop left to bottom right
qrcode.InverseDiagonalBottom left to top right
qrcode.RadialCenter to edge

Encoding

qrcode.Encoding("UTF-8")

You always pass text as a Go (UTF-8) string; Encoding sets the character set written into the code. The default is ISO-8859-1, like PHP. Names are case-insensitive:

Byte encodings include ISO-8859-1 through ISO-8859-11, ISO-8859-13 through ISO-8859-16, Shift-JIS (also SJIS and SHIFT_JIS), Windows-1250, Windows-1251, Windows-1252, Windows-1256, UTF-16BE, UTF-8, ASCII, GBK and EUC-KR. ISO-8859-12 remains accepted for numeric and alphanumeric segments, which use QR-specific alphabets.

An unknown name returns ErrInvalidOption when you set it. Text that the character set can't represent returns ErrUnencodable when you render. For text outside Western European characters, use UTF-8.

A logo is drawn in the center of PNG output.

data, err := os.ReadFile("resources/logo.png")
if err != nil {
	return err
}
g, err := qrcode.New(
	qrcode.Size(400),
	qrcode.ErrorCorrection(qrcode.High),
	qrcode.LogoBytes(data, 0.2),
)
if err != nil {
	return err
}
png, err := g.PNG("https://example.com")
  • LogoBytes(data, fraction) decodes PNG, JPEG or the first frame of a GIF, up to 16 MiB.
  • Logo(img, fraction) takes an image.Image you already have.
  • fraction is the logo's largest width relative to the code, typically 0.2. The logo keeps its aspect ratio.
  • The option copies the image when you create it, so you can reuse it across generators and goroutines.
  • Rendering SVG or EPS with a logo returns ErrInvalidOption.

Scan the result at the size you will print it: a large logo at a low error correction level can make the code unreadable.

Custom rendering

Encode returns the symbol without rendering it:

m, err := g.Encode("https://example.com")
if err != nil {
	return err
}
for y := 0; y < m.Size(); y++ {
	for x := 0; x < m.Size(); x++ {
		if m.At(x, y) {
			// draw a dark module
		}
	}
}

m.Version() and m.Level() report the QR version and error correction level. The matrix has no quiet zone; add your own margin.