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 ProxyMode bool
Prefetch bool Prefetch bool
Config jsonFile Config jsonFile
Version = "0.9.5" Version = "0.9.6"
WriteLock = cache.New(5*time.Minute, 10*time.Minute) 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, Lossless: true,
StripMetadata: true, StripMetadata: true,
}) })
// If some special images cannot encode with default ReductionEffort(0), then try with 4 // Lossless mode will not encount problems as below, because in libvips as code below
// Example: https://github.com/webp-sh/webp_server_go/issues/234 // config.method = ExUtilGetInt(argv[++c], 0, &parse_error);
if err != nil { // use_lossless_preset = 0; // disable -z option
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 { } else {
buf, _, err = img.ExportWebp(&vips.WebpExportParams{ buf, _, err = img.ExportWebp(&vips.WebpExportParams{
Quality: quality, 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 // Example: https://github.com/webp-sh/webp_server_go/issues/234
if err != nil { if err != nil {
if strings.Contains(err.Error(), "unable to encode") { 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")
buf, _, err = img.ExportWebp(&vips.WebpExportParams{ // Loop through ReductionEffort from 1 to 6
Quality: quality, for i := 1; i <= 6; i++ {
Lossless: false, log.Warnf("Retry using ReductionEffort: %d", i)
StripMetadata: true, buf, _, err = img.ExportWebp(&vips.WebpExportParams{
ReductionEffort: 4, Quality: quality,
}) Lossless: false,
StripMetadata: true,
ReductionEffort: i,
})
if err == nil {
break
}
}
} }
} }
} }