diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2026-07-11 13:09:00 -0230 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2026-07-11 13:09:00 -0230 |
| commit | 6c1f9200881b859bcc6d7d5b0d9d37a5cc65eb60 (patch) | |
| tree | 7095e5f52f3a62b08135810e59f9a233b861fc4e /htmlimg.go | |
| download | htmlimg-6c1f9200881b859bcc6d7d5b0d9d37a5cc65eb60.zip | |
encode
Diffstat (limited to 'htmlimg.go')
| -rw-r--r-- | htmlimg.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/htmlimg.go b/htmlimg.go new file mode 100644 index 0000000..332bc69 --- /dev/null +++ b/htmlimg.go @@ -0,0 +1,23 @@ +// Package htmlimg produces base64-encoded HTML <img> tags. +// +// <img src="data:image/png;base64,iVBORw..."> +package htmlimg + +import ( + "bytes" + "encoding/base64" + "html/template" + "image" + "image/png" +) + +// Encode PNG- and then base64-encodes img and returns it embedded in an +// HTML <img> tag. +func Encode(m image.Image) (template.HTML, error) { + pngBuf := new(bytes.Buffer) + if err := png.Encode(pngBuf, m); err != nil { + return "", err + } + b64 := base64.StdEncoding.EncodeToString(pngBuf.Bytes()) + return template.HTML(`<img src="data:image/png;base64,` + b64 + `">`), nil +} |