mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-09-28 20:43:54 +02:00
Don't check dimensions for vector formats; Simplify guard scale of vector images
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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
processing/vector_guard_scale.go
Normal file
28
processing/vector_guard_scale.go
Normal file
@@ -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
|
||||
}
|
@@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
var watermarkPipeline = pipeline{
|
||||
vectorGuardScale,
|
||||
prepare,
|
||||
scaleOnLoad,
|
||||
importColorProfile,
|
||||
|
Reference in New Issue
Block a user