svg2png/svg2png.go
2022-03-07 16:32:51 +00:00

30 lines
608 B
Go

package svg2png
import (
"bytes"
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
"image"
"image/png"
"io"
)
func RasterSvgToPng(r io.Reader) ([]byte, error) {
svg, err := oksvg.ReadIconStream(r, oksvg.StrictErrorMode)
if err != nil {
return nil, err
}
w, h := int(svg.ViewBox.W), int(svg.ViewBox.H)
img := image.NewRGBA(image.Rect(0, 0, w, h))
scannerGV := rasterx.NewScannerGV(0, 0, img, img.Bounds())
raster := rasterx.NewDasher(w, h, scannerGV)
svg.Draw(raster, 1)
b := new(bytes.Buffer)
err = png.Encode(b, img)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}