qrcode

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 28 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

laravel-go-qrcode

CI Go reference docs Go 1.27

Go port of simplesoftwareio/simple-qrcode 4.2.0 for styled SVG, PNG and EPS QR codes. Set module and eye shapes, colors, gradients and PNG logos. The laravelqr package adds Laravel for Go responses and view helpers. Tests compare behavior with fixtures generated by the PHP package.

Status: alpha.

Quick start

go get github.com/bowerbird-si/laravel-go-qrcode@v0.1.0
package qrcode_test

import (
	"fmt"
	"log"
	"strings"

	"github.com/bowerbird-si/laravel-go-qrcode"
)

// The quick start: the README shows this example, and CI runs it.
func Example() {
	g, err := qrcode.New(qrcode.Size(200), qrcode.Margin(4))
	if err != nil {
		log.Fatal(err)
	}

	svg, err := g.SVG("https://example.com")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(strings.HasPrefix(string(svg), `<svg xmlns="http://www.w3.org/2000/svg" width="200"`))

	src, err := g.DataURI("https://example.com", qrcode.PNGFormat)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(strings.HasPrefix(src, "data:image/png;base64,"))
	// Output:
	// true
	// true
}

The same code is in example_test.go; CI compiles and runs it.

In a Laravel for Go app, list the package and render a code in a view:

qr, err := laravelqr.New(qrcode.Size(240))
if err != nil {
	log.Fatal(err)
}
laravel.Run(laravel.Options{
	Packages: []laravel.Package{qr},
	// Routes, Views, …
})
{{ qrcode .TicketURL }}
<img src="{{ qrcodeSrc .TicketURL }}" alt="Ticket QR code">

See Laravel for Go for responses and email.

PHP parity

Follows simplesoftwareio/simple-qrcode 4.2.0 (commit 916db7948ca6).

Parity level Abilities Covered Diverged Planned PHP fixtures Last verified
full 50 23 27 0 74 2026-12-04

Every ability is listed in parity.toml, with the tests that cover it or the reason it differs.

Differences from PHP

  • New and With validate typed options. Choose an output format at render time. Pass an open io.Writer to WriteTo to save a file.
  • The automatic encoder may choose a different mask or smaller QR version. Tests check the payload and compare exact modules at controlled settings.
  • Go checks encoding names during configuration and normalizes Shift-JIS aliases. Kanji works in Kanji mode, and mixed Shift-JIS uses ECI 20 in byte mode. ISO-8859-12 can only use numeric or alphanumeric modes, which skip byte transcoding. Bacon #170 · Bacon #172
  • Colors use Go's 0–255 opacity scale. Give the outer finder color first and the inner color second. EPS requires opaque colors.
  • PNG logos come from file bytes or an image supplied by the app. A generator snapshots the logo and preserves its aspect ratio. A logo with an SVG or EPS request returns an option error.
  • Render size is bounded to 4096 pixels per side. PNG requires at least one pixel per module.
  • Encode exposes a read-only matrix for custom drawing. Format, style, eye and fill configuration use native Go options.
  • Payload helpers return typed-validation errors and build text for any renderer. Email escapes spaces and normalizes line endings. Geo coordinates use plain decimals. Phone numbers use hyphens for spaces. SMS uses an escaped body query and requires a recipient. WiFi escapes field delimiters and spells out boolean values. Bitcoin accepts an exact decimal amount and omits the question mark for an empty query. Simple QrCode #169 · Simple QrCode #333
  • Laravel apps register the adapter explicitly and resolve the generator through the application context. Inline SVG is generated as trusted markup, while PNG and EPS responses carry bytes and the appropriate media type.

Installation and configuration

See the guide: options and styles, payloads, Laravel for Go and migrating from PHP.

Compatibility

Module Framework Go
github.com/bowerbird-si/laravel-go-qrcode go.laravel.com v0.2.0 1.27

24 tests · 74 PHP fixtures (counted by kit readme)

Contributing

