Kaynağa Gözat

Fix WebP dimension limit handling.

DarthSim 4 yıl önce
ebeveyn
işleme
9ad8bb1403
2 değiştirilmiş dosya ile 17 ekleme ve 11 silme
  1. 2 1
      CHANGELOG.md
  2. 15 10
      process.go

+ 2 - 1
CHANGELOG.md

@@ -11,7 +11,8 @@
 - Speed up generation of video thumbnails with large timestamps.
 
 ### Fix
-- Fix `padding` and `extend` behaior when converting from a fromat without alpha support to one with alpha support
+- Fix `padding` and `extend` behaior when converting from a fromat without alpha support to one with alpha support.
+- Fix WebP dimension limit handling.
 - (pro) Fix thumbnails generation of some videos.
 
 ## [2.14.1] - 2020-07-22

+ 15 - 10
process.go

@@ -418,6 +418,21 @@ func transformImage(ctx context.Context, img *vipsImage, data []byte, po *proces
 		return err
 	}
 
+	if po.Format == imageTypeWEBP {
+		webpLimitShrink := float64(maxInt(img.Width(), img.Height())) / webpMaxDimension
+
+		if webpLimitShrink > 1.0 {
+			if err = img.Resize(1.0/webpLimitShrink, hasAlpha); err != nil {
+				return err
+			}
+			logWarning("WebP dimension size is limited to %d. The image is rescaled to %dx%d", int(webpMaxDimension), img.Width(), img.Height())
+		}
+
+		if err = img.CopyMemory(); err != nil {
+			return err
+		}
+	}
+
 	checkTimeout(ctx)
 
 	if !iccImported {
@@ -729,16 +744,6 @@ func processImage(ctx context.Context) ([]byte, context.CancelFunc, error) {
 		po.Width, po.Height = 0, 0
 	}
 
-	if po.Format == imageTypeWEBP {
-		webpLimitShrink := float64(maxInt(po.Width, po.Height)) * po.Dpr / webpMaxDimension
-
-		if webpLimitShrink > 1.0 {
-			po.Width = int(float64(po.Width) / webpLimitShrink)
-			po.Height = int(float64(po.Height) / webpLimitShrink)
-			logWarning("WebP dimension size is limited to %d. Requested dimensions are rescaled to %dx%d", int(webpMaxDimension), po.Width, po.Height)
-		}
-	}
-
 	animationSupport := conf.MaxAnimationFrames > 1 && vipsSupportAnimation(imgdata.Type) && vipsSupportAnimation(po.Format)
 
 	pages := 1