aboutsummaryrefslogtreecommitdiff
path: root/themagicpipe/imageconverter.go
blob: 38f7eb1f663af3219343e2fab278d68257761038 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package themagicpipe

import (
	"os/exec"
	"encoding/base64"
	"errors"
	"bytes"
	"strings"
)

// 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")
	}
	imageBase64 := strings.TrimPrefix(dataURL, "data:image/png;base64,")
	imageRaw, err := base64.StdEncoding.DecodeString(imageBase64)
	if err != nil {
		return "", err
	}

	converter := exec.Command("convert", "-", "-background", "white", "-flatten", "-resize", "400x200!", "-remap", "palette.png", "PNG8:-")
	converter.Stdin = bytes.NewBuffer(imageRaw)

	var output []byte
	output, err = converter.Output()
	if err != nil {
		return "", err
	}

	imageBase64Processed := base64.StdEncoding.EncodeToString(output)
	return "data:image/png;base64," + imageBase64Processed, nil
}