Files
dygou_go/image.go
T
2026-06-19 12:10:55 +08:00

83 lines
1.9 KiB
Go

package main
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
"path/filepath"
"time"
"github.com/golang/freetype"
)
// saveSuccessImage 保存成功兑换结果图片。
func (s *appState) saveSuccessImage(account Account, product Product, roleName string) {
s.fontMu.Lock()
font := s.font
s.fontMu.Unlock()
if font == nil {
return
}
productName := product.CommodityName
if productName == "" {
productName = product.CommodityID
}
width, height := 600, 280
img := image.NewRGBA(image.Rect(0, 0, width, height))
bgColor := color.RGBA{213, 239, 253, 255}
topColor := color.RGBA{76, 175, 80, 255}
white := color.RGBA{255, 255, 255, 255}
black := color.RGBA{0, 0, 0, 255}
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
if y < 60 {
img.Set(x, y, topColor)
} else {
img.Set(x, y, bgColor)
}
}
}
ctx := freetype.NewContext()
ctx.SetDPI(72)
ctx.SetFont(font)
ctx.SetClip(img.Bounds())
ctx.SetDst(img)
lines := []struct {
Text string
Size float64
X, Y int
Color color.Color
}{
{productName, 20, 25, 42, white},
{"兑换时间:" + time.Now().In(cst).Format("2006年01月02日 15:04:05"), 16, 25, 110, black},
{"游戏昵称:" + emptyDefault(roleName, "未知"), 16, 25, 150, black},
{"兑换账号:" + account.Name, 16, 25, 190, black},
}
for _, line := range lines {
ctx.SetFontSize(line.Size)
ctx.SetSrc(image.NewUniform(line.Color))
_, _ = ctx.DrawString(line.Text, freetype.Pt(line.X, line.Y))
}
name := safeFilename(fmt.Sprintf("%s_%s_%d.png", account.Name, productName, time.Now().Unix()))
path := filepath.Join(s.paths.SuccessDir, name)
file, err := os.Create(path)
if err != nil {
logEvent("图片", "error", "成功图片创建失败: %v", err)
return
}
defer file.Close()
if err := png.Encode(file, img); err != nil {
logEvent("图片", "error", "成功图片保存失败: %v", err)
return
}
logEvent("图片", "success", "成功图片已保存: %s", path)
}