package web import ( "bytes" "encoding/base64" "image/png" "net/http" "github.com/boombuler/barcode" "github.com/boombuler/barcode/code128" qrcode "github.com/skip2/go-qrcode" ) // qrDataURI renders content as a QR code PNG and returns it as a data: URI // suitable for an , so labels need no separate image requests. func qrDataURI(content string, size int) (string, error) { png, err := qrcode.Encode(content, qrcode.Medium, size) if err != nil { return "", err } return "data:image/png;base64," + base64.StdEncoding.EncodeToString(png), nil } // barcodeDataURI renders content as a Code128 linear barcode PNG, scaled to // widthPx x heightPx, and returns it as a data: URI. func barcodeDataURI(content string, widthPx, heightPx int) (string, error) { bc, err := code128.Encode(content) if err != nil { return "", err } scaled, err := barcode.Scale(bc, widthPx, heightPx) if err != nil { return "", err } var buf bytes.Buffer if err := png.Encode(&buf, scaled); err != nil { return "", err } return "data:image/png;base64," + base64.StdEncoding.EncodeToString(buf.Bytes()), nil } // requestOrigin reconstructs the scheme+host the request arrived on, for // building absolute URLs (e.g. QR codes need a full URL, not a path). func requestOrigin(r *http.Request) string { scheme := "http" if r.TLS != nil { scheme = "https" } if proto := r.Header.Get("X-Forwarded-Proto"); proto != "" { scheme = proto } return scheme + "://" + r.Host }