diff options
author | vulonkaaz <7442677+vulonkaaz@users.noreply.github.com> | 2024-06-19 17:44:29 +0200 |
---|---|---|
committer | vulonkaaz <7442677+vulonkaaz@users.noreply.github.com> | 2024-06-19 17:44:29 +0200 |
commit | 2fe3b4972f02be8b0c6143325d541ddda4b91559 (patch) | |
tree | 5b430810b19e0c6b1112c601b9e64b9e9ec72c61 /themagicpipe/imageconverter.go |
Initial version
Diffstat (limited to 'themagicpipe/imageconverter.go')
-rw-r--r-- | themagicpipe/imageconverter.go | 23 |
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 +} + |