From 2fe3b4972f02be8b0c6143325d541ddda4b91559 Mon Sep 17 00:00:00 2001 From: vulonkaaz <7442677+vulonkaaz@users.noreply.github.com> Date: Wed, 19 Jun 2024 17:44:29 +0200 Subject: Initial version --- themagicpipe/imageconverter.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 themagicpipe/imageconverter.go (limited to 'themagicpipe/imageconverter.go') 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 +} + -- cgit v1.2.3