diff --git a/config.go b/config.go index 1b4ef845..a61027e4 100644 --- a/config.go +++ b/config.go @@ -152,6 +152,8 @@ type config struct { EnforceWebp bool EnableClientHints bool + DisableShrinkOnLoad bool + Keys []securityKey Salts []securityKey AllowInsecure bool @@ -267,6 +269,8 @@ func init() { boolEnvConfig(&conf.EnforceWebp, "IMGPROXY_ENFORCE_WEBP") boolEnvConfig(&conf.EnableClientHints, "IMGPROXY_ENABLE_CLIENT_HINTS") + boolEnvConfig(&conf.DisableShrinkOnLoad, "IMGPROXY_DISABLE_SHRINK_ON_LOAD") + hexEnvConfig(&conf.Keys, "IMGPROXY_KEY") hexEnvConfig(&conf.Salts, "IMGPROXY_SALT") intEnvConfig(&conf.SignatureSize, "IMGPROXY_SIGNATURE_SIZE") diff --git a/docs/configuration.md b/docs/configuration.md index b8086688..42ddd220 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -200,3 +200,4 @@ imgproxy can send logs to syslog, but this feature is disabled by default. To en ### Miscellaneous * `IMGPROXY_BASE_URL`: base URL prefix that will be added to every requested image URL. For example, if the base URL is `http://example.com/images` and `/path/to/image.png` is requested, imgproxy will download the source image from `http://example.com/images/path/to/image.png`. Default: blank. +* `IMGPROXY_DISABLE_SHRINK_ON_LOAD`: when `true`, disables shrink-on-load for JPEG and WebP. Allows to process the whole image in linear colorspace but dramatically slows down resizing and increases memory usage when working with large images. diff --git a/process.go b/process.go index 22b61b6b..9cdb15f5 100644 --- a/process.go +++ b/process.go @@ -284,8 +284,8 @@ func transformImage(ctx context.Context, img **C.VipsImage, data []byte, po *pro scale := calcScale(imgWidth, imgHeight, po, imgtype) - if scale != 1 { - if imgtype == imageTypeSVG && data != nil { + if scale != 1 && data != nil { + if imgtype == imageTypeSVG { // Load SVG with desired scale if tmp, err := vipsLoadImage(data, imgtype, 1, scale, false); err == nil { C.swap_and_clear(img, tmp) @@ -294,18 +294,16 @@ func transformImage(ctx context.Context, img **C.VipsImage, data []byte, po *pro } scale = 1 - } else { + } else if !conf.DisableShrinkOnLoad && scale < 1.0 { // Do some shrink-on-load - if scale < 1.0 && data != nil { - if shrink := calcShink(scale, imgtype); shrink != 1 { - if tmp, err := vipsLoadImage(data, imgtype, shrink, 1.0, false); err == nil { - C.swap_and_clear(img, tmp) - } else { - return err - } - - scale = scale * float64(shrink) + if shrink := calcShink(scale, imgtype); shrink != 1 { + if tmp, err := vipsLoadImage(data, imgtype, shrink, 1.0, false); err == nil { + C.swap_and_clear(img, tmp) + } else { + return err } + + scale = scale * float64(shrink) } } }