Better ReductionEffort (#255)

* Better ReductionEffort

* Bump version
This commit is contained in:
Nova Kwok 2023-07-18 23:17:34 +08:00 committed by GitHub
parent 24b96a901b
commit b988668893
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 20 deletions

View File

@ -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)
)

View File

@ -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")
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: 4,
ReductionEffort: i,
})
if err == nil {
break
}
}
}
}
}