Bug reports, failing test cases, documentation fixes and features are welcome, including AI-assisted contributions. Run scripts/kit ci and describe what you checked in the pull request. See CONTRIBUTING.md.

Credits

Based on simplesoftwareio/simple-qrcode (MIT). QR encoding uses krobertson/go-qr, and the tests scan output with makiuchi-d/gozxing. Licence notices are in NOTICE.

Status and licence

Maintenance status is active. The package is available under the MIT licence.

Community package, not affiliated with Laravel.

Documentation

Overview

Package qrcode is the Go port of simplesoftwareio/simple-qrcode for styled SVG, PNG and EPS QR codes.

Community package, not affiliated with Laravel.

Example

The quick start: the README shows this example, and CI runs it.

g, err := qrcode.New(qrcode.Size(200), qrcode.Margin(4))
if err != nil {
	log.Fatal(err)
}

svg, err := g.SVG("https://example.com")
if err != nil {
	log.Fatal(err)
}
fmt.Println(strings.HasPrefix(string(svg), `<svg xmlns="http://www.w3.org/2000/svg" width="200"`))

src, err := g.DataURI("https://example.com", qrcode.PNGFormat)
if err != nil {
	log.Fatal(err)
}
fmt.Println(strings.HasPrefix(src, "data:image/png;base64,"))
Output:
true
true
Example (Payload)
password := "guest-code"
text, err := qrcode.WiFi{SSID: "Guest", Auth: qrcode.WiFiWPA, Password: &password}.Payload()
if err != nil {
	log.Fatal(err)
}
fmt.Println(text)
Output:
WIFI:T:WPA;S:Guest;P:guest-code;

Index

Examples

Constants

View Source
const MaxSize = 4096

MaxSize is the largest supported width and height in pixels.

View Source
const Version = "0.1.0"

Version is the version of this module.

Variables

View Source
var (
	// ErrInvalidOption indicates an option or option combination cannot be used.
	ErrInvalidOption = errors.New("qrcode: invalid option")
	// ErrInvalidPayload indicates empty text or an invalid structured payload.
	ErrInvalidPayload = errors.New("qrcode: invalid payload")
	// ErrUnencodable indicates text cannot be encoded with the selected character set.
	ErrUnencodable = errors.New("qrcode: text cannot be encoded")
	// ErrDataTooLong indicates the payload exceeds QR capacity at the selected correction level.
	ErrDataTooLong = errors.New("qrcode: payload exceeds QR capacity")
)

Functions

This section is empty.

Types

type Bitcoin

type Bitcoin struct {
	// Address is a caller-provided Bitcoin address.
	Address string
	// Amount is an optional decimal string denominated in BTC.
	Amount *string
	// Label is the optional recipient label.
	Label *string
	// Message is the optional payment message.
	Message *string
	// ReturnURL fills the legacy r query parameter if provided.
	ReturnURL *string
}

Bitcoin builds a payment request payload, equivalent to PHP BTC.

func (Bitcoin) Payload

func (b Bitcoin) Payload() (string, error)

Payload returns a bitcoin URI with escaped query parameters.

type Email

type Email struct {
	// To is an optional single mailbox address.
	To string
	// Subject is the optional subject line.
	Subject *string
	// Body is the optional message body.
	Body *string
}

Email builds a mailto payload, equivalent to PHP email. Nil fields are omitted.

func (Email) Payload

func (e Email) Payload() (string, error)

Payload returns the escaped mailto URI.

type Eye

type Eye uint8

Eye identifies one of the symbol's three finder patterns.

const (
	// TopLeft is the first finder pattern.
	TopLeft Eye = iota
	// TopRight is the second finder pattern.
	TopRight
	// BottomLeft is the third finder pattern.
	BottomLeft
)

type EyeShape

type EyeShape uint8

EyeShape determines the geometry of a finder pattern.

const (
	// EyeFollowsModule uses the selected module style for the finder pattern.
	EyeFollowsModule EyeShape = iota
	// EyeSquare draws a square ring and center.
	EyeSquare
	// EyeCircle draws a square outer ring with a round center.
	EyeCircle
)

