Browse Source

min-width and min-height processing options

DarthSim 4 years ago
parent
commit
fca4f87445
4 changed files with 63 additions and 0 deletions
  1. 1 0
      CHANGELOG.md
  2. 26 0
      docs/generating_the_url.md
  3. 14 0
      process.go
  4. 22 0
      processing_options.go

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
 - [skip processing](https://docs.imgproxy.net/#/generating_the_url?id=skip-processing) processing option.
 - [Datadog](./docs/datadog.md) metrics.
 - `force` and `fill-down` resizing types.
+- [min-width](https://docs.imgproxy.net/#/generating_the_url?id=min-width) and [min-height](https://docs.imgproxy.net/#/generating_the_url?id=min-height) processing options.
 
 ### Removed
 - Removed basic URL format, use [advanced one](./docs/generating_the_url.md) instead.

+ 26 - 0
docs/generating_the_url.md

@@ -95,6 +95,32 @@ Defines the height of the resulting image. When set to `0`, imgproxy will calcul
 
 Default: `0`
 
+### Min width
+
+```
+min-width:%width
+mw:%width
+```
+
+Defines the minimum width of the resulting image.
+
+**⚠️Warning:** When both `width` and `min-width` are set, the final image will be cropped according to `width`, so use this combination with care.
+
+Default: `0`
+
+### Min height
+
+```
+min-height:%height
+mh:%height
+```
+
+Defines the minimum height of the resulting image.
+
+**⚠️Warning:** When both `height` and `min-height` are set, the final image will be cropped according to `height`, so use this combination with care.
+
+Default: `0`
+
 ### Dpr
 
 ```

+ 14 - 0
process.go

@@ -135,6 +135,20 @@ func calcScale(width, height int, po *processingOptions, imgtype imageType) (flo
 		}
 	}
 
+	if po.MinWidth > 0 {
+		if minShrink := srcW / float64(po.MinWidth); minShrink < wshrink {
+			hshrink /= wshrink / minShrink
+			wshrink = minShrink
+		}
+	}
+
+	if po.MinHeight > 0 {
+		if minShrink := srcH / float64(po.MinHeight); minShrink < hshrink {
+			wshrink /= hshrink / minShrink
+			hshrink = minShrink
+		}
+	}
+
 	wshrink /= po.Dpr
 	hshrink /= po.Dpr
 

+ 22 - 0
processing_options.go

@@ -132,6 +132,8 @@ type processingOptions struct {
 	ResizingType      resizeType
 	Width             int
 	Height            int
+	MinWidth          int
+	MinHeight         int
 	Dpr               float64
 	Gravity           gravityOptions
 	Enlarge           bool
@@ -461,6 +463,22 @@ func applyHeightOption(po *processingOptions, args []string) error {
 	return parseDimension(&po.Height, "height", args[0])
 }
 
+func applyMinWidthOption(po *processingOptions, args []string) error {
+	if len(args) > 1 {
+		return fmt.Errorf("Invalid min width arguments: %v", args)
+	}
+
+	return parseDimension(&po.MinWidth, "min width", args[0])
+}
+
+func applyMinHeightOption(po *processingOptions, args []string) error {
+	if len(args) > 1 {
+		return fmt.Errorf("Invalid min height arguments: %v", args)
+	}
+
+	return parseDimension(&po.MinHeight, " min height", args[0])
+}
+
 func applyEnlargeOption(po *processingOptions, args []string) error {
 	if len(args) > 1 {
 		return fmt.Errorf("Invalid enlarge arguments: %v", args)
@@ -970,6 +988,10 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
 		return applyWidthOption(po, args)
 	case "height", "h":
 		return applyHeightOption(po, args)
+	case "min-width", "mw":
+		return applyMinWidthOption(po, args)
+	case "min-height", "mh":
+		return applyMinHeightOption(po, args)
 	case "enlarge", "el":
 		return applyEnlargeOption(po, args)
 	case "extend", "ex":