mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-19 14:12:01 +08:00
feat(encoder): add logging and support for AVIF and JXL image formats
This commit is contained in:
parent
b0e866f9dc
commit
059aba8a4d
@ -199,6 +199,8 @@ func preProcessImage(img *vips.ImageRef, imageType string, extraParams config.Ex
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ProcessAndSaveImage(rawImageAbs, exhaustFilename string, extraParams config.ExtraParams) error {
|
func ProcessAndSaveImage(rawImageAbs, exhaustFilename string, extraParams config.ExtraParams) error {
|
||||||
|
log.Infof("开始处理图像: 源文件=%s, 目标文件=%s", rawImageAbs, exhaustFilename)
|
||||||
|
|
||||||
// 创建目标目录
|
// 创建目标目录
|
||||||
if err := os.MkdirAll(path.Dir(exhaustFilename), 0755); err != nil {
|
if err := os.MkdirAll(path.Dir(exhaustFilename), 0755); err != nil {
|
||||||
log.Errorf("创建目标目录失败: %v", err)
|
log.Errorf("创建目标目录失败: %v", err)
|
||||||
@ -227,22 +229,29 @@ func ProcessAndSaveImage(rawImageAbs, exhaustFilename string, extraParams config
|
|||||||
img.RemoveMetadata()
|
img.RemoveMetadata()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var buf []byte
|
||||||
|
var exportErr error
|
||||||
|
|
||||||
// 确定输出格式
|
// 确定输出格式
|
||||||
outputFormat := vips.ImageTypeWebP // 默认使用 WebP
|
|
||||||
if strings.HasSuffix(exhaustFilename, ".avif") {
|
if strings.HasSuffix(exhaustFilename, ".avif") {
|
||||||
outputFormat = vips.ImageTypeAVIF
|
buf, _, exportErr = img.ExportAvif(vips.AvifExportParams{
|
||||||
|
Quality: config.Config.Quality,
|
||||||
|
})
|
||||||
} else if strings.HasSuffix(exhaustFilename, ".jxl") {
|
} else if strings.HasSuffix(exhaustFilename, ".jxl") {
|
||||||
outputFormat = vips.ImageTypeJPEG // 假设 JXL 不直接支持,使用 JPEG
|
// 注意:govips 可能不直接支持 JXL 导出,这里使用 JPEG 作为替代
|
||||||
|
buf, _, exportErr = img.ExportJpeg(vips.JpegExportParams{
|
||||||
|
Quality: config.Config.Quality,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// 默认使用 WebP
|
||||||
|
buf, _, exportErr = img.ExportWebp(vips.WebpExportParams{
|
||||||
|
Quality: config.Config.Quality,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 导出图像
|
if exportErr != nil {
|
||||||
buf, _, err := img.Export(vips.ExportParams{
|
log.Errorf("导出图像失败: %v", exportErr)
|
||||||
Format: outputFormat,
|
return exportErr
|
||||||
Quality: config.Config.Quality,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("导出图像失败: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入文件
|
// 写入文件
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
schedule "webp_server_go/schedule"
|
schedule "webp_server_go/schedule"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/etag"
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@ -103,9 +102,6 @@ func main() {
|
|||||||
if config.Prefetch {
|
if config.Prefetch {
|
||||||
go encoder.PrefetchImages()
|
go encoder.PrefetchImages()
|
||||||
}
|
}
|
||||||
app.Use(etag.New(etag.Config{
|
|
||||||
Weak: true,
|
|
||||||
}))
|
|
||||||
|
|
||||||
listenAddress := config.Config.Host + ":" + config.Config.Port
|
listenAddress := config.Config.Host + ":" + config.Config.Port
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user