summaryrefslogtreecommitdiffstats
path: root/htmlimg.go
diff options
context:
space:
mode:
Diffstat (limited to 'htmlimg.go')
-rw-r--r--htmlimg.go23
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
+}