feat: 使用自制绘图库替换gg库

This commit is contained in:
lixiangwuxian 2025-05-15 10:51:08 +08:00
parent b24066d50b
commit 444be993c3
3 changed files with 36 additions and 8 deletions

4
go.sum
View File

@ -1,9 +1,5 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
git.lxtend.com/lixiangwuxian/imagedd v0.0.0-20250513115110-2cf9da089612 h1:ew7YcheoePX1rQ9XQxqvfWLkAdGyCVD3h8LRjO5i/dQ=
git.lxtend.com/lixiangwuxian/imagedd v0.0.0-20250513115110-2cf9da089612/go.mod h1:luas4p32Wtsywcz+8HsxIB3gf65FDDBa+3XYhm0S2b8=
git.lxtend.com/lixiangwuxian/imagedd v0.0.0-20250513153039-c482f721b1ae h1:uFuLriBS+ciaUanGbe3FV8acxlwVvV1CA+zkn7292i0=
git.lxtend.com/lixiangwuxian/imagedd v0.0.0-20250513153039-c482f721b1ae/go.mod h1:luas4p32Wtsywcz+8HsxIB3gf65FDDBa+3XYhm0S2b8=
git.lxtend.com/lixiangwuxian/imagedd v0.0.0-20250513160811-82e354862815 h1:GyM0BUsJOk+1Jx1IGDKkkuSTrg+3KUi6/Qi4qnUnqAg=
git.lxtend.com/lixiangwuxian/imagedd v0.0.0-20250513160811-82e354862815/go.mod h1:+G/BR3iv5Yw0bIqZTRcBxpXwcv3bIso+XhN0MTfnjCY=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=

View File

@ -33,7 +33,7 @@ func xiBao(msg model.Message) (reply *model.Reply) {
FromMsg: msg,
}
}
xibao.GenerateCongratulationImage(tokens[1], "./resource/xibao_background.png", filePath, true)
xibao.GenerateCongratulationImageNew(tokens[1], "./resource/xibao_background.png", filePath, true)
imageMsg := message.ImageMessage{
Type: "image",
Data: message.ImageMessageData{
@ -51,7 +51,7 @@ func xiBaoTemp(msg model.Message) (reply *model.Reply, isTrigger bool) {
handler.UnRegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId)
fileName := uuid.New().String()
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
xibao.GenerateCongratulationImage(msg.RawMsg, "./resource/xibao_background.png", filePath, true)
xibao.GenerateCongratulationImageNew(msg.RawMsg, "./resource/xibao_background.png", filePath, true)
imageMsg := message.ImageMessage{
Type: "image",
Data: message.ImageMessageData{
@ -78,7 +78,7 @@ func beiBao(msg model.Message) (reply *model.Reply) {
FromMsg: msg,
}
}
xibao.GenerateCongratulationImage(tokens[1], "./resource/beibao_background.png", filePath, false)
xibao.GenerateCongratulationImageNew(tokens[1], "./resource/beibao_background.png", filePath, false)
imageMsg := message.ImageMessage{
Type: "image",
Data: message.ImageMessageData{
@ -96,7 +96,7 @@ func beiBaoTemp(msg model.Message) (reply *model.Reply, isTrigger bool) {
handler.UnRegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId)
fileName := uuid.New().String()
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
xibao.GenerateCongratulationImage(msg.RawMsg, "./resource/beibao_background.png", filePath, false)
xibao.GenerateCongratulationImageNew(msg.RawMsg, "./resource/beibao_background.png", filePath, false)
imageMsg := message.ImageMessage{
Type: "image",
Data: message.ImageMessageData{

View File

@ -1,10 +1,14 @@
package xibao
import (
"image"
"image/color"
"log"
"os"
"strings"
"git.lxtend.com/lixiangwuxian/imagedd/sprite"
"git.lxtend.com/lixiangwuxian/imagedd/text2img"
"git.lxtend.com/qqbot/message"
"git.lxtend.com/qqbot/util"
"github.com/fogleman/gg"
@ -153,6 +157,34 @@ func GenerateCongratulationImage(text string, inputFile, outputFile string, isGo
}
}
func GenerateCongratulationImageNew(text string, inputFile, outputFile string, isGood bool) {
baseboard := sprite.NewNamedSpriteBoard()
backgroundImageSprite, err := sprite.LoadSpriteFromFile("bg", inputFile)
if err != nil {
log.Print("无法加载背景图片:", err)
return
}
baseboard.AddSprite(backgroundImageSprite)
var textColor color.RGBA
if isGood {
// 设置文本颜色为红色
textColor = color.RGBA{R: 255 * 0.8, G: 0, B: 0, A: 255}
} else {
textColor = color.RGBA{R: 0, G: 0, B: 255 * 0.8, A: 255}
}
textImage, err := text2img.RenderTextToTrimmedImage(nil, text, 48, textColor, 0, 0)
if err != nil {
log.Print("无法渲染文本:", err)
return
}
textSprite := sprite.Sprite{
Images: []image.Image{textImage},
Position: image.Point{X: backgroundImageSprite.GetCurrentImage().Bounds().Dx()/2 - textImage.Bounds().Dx()/2, Y: backgroundImageSprite.GetCurrentImage().Bounds().Dy()/2 - textImage.Bounds().Dy()/2},
}
baseboard.AddSprite(&textSprite)
baseboard.SaveToApng(outputFile)
}
func isImageCQ(text string) (string, bool) {
imgMsg := message.ImageMessage{}
if err := imgMsg.ParseMessage(text); err == nil {