1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00

feat: add webp image convert function

This commit is contained in:
Maiko Tan 2023-01-05 21:58:37 +08:00
parent 4061904945
commit 2a4ea28f4d
No known key found for this signature in database
GPG Key ID: 0F3B49C721E5F453
5 changed files with 25 additions and 1 deletions

View File

@ -153,9 +153,23 @@ func (bot *CQBot) uploadLocalImage(target message.Source, img *LocalImageElement
defer func() { _ = f.Close() }()
img.Stream = f
}
if mt, ok := mime.CheckImage(img.Stream); !ok {
mt, ok := mime.CheckImage(img.Stream)
if !ok {
return nil, errors.New("image type error: " + mt)
}
if mt == "image/webp" && base.ConvertWebpImage != false {
newname := img.File + ".png"
err := global.ConvertImagePng(img.File, newname)
if err != nil {
return nil, errors.Wrap(err, "convert webp image error")
}
f, err := os.Open(newname)
if err != nil {
return nil, errors.Wrap(err, "open image error")
}
defer func() { _ = f.Close() }()
img.Stream = f
}
i, err := bot.Client.UploadImage(target, img.Stream, 4)
if err != nil {
return nil, err

View File

@ -46,3 +46,8 @@ func ExtractCover(src string, target string) error {
cmd := exec.Command("ffmpeg", "-i", src, "-y", "-ss", "0", "-frames:v", "1", target)
return errors.Wrap(cmd.Run(), "extract video cover failed")
}
func ConvertImagePng(src string, target string) error {
cmd := exec.Command("ffmpeg", "-i", src, "-y", "-f", "png", target)
return errors.Wrap(cmd.Run(), "convert image to png failed")
}

View File

@ -30,6 +30,7 @@ var (
SplitURL bool // 是否分割URL
ForceFragmented bool // 是否启用强制分片
SkipMimeScan bool // 是否跳过Mime扫描
ConvertWebpImage bool // 是否转换Webp图片
ReportSelfMessage bool // 是否上报自身消息
UseSSOAddress bool // 是否使用服务器下发的新地址进行重连
LogForceNew bool // 是否在每次启动时强制创建全新的文件储存日志
@ -79,6 +80,7 @@ func Init() {
ExtraReplyData = conf.Message.ExtraReplyData
ForceFragmented = conf.Message.ForceFragment
SkipMimeScan = conf.Message.SkipMimeScan
ConvertWebpImage = conf.Message.ConvertWebpImage
ReportSelfMessage = conf.Message.ReportSelfMessage
UseSSOAddress = conf.Account.UseSSOAddress
AllowTempSession = conf.Account.AllowTempSession

View File

@ -55,6 +55,7 @@ type Config struct {
RemoveReplyAt bool `yaml:"remove-reply-at"`
ExtraReplyData bool `yaml:"extra-reply-data"`
SkipMimeScan bool `yaml:"skip-mime-scan"`
ConvertWebpImage bool `yaml:"convert-webp-image"`
} `yaml:"message"`
Output struct {

View File

@ -43,6 +43,8 @@ message:
extra-reply-data: false
# 跳过 Mime 扫描, 忽略错误数据
skip-mime-scan: false
# 是否自动转换 WebP 图片
convert-webp-image: false
output:
# 日志等级 trace,debug,info,warn,error