feat: 添加喜报功能
This commit is contained in:
37
service/xibao/image_gen.go
Normal file
37
service/xibao/image_gen.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package xibao
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/fogleman/gg"
|
||||
)
|
||||
|
||||
func GenerateCongratulationImage(text string, outputFile string) {
|
||||
// 加载喜报背景图片
|
||||
im, err := gg.LoadImage("./resource/xibao_background.png") // 需要提前准备的背景图片
|
||||
if err != nil {
|
||||
log.Print("无法加载喜报图片:", err)
|
||||
}
|
||||
|
||||
// 创建与背景图片大小相同的画布
|
||||
width := im.Bounds().Dx()
|
||||
height := im.Bounds().Dy()
|
||||
dc := gg.NewContext(width, height)
|
||||
|
||||
// 将背景图片绘制到画布上
|
||||
dc.DrawImage(im, 0, 0)
|
||||
|
||||
// 设置字体和大小,字体文件需要自备,放在合适的路径
|
||||
if err := dc.LoadFontFace("./resource/font.ttf", 96); err != nil {
|
||||
log.Print("无法加载字体:", err)
|
||||
}
|
||||
|
||||
dc.SetRGB(1, 0, 0)
|
||||
|
||||
dc.DrawStringAnchored(text, float64(width)/2, float64(height)/2, 0.5, 0.5)
|
||||
|
||||
err = dc.SavePNG(outputFile)
|
||||
if err != nil {
|
||||
log.Print("无法保存生成的图片:", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user