feat: 喜报不要求保证同一条消息

This commit is contained in:
lixiangwuxian
2024-11-01 22:35:39 +08:00
parent 74cb2d19ca
commit 71b3fe8bbf
4 changed files with 71 additions and 11 deletions

View File

@@ -1,14 +1,15 @@
package xibao
import (
"fmt"
"log"
"os"
"strings"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/util"
"github.com/fogleman/gg"
"github.com/google/uuid"
"golang.org/x/image/font/opentype"
)
func GenerateCongratulationImage(text string, inputFile, outputFile string, isGood bool) {
@@ -51,8 +52,8 @@ func GenerateCongratulationImage(text string, inputFile, outputFile string, isGo
scaleFactor := 1.0
// 根据高度比例进行缩放
if float64(heightC) > float64(height)/1.5 {
scaleFactor = (float64(height) / 1.5) / float64(heightC)
if float64(heightC) > float64(height)/1.8 {
scaleFactor = (float64(height) / 1.8) / float64(heightC)
}
// 根据宽度比例进行缩放
@@ -64,10 +65,10 @@ func GenerateCongratulationImage(text string, inputFile, outputFile string, isGo
if heightC < height/5 && widthC < width/4 {
if float64(widthC)*(float64(height)/5)/float64(heightC) > float64(width/4) {
scaleFactor = (float64(height) / 5) / float64(heightC)
log.Print(fmt.Sprintf("图片高度过小,已缩放至高度的 %f 倍", scaleFactor))
// log.Print(fmt.Sprintf("图片高度过小,已缩放至高度的 %f 倍", scaleFactor))
} else {
scaleFactor = (float64(width) / 4) / float64(widthC)
log.Print(fmt.Sprintf("图片宽度过小,已缩放至宽度的 %f 倍", scaleFactor))
// log.Print(fmt.Sprintf("图片宽度过小,已缩放至宽度的 %f 倍", scaleFactor))
}
}
@@ -90,10 +91,43 @@ func GenerateCongratulationImage(text string, inputFile, outputFile string, isGo
}
// 设置字体和大小,字体文件需要自备,放在合适的路径
// if err := dc.LoadFontFace("./resource/emoji.ttf", 96); err != nil {
// log.Print("无法加载字体:", err)
// return
// }
fontBytes, err := os.ReadFile("./resource/emoji.ttf")
if err != nil {
log.Print("failed to load font: %v", err)
}
// 解析字体
font, err := opentype.Parse(fontBytes)
if err != nil {
log.Print("failed to parse font: %v", err)
}
// 设置字体大小和 DPI
const fontSize = 48
const dpi = 72
// 创建 font.Face用于渲染文本
face, err := opentype.NewFace(font, &opentype.FaceOptions{
Size: fontSize,
DPI: dpi,
Hinting: 0,
})
if err != nil {
log.Print("failed to create font face: %v", err)
return
}
dc.SetFontFace(face)
if err := dc.LoadFontFace("./resource/font.ttf", 96); err != nil {
log.Print("无法加载字体:", err)
return
}
if isGood {
// 设置文本颜色为红色
dc.SetRGB(0.8, 0, 0)