Implement Max Bytes Filter (#275)

* Implement Max Bytes Filter

* Update according to code review comments

* Refactor according to code review comments
This commit is contained in:
Dmitry Zuev
2019-11-29 15:27:35 +03:00
committed by Sergey Alexandrovich
parent 49aaecf464
commit d74c402066
3 changed files with 70 additions and 0 deletions

View File

@@ -116,6 +116,7 @@ type processingOptions struct {
Crop cropOptions
Format imageType
Quality int
MaxBytes int
Flatten bool
Background rgbColor
Blur float32
@@ -193,6 +194,7 @@ func newProcessingOptions() *processingOptions {
Gravity: gravityOptions{Type: gravityCenter},
Enlarge: false,
Quality: conf.Quality,
MaxBytes: 0,
Format: imageTypeUnknown,
Background: rgbColor{255, 255, 255},
Blur: 0,
@@ -542,6 +544,20 @@ func applyQualityOption(po *processingOptions, args []string) error {
return nil
}
func applyMaxBytesOption(po *processingOptions, args []string) error {
if len(args) > 1 {
return fmt.Errorf("Invalid max_bytes arguments: %v", args)
}
if max, err := strconv.Atoi(args[0]); err == nil && max >= 0 {
po.MaxBytes = max
} else {
return fmt.Errorf("Invalid max_bytes: %s", args[0])
}
return nil
}
func applyBackgroundOption(po *processingOptions, args []string) error {
switch len(args) {
case 1:
@@ -744,6 +760,8 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
return applyCropOption(po, args)
case "quality", "q":
return applyQualityOption(po, args)
case "max_bytes", "mb":
return applyMaxBytesOption(po, args)
case "background", "bg":
return applyBackgroundOption(po, args)
case "blur", "bl":