소스 검색

Don't check dimensions for vector formats; Simplify guard scale of vector images

DarthSim 1 개월 전
부모
커밋
e5c72270bb
7개의 변경된 파일47개의 추가작업 그리고 28개의 파일을 삭제
  1. 7 0
      processing/pipeline.go
  2. 0 13
      processing/prepare.go
  3. 6 2
      processing/processing.go
  4. 5 0
      processing/scale_on_load.go
  5. 0 13
      processing/trim.go
  6. 28 0
      processing/vector_guard_scale.go
  7. 1 0
      processing/watermark.go

+ 7 - 0
processing/pipeline.go

@@ -31,6 +31,10 @@ type pipelineContext struct {
 
 	dprScale float64
 
+	// The base scale factor for vector images.
+	// It is used to downscale the input vector image to the maximum allowed resolution
+	vectorBaseScale float64
+
 	// The width we aim to get.
 	// Based on the requested width scaled according to processing options.
 	// Can be 0 if width is not specified in the processing options.
@@ -70,6 +74,9 @@ func (p pipeline) Run(ctx context.Context, img *vips.Image, po *options.Processi
 		wscale: 1.0,
 		hscale: 1.0,
 
+		dprScale:        1.0,
+		vectorBaseScale: 1.0,
+
 		cropGravity: po.Crop.Gravity,
 	}
 

+ 0 - 13
processing/prepare.go

@@ -263,20 +263,7 @@ func prepare(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptio
 	heightToScale := imath.MinNonZero(pctx.cropHeight, pctx.srcHeight)
 
 	pctx.calcScale(widthToScale, heightToScale, po)
-
-	// The size of a vector image is not checked during download, yet it can be very large.
-	// So we should scale it down to the maximum allowed resolution
-	if !pctx.trimmed && imgdata != nil && imgdata.Format().IsVector() && !po.Enlarge {
-		resolution := imath.Round((float64(img.Width()*img.Height()) * pctx.wscale * pctx.hscale))
-		if resolution > po.SecurityOptions.MaxSrcResolution {
-			scale := math.Sqrt(float64(po.SecurityOptions.MaxSrcResolution) / float64(resolution))
-			pctx.wscale *= scale
-			pctx.hscale *= scale
-		}
-	}
-
 	pctx.calcSizes(widthToScale, heightToScale, po)
-
 	pctx.limitScale(widthToScale, heightToScale, po)
 
 	return nil

+ 6 - 2
processing/processing.go

@@ -19,6 +19,7 @@ import (
 )
 
 var mainPipeline = pipeline{
+	vectorGuardScale,
 	trim,
 	prepare,
 	scaleOnLoad,
@@ -298,8 +299,11 @@ func ProcessImage(ctx context.Context, imgdata imagedata.ImageData, po *options.
 	}
 
 	originWidth, originHeight := getImageSize(img)
-	if err := security.CheckDimensions(originWidth, originHeight, 1, po.SecurityOptions); err != nil {
-		return nil, err
+
+	if !imgdata.Format().IsVector() {
+		if err := security.CheckDimensions(originWidth, originHeight, 1, po.SecurityOptions); err != nil {
+			return nil, err
+		}
 	}
 
 	// Let's check if we should skip standard processing

+ 5 - 0
processing/scale_on_load.go

@@ -51,6 +51,11 @@ func scaleOnLoad(pctx *pipelineContext, img *vips.Image, po *options.ProcessingO
 	preshrink := math.Min(wshrink, hshrink)
 	prescale := 1.0 / preshrink
 
+	if imgdata != nil && imgdata.Format().IsVector() {
+		// For vector images, apply the vector base scale
+		prescale *= pctx.vectorBaseScale
+	}
+
 	if !canScaleOnLoad(pctx, imgdata, prescale) {
 		return nil
 	}

+ 0 - 13
processing/trim.go

@@ -1,8 +1,6 @@
 package processing
 
 import (
-	"math"
-
 	"github.com/imgproxy/imgproxy/v3/imagedata"
 	"github.com/imgproxy/imgproxy/v3/options"
 	"github.com/imgproxy/imgproxy/v3/vips"
@@ -13,17 +11,6 @@ func trim(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions,
 		return nil
 	}
 
-	// The size of a vector image is not checked during download, yet it can be very large.
-	// So we should scale it down to the maximum allowed resolution
-	if imgdata != nil && imgdata.Format().IsVector() {
-		if resolution := img.Width() * img.Height(); resolution > po.SecurityOptions.MaxSrcResolution {
-			scale := math.Sqrt(float64(po.SecurityOptions.MaxSrcResolution) / float64(resolution))
-			if err := img.Load(imgdata, 1, scale, 1); err != nil {
-				return err
-			}
-		}
-	}
-
 	// We need to import color profile before trim
 	if err := importColorProfile(pctx, img, po, imgdata); err != nil {
 		return err

+ 28 - 0
processing/vector_guard_scale.go

@@ -0,0 +1,28 @@
+package processing
+
+import (
+	"math"
+
+	"github.com/imgproxy/imgproxy/v3/imagedata"
+	"github.com/imgproxy/imgproxy/v3/options"
+	"github.com/imgproxy/imgproxy/v3/vips"
+)
+
+// vectorGuardScale checks if the image is a vector format and downscales it
+// to the maximum allowed resolution if necessary
+func vectorGuardScale(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata imagedata.ImageData) error {
+	if imgdata == nil || !imgdata.Format().IsVector() {
+		return nil
+	}
+
+	if resolution := img.Width() * img.Height(); resolution > po.SecurityOptions.MaxSrcResolution {
+		scale := math.Sqrt(float64(po.SecurityOptions.MaxSrcResolution) / float64(resolution))
+		pctx.vectorBaseScale = scale
+
+		if err := img.Load(imgdata, 1, scale, 1); err != nil {
+			return err
+		}
+	}
+
+	return nil
+}

+ 1 - 0
processing/watermark.go

@@ -12,6 +12,7 @@ import (
 )
 
 var watermarkPipeline = pipeline{
+	vectorGuardScale,
 	prepare,
 	scaleOnLoad,
 	importColorProfile,