-
Notifications
You must be signed in to change notification settings - Fork 1
/
avatars.go
52 lines (44 loc) · 1.04 KB
/
avatars.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package avatars
import (
"bytes"
"html"
"image/png"
"github.com/lafriks/go-avatars/bottts"
"github.com/fogleman/gg"
"github.com/lafriks/go-svg"
"github.com/lafriks/go-svg/renderer"
rendr_gg "github.com/lafriks/go-svg/renderer/gg"
)
func SVG(name string) ([]byte, error) {
attrs, body := bottts.Generate(name)
res := bytes.Buffer{}
res.WriteString("<svg")
for name, val := range attrs {
res.WriteByte(' ')
res.WriteString(name)
res.WriteString(`="`)
res.WriteString(html.EscapeString(val))
res.WriteByte('"')
}
res.WriteString(">\n")
res.WriteString(body)
res.WriteString("\n</svg>")
return res.Bytes(), nil
}
func PNG(name string) ([]byte, error) {
avatar, err := SVG(name)
if err != nil {
return nil, err
}
s, err := svg.Parse(bytes.NewReader(avatar), svg.IgnoreErrorMode)
if err != nil {
return nil, err
}
gc := gg.NewContext(256, 256)
rendr_gg.Draw(gc, s, renderer.Target(0, 0, 256, 256))
buf := bytes.NewBuffer(nil)
if err := png.Encode(buf, gc.Image()); err != nil {
return nil, err
}
return buf.Bytes(), nil
}