i forgor to commit sorry
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/png"
|
||||
"github.com/srwiley/oksvg"
|
||||
"github.com/srwiley/rasterx"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user