From b98866889363bcebddc740b1db7be5fef59b8a89 Mon Sep 17 00:00:00 2001 From: Nova Kwok Date: Tue, 18 Jul 2023 23:17:34 +0800 Subject: [PATCH] Better ReductionEffort (#255) * Better ReductionEffort * Bump version --- config/config.go | 2 +- encoder/encoder.go | 36 +++++++++++++++++------------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/config/config.go b/config/config.go index f73fe8d..5416a8f 100644 --- a/config/config.go +++ b/config/config.go @@ -58,7 +58,7 @@ var ( ProxyMode bool Prefetch bool Config jsonFile - Version = "0.9.5" + Version = "0.9.6" WriteLock = cache.New(5*time.Minute, 10*time.Minute) ) diff --git a/encoder/encoder.go b/encoder/encoder.go index 48e2f48..0eee906 100644 --- a/encoder/encoder.go +++ b/encoder/encoder.go @@ -228,18 +228,9 @@ func webpEncoder(p1, p2 string, extraParams config.ExtraParams) error { 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, - }) - } - } + // Lossless mode will not encount problems as below, because in libvips as code below + // config.method = ExUtilGetInt(argv[++c], 0, &parse_error); + // use_lossless_preset = 0; // disable -z option } else { buf, _, err = img.ExportWebp(&vips.WebpExportParams{ Quality: quality, @@ -250,13 +241,20 @@ func webpEncoder(p1, p2 string, extraParams config.ExtraParams) error { // 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, - }) + log.Warnf("Can't encode source image to WebP with default ReductionEffort") + // Loop through ReductionEffort from 1 to 6 + for i := 1; i <= 6; i++ { + log.Warnf("Retry using ReductionEffort: %d", i) + buf, _, err = img.ExportWebp(&vips.WebpExportParams{ + Quality: quality, + Lossless: false, + StripMetadata: true, + ReductionEffort: i, + }) + if err == nil { + break + } + } } } }