mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-20 14:30:10 +08:00
📝 运势 恢复为传递 base64
This commit is contained in:
parent
a80f8082cc
commit
ccde99e695
@ -23,6 +23,7 @@ import (
|
|||||||
"github.com/FloatTech/zbputils/file"
|
"github.com/FloatTech/zbputils/file"
|
||||||
"github.com/FloatTech/zbputils/math"
|
"github.com/FloatTech/zbputils/math"
|
||||||
"github.com/FloatTech/zbputils/process"
|
"github.com/FloatTech/zbputils/process"
|
||||||
|
"github.com/FloatTech/zbputils/txt2img"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -146,22 +147,30 @@ func init() {
|
|||||||
|
|
||||||
digest := md5.Sum(helper.StringToBytes(zipfile + strconv.Itoa(index) + title + text))
|
digest := md5.Sum(helper.StringToBytes(zipfile + strconv.Itoa(index) + title + text))
|
||||||
cachefile := cache + hex.EncodeToString(digest[:])
|
cachefile := cache + hex.EncodeToString(digest[:])
|
||||||
if file.IsExist(cachefile) {
|
|
||||||
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + cachefile))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
dlmu.Lock()
|
var data []byte
|
||||||
// 绘制背景
|
switch file.IsExist(cachefile) {
|
||||||
err = draw(background, title, text, cachefile)
|
case true:
|
||||||
dlmu.Unlock()
|
data, err = os.ReadFile(cachefile)
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
_ = os.Remove(cachefile)
|
||||||
|
fallthrough
|
||||||
|
case false:
|
||||||
|
dlmu.Lock()
|
||||||
|
// 绘制背景
|
||||||
|
data, err = draw(background, title, text)
|
||||||
|
dlmu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("ERROR: ", err))
|
ctx.SendChain(message.Text("ERROR: ", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
_ = os.WriteFile(cachefile, data, 0644)
|
||||||
// 发送图片
|
// 发送图片
|
||||||
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + cachefile))
|
ctx.SendChain(message.Image("base64://" + helper.BytesToString(data)))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,20 +214,20 @@ func randtext(seed int64) (string, string, error) {
|
|||||||
// @param title 签名
|
// @param title 签名
|
||||||
// @param text 签文
|
// @param text 签文
|
||||||
// @return 错误信息
|
// @return 错误信息
|
||||||
func draw(back image.Image, title, text, cachefile string) error {
|
func draw(back image.Image, title, text string) ([]byte, error) {
|
||||||
canvas := gg.NewContext(back.Bounds().Size().Y, back.Bounds().Size().X)
|
canvas := gg.NewContext(back.Bounds().Size().Y, back.Bounds().Size().X)
|
||||||
canvas.DrawImage(back, 0, 0)
|
canvas.DrawImage(back, 0, 0)
|
||||||
// 写标题
|
// 写标题
|
||||||
canvas.SetRGB(1, 1, 1)
|
canvas.SetRGB(1, 1, 1)
|
||||||
if err := canvas.LoadFontFace(font, 45); err != nil {
|
if err := canvas.LoadFontFace(font, 45); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
sw, _ := canvas.MeasureString(title)
|
sw, _ := canvas.MeasureString(title)
|
||||||
canvas.DrawString(title, 140-sw/2, 112)
|
canvas.DrawString(title, 140-sw/2, 112)
|
||||||
// 写正文
|
// 写正文
|
||||||
canvas.SetRGB(0, 0, 0)
|
canvas.SetRGB(0, 0, 0)
|
||||||
if err := canvas.LoadFontFace(font, 23); err != nil {
|
if err := canvas.LoadFontFace(font, 23); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
tw, th := canvas.MeasureString("测")
|
tw, th := canvas.MeasureString("测")
|
||||||
tw, th = tw+10, th+10
|
tw, th = tw+10, th+10
|
||||||
@ -246,7 +255,7 @@ func draw(back image.Image, title, text, cachefile string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return canvas.SavePNG(cachefile)
|
return txt2img.TxtCanvas{Canvas: canvas}.ToBase64()
|
||||||
}
|
}
|
||||||
|
|
||||||
func offest(total, now int, distance float64) float64 {
|
func offest(total, now int, distance float64) float64 {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user