refactor: 优化 LastDiffToImage 函数,动态调整背景图大小以适应头像和文本,提升图像生成效果
This commit is contained in:
parent
74b92f675b
commit
761dde3693
@ -6,6 +6,7 @@ import (
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
@ -542,14 +543,7 @@ func (p PlayerDataLite) LastDiffToImage(lastDayQueryData PlayerDataLite) string
|
||||
// }
|
||||
|
||||
baseboard := sprite.NewNamedSpriteBoard()
|
||||
background := image.NewRGBA(image.Rect(0, 0, 500, 1000))
|
||||
draw.Draw(background, background.Bounds(), image.White, image.Point{}, draw.Src)
|
||||
backgroundSpirit := sprite.Sprite{
|
||||
Name: "background",
|
||||
Image: background,
|
||||
Index: 0,
|
||||
}
|
||||
baseboard.AddSprite(&backgroundSpirit)
|
||||
|
||||
avatar, err := util.ResizeImageByMaxHeight2Image(filePath, 20)
|
||||
if err != nil {
|
||||
log.Default().Printf("缩放头像失败,url:%s,err:%v", p.Avatar, err)
|
||||
@ -621,10 +615,22 @@ func (p PlayerDataLite) LastDiffToImage(lastDayQueryData PlayerDataLite) string
|
||||
Name: "text",
|
||||
Image: textImg,
|
||||
Index: 2,
|
||||
Position: image.Point{X: 23, Y: 23},
|
||||
Position: image.Point{X: 23, Y: 0},
|
||||
}
|
||||
baseboard.AddSprite(&textSpirit)
|
||||
|
||||
totalWidth := textImg.Bounds().Dx() + avatar.Bounds().Dx()
|
||||
totalHeight := math.Max(float64(textImg.Bounds().Dy()), float64(avatar.Bounds().Dy()))
|
||||
|
||||
background := image.NewRGBA(image.Rect(0, 0, int(totalWidth), int(totalHeight)))
|
||||
draw.Draw(background, background.Bounds(), image.White, image.Point{}, draw.Src)
|
||||
backgroundSpirit := sprite.Sprite{
|
||||
Name: "background",
|
||||
Image: background,
|
||||
Index: 0,
|
||||
}
|
||||
baseboard.AddSprite(&backgroundSpirit)
|
||||
|
||||
if err := baseboard.SaveToPng(util.GenTempFilePath("cbl.png")); err != nil {
|
||||
log.Default().Printf("保存图片失败,err:%v", err)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user