Browse Source

properly parse boolean processing options

DarthSim 5 years ago
parent
commit
ba3e20b865
4 changed files with 19 additions and 7 deletions
  1. 2 0
      CHANGELOG.md
  2. 4 4
      docs/generating_the_url_advanced.md
  3. 1 1
      docs/generating_the_url_basic.md
  4. 12 2
      processing_options.go

+ 2 - 0
CHANGELOG.md

@@ -1,6 +1,8 @@
 # Changelog
 
 ## [Unreleased]
+### Changed
+- Boolean processing options such as `enlarge` and `extend` are properly parsed. `1`, `t`, `TRUE`, `true`, `True` are truthy, `0`, `f`, `F`, `FALSE`, `false`, `False` are falsy. All other values are treated as falsy and generate a warning message.
 
 ## [2.6.2] - 2019-11-11
 ### Fixed

+ 4 - 4
docs/generating_the_url_advanced.md

@@ -103,9 +103,9 @@ enlarge:%enlarge
 el:%enlarge
 ```
 
-If set to `0`, imgproxy will not enlarge the image if it is smaller than the given size. With any other value, imgproxy will enlarge the image.
+When set to `1`, `t` or `true`, imgproxy will enlarge the image if it is smaller than the given size.
 
-Default: `0`
+Default: false
 
 #### Extend
 
@@ -114,9 +114,9 @@ extend:%extend
 ex:%extend
 ```
 
-If set to `0`, imgproxy will not extend the image if the resizing result is smaller than the given size. With any other value, imgproxy will extend the image to the given size.
+When set to `1`, `t` or `true`, imgproxy will extend the image if it is smaller than the given size.
 
-Default: `0`
+Default: false
 
 #### Gravity
 

+ 1 - 1
docs/generating_the_url_basic.md

@@ -49,7 +49,7 @@ When imgproxy needs to cut some parts of the image, it is guided by the gravity.
 
 ### Enlarge
 
-When set to `0`, imgproxy will not enlarge the image if it is smaller than the given size. With any other value, imgproxy will enlarge the image.
+When set to `1`, `t` or `true`, imgproxy will enlarge the image if it is smaller than the given size.
 
 ### Source URL
 

+ 12 - 2
processing_options.go

@@ -330,6 +330,16 @@ func parseDimension(d *int, name, arg string) error {
 	return nil
 }
 
+func parseBoolOption(str string) bool {
+	b, err := strconv.ParseBool(str)
+
+	if err != nil {
+		logWarning("`%s` is not a valid boolean value. Treated as false", str)
+	}
+
+	return b
+}
+
 func isGravityOffcetValid(gravity gravityType, offset float64) bool {
 	if gravity == gravityCenter {
 		return true
@@ -397,7 +407,7 @@ func applyEnlargeOption(po *processingOptions, args []string) error {
 		return fmt.Errorf("Invalid enlarge arguments: %v", args)
 	}
 
-	po.Enlarge = args[0] != "0"
+	po.Enlarge = parseBoolOption(args[0])
 
 	return nil
 }
@@ -407,7 +417,7 @@ func applyExtendOption(po *processingOptions, args []string) error {
 		return fmt.Errorf("Invalid extend arguments: %v", args)
 	}
 
-	po.Extend = args[0] != "0"
+	po.Extend = parseBoolOption(args[0])
 
 	return nil
 }