type Format

type Format uint8

Format selects a rendered output type.

const (
	// SVGFormat writes an SVG document, the default in PHP.
	SVGFormat Format = iota
	// PNGFormat writes a PNG image.
	PNGFormat
	// EPSFormat writes an encapsulated PostScript document.
	EPSFormat
)

func (Format) ContentType

func (f Format) ContentType() string

ContentType returns the format's MIME type, or an empty string for invalid formats.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator holds reusable QR settings. Its zero value uses the default settings.

func New

func New(opts ...Option) (Generator, error)

New creates a generator with the supplied options applied in order.

func (Generator) DataURI

func (g Generator) DataURI(text string, format Format) (string, error)

DataURI renders a PNG or SVG and returns its base64 data URI.

func (Generator) EPS

func (g Generator) EPS(text string) ([]byte, error)

EPS renders an encapsulated PostScript document, equivalent to PHP generate with format eps.

func (Generator) Encode

func (g Generator) Encode(text string) (Matrix, error)

Encode builds a matrix from UTF-8 input without rendering it.

func (Generator) Generate

func (g Generator) Generate(text string, format Format) ([]byte, error)

Generate renders text in the chosen format, equivalent to PHP generate.

func (Generator) PNG

func (g Generator) PNG(text string) ([]byte, error)

PNG renders a PNG image, equivalent to PHP generate with format png.

func (Generator) SVG

func (g Generator) SVG(text string) ([]byte, error)

SVG renders an SVG document, equivalent to PHP generate with format svg.

func (Generator) With

func (g Generator) With(opts ...Option) (Generator, error)

With returns a copy of the generator with additional options applied in order.

func (Generator) WriteTo

func (g Generator) WriteTo(w io.Writer, text string, format Format) error

WriteTo renders to a caller-supplied writer and propagates write errors.

type Geo

type Geo struct {
	// Latitude is in decimal degrees from -90 to 90.
	Latitude float64
	// Longitude is in decimal degrees from -180 to 180.
	Longitude float64
}

Geo builds a geographic location payload, equivalent to PHP geo.

func (Geo) Payload

func (g Geo) Payload() (string, error)

Payload returns a geo URI with decimal coordinates.

type GradientType

type GradientType uint8

GradientType determines the orientation of a two-color foreground gradient.

const (
	// Vertical runs from top to bottom.
	Vertical GradientType = iota
	// Horizontal runs from left to right.
	Horizontal
	// Diagonal runs from top left to bottom right.
	Diagonal
	// InverseDiagonal runs from bottom left to top right.
	InverseDiagonal
	// Radial runs from the center to the outer edge.
	Radial
)

type Level

type Level uint8

Level is the QR symbol's error correction level.

const (
	// Low (L) is the default level and restores about 7% of codewords.
	Low Level = iota
	// Medium (M) restores about 15% of codewords.
	Medium
	// Quartile (Q) restores about 25% of codewords.
	Quartile
	// High (H) restores about 30% of codewords.
	High
)

type Matrix

type Matrix struct {
	// contains filtered or unexported fields
}

Matrix is an immutable QR symbol without a quiet zone. Coordinates are zero-based.

func (Matrix) At

func (m Matrix) At(x, y int) bool

At reports whether the module at column x, row y is dark. Outside the symbol it returns false.

func (Matrix) Level

func (m Matrix) Level() Level

Level returns the symbol's error correction level.

func (Matrix) Size

func (m Matrix) Size() int

Size returns the number of modules on one side of the symbol.

func (Matrix) Version

func (m Matrix) Version() int

Version returns the QR version, or zero for an empty Matrix.

type Option

type Option func(*Generator) error

Option configures a Generator. Use the option functions to construct one.

func Background

func Background(c color.NRGBA) Option

Background sets the symbol's background color, equivalent to PHP backgroundColor.

func Encoding

func Encoding(name string) Option

