violet/favicons/svg2png.go

37 lines
891 B
Go
Raw Normal View History

2023-04-22 22:18:39 +01:00
package favicons
import (
"bytes"
"fmt"
"os/exec"
)
2023-04-24 01:35:23 +01:00
// svg2png takes an input inkscape binary path and svg image bytes and outputs
// the png image bytes or an error.
2023-04-22 22:18:39 +01:00
func svg2png(inkscapeCmd string, in []byte) (out []byte, err error) {
2023-04-24 01:35:23 +01:00
// create stdout and stderr buffers
2023-04-22 22:18:39 +01:00
var stdout, stderr bytes.Buffer
2023-04-24 01:35:23 +01:00
// prepare inkscape command and attach buffers
2023-04-22 22:18:39 +01:00
cmd := exec.Command(inkscapeCmd, "--export-type", "png", "--export-filename", "-", "--export-background-opacity", "0", "--pipe")
cmd.Stdin = bytes.NewBuffer(in)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
2023-04-24 01:35:23 +01:00
// run the command and return errors
2023-04-22 22:18:39 +01:00
if e := cmd.Run(); e != nil {
err = fmt.Errorf("%s\nSTDERR:\n%s", e.Error(), stderr.String())
return
}
2023-04-24 01:35:23 +01:00
// error if there is no output
2023-04-22 22:18:39 +01:00
if stdout.Len() == 0 {
err = fmt.Errorf("got no data from inkscape")
return
}
2023-04-24 01:35:23 +01:00
// return the raw bytes
2023-04-22 22:18:39 +01:00
out = stdout.Bytes()
return
}