Files
lambda/svg-conv.go
T

41 lines
859 B
Go
Raw Normal View History

2026-04-06 11:04:53 +01:00
package main
import (
"bytes"
"fmt"
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
"image"
"image/png"
2026-04-06 11:04:53 +01:00
)
func SVGToPNG(svgData []byte) ([]byte, error) {
// Parse SVG
icon, err := oksvg.ReadIconStream(bytes.NewReader(svgData))
if err != nil {
return nil, fmt.Errorf("failed to parse SVG: %w", err)
}
w := int(icon.ViewBox.W)
h := int(icon.ViewBox.H)
if w == 0 || h == 0 {
w, h = 100, 100
}
// Rasterize into an RGBA image
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
scanner := rasterx.NewScannerGV(w, h, rgba, rgba.Bounds())
dasher := rasterx.NewDasher(w, h, scanner)
icon.SetTarget(0, 0, float64(w), float64(h))
icon.Draw(dasher, 1.0)
// Encode to PNG bytes
var buf bytes.Buffer
if err := png.Encode(&buf, rgba); err != nil {
return nil, fmt.Errorf("failed to encode PNG: %w", err)
}
return buf.Bytes(), nil
}