From 24b96a901b4d3cd34911d9f0f8ca64efd19cf86c Mon Sep 17 00:00:00 2001 From: Nova Kwok Date: Tue, 18 Jul 2023 16:16:14 +0800 Subject: [PATCH] ReductionEffort to 0 (#254) --- config/config.go | 2 +- encoder/encoder.go | 38 +++++++++++++++++++++++++++++++------- webp-server.go | 6 +++--- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index 69a5299..f73fe8d 100644 --- a/config/config.go +++ b/config/config.go @@ -58,7 +58,7 @@ var ( ProxyMode bool Prefetch bool Config jsonFile - Version = "0.9.4" + Version = "0.9.5" WriteLock = cache.New(5*time.Minute, 10*time.Minute) ) diff --git a/encoder/encoder.go b/encoder/encoder.go index fcae293..48e2f48 100644 --- a/encoder/encoder.go +++ b/encoder/encoder.go @@ -5,6 +5,7 @@ import ( "os" "path" "runtime" + "strings" "sync" "webp_server_go/config" "webp_server_go/helper" @@ -224,17 +225,40 @@ func webpEncoder(p1, p2 string, extraParams config.ExtraParams) error { // If quality >= 100, we use lossless mode if quality >= 100 { buf, _, err = img.ExportWebp(&vips.WebpExportParams{ - Lossless: true, - StripMetadata: true, - ReductionEffort: 4, + Lossless: true, + StripMetadata: true, }) + // If some special images cannot encode with default ReductionEffort(0), then try with 4 + // Example: https://github.com/webp-sh/webp_server_go/issues/234 + if err != nil { + if strings.Contains(err.Error(), "unable to encode") { + log.Warnf("Can't encode source image to WebP with default ReductionEffort, retry using ReductionEffort:4") + buf, _, err = img.ExportWebp(&vips.WebpExportParams{ + Lossless: true, + StripMetadata: true, + ReductionEffort: 4, + }) + } + } } else { buf, _, err = img.ExportWebp(&vips.WebpExportParams{ - Quality: quality, - Lossless: false, - StripMetadata: true, - ReductionEffort: 4, + Quality: quality, + Lossless: false, + StripMetadata: true, }) + // If some special images cannot encode with default ReductionEffort(0), then try with 4 + // Example: https://github.com/webp-sh/webp_server_go/issues/234 + if err != nil { + if strings.Contains(err.Error(), "unable to encode") { + log.Warnf("Can't encode source image to WebP with default ReductionEffort, retry using ReductionEffort:4") + buf, _, err = img.ExportWebp(&vips.WebpExportParams{ + Quality: quality, + Lossless: false, + StripMetadata: true, + ReductionEffort: 4, + }) + } + } } if err != nil { diff --git a/webp-server.go b/webp-server.go index 72a42b9..16a9293 100644 --- a/webp-server.go +++ b/webp-server.go @@ -17,8 +17,8 @@ import ( ) var app = fiber.New(fiber.Config{ - ServerHeader: "Webp Server Go", - AppName: "Webp Server Go", + ServerHeader: "WebP Server Go", + AppName: "WebP Server Go", DisableStartupMessage: true, ProxyHeader: "X-Real-IP", }) @@ -43,7 +43,7 @@ func setupLogger() { TimeFormat: config.TimeDateFormat, })) app.Use(recover.New(recover.Config{})) - log.Infoln("fiber ready.") + log.Infoln("WebP Server Go ready.") } func init() {