Skip to content
This repository was archived by the owner on Feb 14, 2024. It is now read-only.

Commit 0e5216a

Browse files
authored
feat: crop max with and height (#13)
1 parent 8bb798a commit 0e5216a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ var (
3939
MaxAnimationFrames int
4040
MaxSvgCheckBytes int
4141
MaxRedirects int
42+
CropMaxWidth int
43+
CropMaxHeight int
4244

4345
JpegProgressive bool
4446
PngInterlaced bool
@@ -383,6 +385,9 @@ func Configure() error {
383385

384386
configurators.Int(&MaxRedirects, "IMGPROXY_MAX_REDIRECTS")
385387

388+
configurators.Int(&CropMaxWidth, "IMGPROXY_CROP_MAX_WIDTH")
389+
configurators.Int(&CropMaxHeight, "IMGPROXY_CROP_MAX_HEIGHT")
390+
386391
configurators.Patterns(&AllowedSources, "IMGPROXY_ALLOWED_SOURCES")
387392

388393
configurators.Bool(&SanitizeSvg, "IMGPROXY_SANITIZE_SVG")

processing/prepare.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package processing
33
import (
44
"math"
55

6+
"github.com/imgproxy/imgproxy/v3/config"
67
"github.com/imgproxy/imgproxy/v3/imagedata"
78
"github.com/imgproxy/imgproxy/v3/imagetype"
89
"github.com/imgproxy/imgproxy/v3/imath"
@@ -45,6 +46,15 @@ func calcScale(width, height int, po *options.ProcessingOptions, imgtype imagety
4546
var wshrink, hshrink float64
4647

4748
srcW, srcH := float64(width), float64(height)
49+
50+
if config.CropMaxWidth > 0 && srcW > float64(config.CropMaxWidth) {
51+
po.Width = config.CropMaxWidth
52+
}
53+
54+
if config.CropMaxHeight > 0 && srcH > float64(config.CropMaxHeight) {
55+
po.Height = config.CropMaxHeight
56+
}
57+
4858
dstW, dstH := float64(po.Width), float64(po.Height)
4959

5060
if po.Width == 0 {

0 commit comments

Comments
 (0)