Files
go-pixelnebula/examples/08_random_avatar_generator.go
2025-03-19 21:10:19 +08:00

34 lines
659 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 08_random_avatar_generator.go
package main
import (
"github.com/landaiqing/go-pixelnebula"
"math/rand"
"os"
"strconv"
"time"
)
const (
savePath = "random_generated_avatars.svg"
)
// 随机生成头像
// Note: 随机传入id即可随机生成头像固定的id会生成相同的头像
func main() {
// 创建随机数
rand.Seed(time.Now().UnixNano())
randomInt := rand.Intn(100)
pixelNebula := pixelnebula.NewPixelNebula().WithDefaultCache()
svg, err := pixelNebula.Generate(strconv.Itoa(randomInt), false).ToSVG()
if err != nil {
panic(err)
}
// 保存图片
os.WriteFile(savePath, []byte(svg), 0644)
defer os.Remove(savePath)
}