Browse Source

Fix HEIC/AVIF dimension limit handling

DarthSim 1 year ago
parent
commit
7fd1d446cf
2 changed files with 25 additions and 1 deletions
  1. 5 1
      CHANGELOG.md
  2. 20 0
      processing/fix_size.go

+ 5 - 1
CHANGELOG.md

@@ -1,7 +1,11 @@
 # Changelog
 
+## [Unreleased]
+### Fix
+- Fix HEIC/AVIF dimension limit handling.
+
 ## [3.24.1] - 2024-04-30
-## Fix
+### Fix
 - Fix the default `IMGPROXY_WORKERS` value when cgroup limits are applied.
 
 ## [3.24.0] - 2024-04-29

+ 20 - 0
processing/fix_size.go

@@ -14,6 +14,7 @@ import (
 const (
 	// https://chromium.googlesource.com/webm/libwebp/+/refs/heads/master/src/webp/encode.h#529
 	webpMaxDimension = 16383.0
+	heifMaxDimension = 16384.0
 	gifMaxDimension  = 65535.0
 	icoMaxDimension  = 256.0
 )
@@ -35,6 +36,23 @@ func fixWebpSize(img *vips.Image) error {
 	return nil
 }
 
+func fixHeifSize(img *vips.Image) error {
+	heifLimitShrink := float64(imath.Max(img.Width(), img.Height())) / heifMaxDimension
+
+	if heifLimitShrink <= 1.0 {
+		return nil
+	}
+
+	scale := 1.0 / heifLimitShrink
+	if err := img.Resize(scale, scale); err != nil {
+		return err
+	}
+
+	log.Warningf("AVIF/HEIC dimension size is limited to %d. The image is rescaled to %dx%d", int(heifMaxDimension), img.Width(), img.Height())
+
+	return nil
+}
+
 func fixGifSize(img *vips.Image) error {
 	gifMaxResolution := float64(vips.GifResolutionLimit())
 	gifResLimitShrink := float64(img.Width()*img.Height()) / gifMaxResolution
@@ -77,6 +95,8 @@ func fixSize(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptio
 	switch po.Format {
 	case imagetype.WEBP:
 		return fixWebpSize(img)
+	case imagetype.AVIF, imagetype.HEIC:
+		return fixHeifSize(img)
 	case imagetype.GIF:
 		return fixGifSize(img)
 	case imagetype.ICO: