aboutsummaryrefslogtreecommitdiff
path: root/themagicpipe
diff options
context:
space:
mode:
Diffstat (limited to 'themagicpipe')
-rw-r--r--themagicpipe/imageconverter.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/themagicpipe/imageconverter.go b/themagicpipe/imageconverter.go
new file mode 100644
index 0000000..5e5738e
--- /dev/null
+++ b/themagicpipe/imageconverter.go
@@ -0,0 +1,23 @@
+package themagicpipe
+import (
+ "os/exec"
+ "strings"
+ "errors"
+)
+
+// Where the magic happens, the base64 data url image is sent through Imagemagick
+// and converted to indexed colors 400x200 png
+func DataURLConverter(dataURL string) (string, error) {
+ if !strings.HasPrefix(dataURL, "data:image/png;base64,") {
+ return "", errors.New("invalid dataURL")
+ }
+ cmd := "base64 -d | convert - -background white -flatten -resize 400x200! -colors 4 PNG8:- | base64 -w0"
+ converter := exec.Command("sh","-c",cmd)
+ converter.Stdin = strings.NewReader(strings.TrimPrefix(dataURL, "data:image/png;base64,"))
+ output, err := converter.Output()
+ if err != nil {
+ return "", err
+ }
+ return "data:image/png;base64,"+string(output), nil
+}
+