fix_webp_size.go 1001 B

123456789101112131415161718192021222324252627282930313233
  1. package processing
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/imagedata"
  4. "github.com/imgproxy/imgproxy/v3/imagetype"
  5. "github.com/imgproxy/imgproxy/v3/imath"
  6. "github.com/imgproxy/imgproxy/v3/options"
  7. "github.com/imgproxy/imgproxy/v3/vips"
  8. log "github.com/sirupsen/logrus"
  9. )
  10. // https://chromium.googlesource.com/webm/libwebp/+/refs/heads/master/src/webp/encode.h#529
  11. const webpMaxDimension = 16383.0
  12. func fixWebpSize(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  13. if po.Format != imagetype.WEBP {
  14. return nil
  15. }
  16. webpLimitShrink := float64(imath.Max(img.Width(), img.Height())) / webpMaxDimension
  17. if webpLimitShrink <= 1.0 {
  18. return nil
  19. }
  20. scale := 1.0 / webpLimitShrink
  21. if err := img.Resize(scale, scale); err != nil {
  22. return err
  23. }
  24. log.Warningf("WebP dimension size is limited to %d. The image is rescaled to %dx%d", int(webpMaxDimension), img.Width(), img.Height())
  25. return copyMemoryAndCheckTimeout(pctx.ctx, img)
  26. }