Encoding selects the symbol's character set, equivalent to PHP encoding. The text passed to Encode and the render methods is always UTF-8.

func ErrorCorrection

func ErrorCorrection(level Level) Option

ErrorCorrection selects the correction level, equivalent to PHP errorCorrection.

func EyeColor

func EyeColor(eye Eye, outer, inner color.NRGBA) Option

EyeColor colors one finder pattern's outer ring and inner center, equivalent to PHP eyeColor. The arguments are named for their physical positions.

func EyeStyle

func EyeStyle(shape EyeShape) Option

EyeStyle sets finder geometry, equivalent to PHP eye.

func Foreground

func Foreground(c color.NRGBA) Option

Foreground sets the default color of dark modules, equivalent to PHP color.

func Gradient

func Gradient(from, to color.NRGBA, kind GradientType) Option

Gradient applies a two-color foreground blend, equivalent to PHP gradient. Explicit finder colors override it.

func Logo(source image.Image, fraction float64) Option

Logo adds a centered logo to PNG output, equivalent to PHP merge. Fraction sets the logo's maximum width relative to the output, typically 0.2. The option snapshots the image and can be reused across generators.

func LogoBytes

func LogoBytes(data []byte, fraction float64) Option

LogoBytes decodes PNG, JPEG or the first frame of a GIF, equivalent to PHP mergeString.

func Margin

func Margin(modules int) Option

Margin sets the quiet zone on each side in modules, equivalent to PHP margin.

func ModuleStyle

func ModuleStyle(style Style, amount float64) Option

ModuleStyle sets the shape and diameter or roundness, equivalent to PHP style. Square ignores amount. Use 0.5 for PHP's default amount.

func Size

func Size(px int) Option

Size sets the output width and height in pixels, equivalent to PHP size.

type PhoneNumber

type PhoneNumber struct {
	// Number is the telephone number, which may contain vanity letters.
	Number string
}

PhoneNumber builds a telephone payload, equivalent to PHP phoneNumber.

func (PhoneNumber) Payload

func (p PhoneNumber) Payload() (string, error)

Payload returns a tel URI.

type SMS

type SMS struct {
	// Number is the message recipient.
	Number string
	// Body is the optional message to prefill.
	Body *string
}

SMS builds a text message payload, equivalent to PHP SMS.

func (SMS) Payload

func (s SMS) Payload() (string, error)

Payload returns an RFC 5724 sms URI.

type Style

type Style uint8

Style determines the geometry of the symbol's dark modules.

const (
	// Square uses full square modules.
	Square Style = iota
	// Dot draws circular modules.
	Dot
	// Round rounds corners along the outlines of connected groups of modules.
	Round
)

type WiFi

type WiFi struct {
	// SSID is the network name.
	SSID string
	// Auth is the authentication scheme.
	Auth WiFiAuth
	// Password is optional. Nil omits the field.
	Password *string
	// Hidden states whether to connect to a hidden network. Nil omits the field.
	Hidden *bool
}

WiFi builds a scanner configuration payload, equivalent to PHP WiFi.

func (WiFi) Payload

func (w WiFi) Payload() (string, error)

Payload returns an escaped WIFI scanner payload.

type WiFiAuth

type WiFiAuth string

WiFiAuth names a network's authentication scheme.

const (
	// WiFiOpen omits the authentication field.
	WiFiOpen WiFiAuth = ""
	// WiFiNoPassword explicitly declares an open network.
	WiFiNoPassword WiFiAuth = "nopass"
	// WiFiWEP is WEP authentication.
	WiFiWEP WiFiAuth = "WEP"
	// WiFiWPA is WPA or WPA2 authentication.
	WiFiWPA WiFiAuth = "WPA"
	// WiFiSAE is WPA3-Personal authentication.
	WiFiSAE WiFiAuth = "SAE"
)

Directories

Path Synopsis
Package laravelqr integrates QR responses and template functions with Laravel for Go.
Package laravelqr integrates QR responses and template functions with Laravel for Go.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL