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)| Level | Restores about | PHP |
|---|---|---|
qrcode.Low (default) | 7% | 'L' |
qrcode.Medium | 15% | 'M' |
qrcode.Quartile | 25% | 'Q' |
qrcode.High | 30% | '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)| Style | Draws | Amount |
|---|---|---|
qrcode.Square (default) | Full squares | Accepted from 0 to below 1 |
qrcode.Dot | A circle per module | Diameter, above 0 and below 1 |
qrcode.Round | Connected groups with rounded corners | Roundness, 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)| Shape | Draws |
|---|---|
qrcode.EyeFollowsModule (default) | The module style |
qrcode.EyeSquare | A square ring and a square center |
qrcode.EyeCircle | A 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.
| Type | Runs |
|---|---|
qrcode.Vertical | Top to bottom |
qrcode.Horizontal | Left to right |
qrcode.Diagonal | Top left to bottom right |
qrcode.InverseDiagonal | Bottom left to top right |
qrcode.Radial | Center 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.
Logo
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 animage.Imageyou already have.fractionis 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.