mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-09 19:52:30 +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
|
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.
|
// The width we aim to get.
|
||||||
// Based on the requested width scaled according to processing options.
|
// Based on the requested width scaled according to processing options.
|
||||||
// Can be 0 if width is not specified in the 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,
|
wscale: 1.0,
|
||||||
hscale: 1.0,
|
hscale: 1.0,
|
||||||
|
|
||||||
|
dprScale: 1.0,
|
||||||
|
vectorBaseScale: 1.0,
|
||||||
|
|
||||||
cropGravity: po.Crop.Gravity,
|
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)
|
heightToScale := imath.MinNonZero(pctx.cropHeight, pctx.srcHeight)
|
||||||
|
|
||||||
pctx.calcScale(widthToScale, heightToScale, po)
|
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.calcSizes(widthToScale, heightToScale, po)
|
||||||
|
|
||||||
pctx.limitScale(widthToScale, heightToScale, po)
|
pctx.limitScale(widthToScale, heightToScale, po)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@@ -19,6 +19,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var mainPipeline = pipeline{
|
var mainPipeline = pipeline{
|
||||||
|
vectorGuardScale,
|
||||||
trim,
|
trim,
|
||||||
prepare,
|
prepare,
|
||||||
scaleOnLoad,
|
scaleOnLoad,
|
||||||
@@ -298,8 +299,11 @@ func ProcessImage(ctx context.Context, imgdata imagedata.ImageData, po *options.
|
|||||||
}
|
}
|
||||||
|
|
||||||
originWidth, originHeight := getImageSize(img)
|
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
|
// 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)
|
preshrink := math.Min(wshrink, hshrink)
|
||||||
prescale := 1.0 / preshrink
|
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) {
|
if !canScaleOnLoad(pctx, imgdata, prescale) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
package processing
|
package processing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
|
||||||
|
|
||||||
"github.com/imgproxy/imgproxy/v3/imagedata"
|
"github.com/imgproxy/imgproxy/v3/imagedata"
|
||||||
"github.com/imgproxy/imgproxy/v3/options"
|
"github.com/imgproxy/imgproxy/v3/options"
|
||||||
"github.com/imgproxy/imgproxy/v3/vips"
|
"github.com/imgproxy/imgproxy/v3/vips"
|
||||||
@@ -13,17 +11,6 @@ func trim(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions,
|
|||||||
return nil
|
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
|
// We need to import color profile before trim
|
||||||
if err := importColorProfile(pctx, img, po, imgdata); err != nil {
|
if err := importColorProfile(pctx, img, po, imgdata); err != nil {
|
||||||
return err
|
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{
|
var watermarkPipeline = pipeline{
|
||||||
|
vectorGuardScale,
|
||||||
prepare,
|
prepare,
|
||||||
scaleOnLoad,
|
scaleOnLoad,
|
||||||
importColorProfile,
|
importColorProfile,
|
||||||
|
Reference in New Issue
Block a user