diff --git a/src/ImageProcessor/ImageFactory.cs b/src/ImageProcessor/ImageFactory.cs index 4dfc2e297..714b6983a 100644 --- a/src/ImageProcessor/ImageFactory.cs +++ b/src/ImageProcessor/ImageFactory.cs @@ -624,7 +624,7 @@ namespace ImageProcessor int width = size.Width; int height = size.Height; - var resizeSettings = new Dictionary { { "MaxWidth", width.ToString("G") }, { "MaxHeight", height.ToString("G") } }; + Dictionary resizeSettings = new Dictionary { { "MaxWidth", width.ToString("G") }, { "MaxHeight", height.ToString("G") } }; ResizeLayer resizeLayer = new ResizeLayer(new Size(width, height)); diff --git a/src/ImageProcessor/Processors/Resize.cs b/src/ImageProcessor/Processors/Resize.cs index 7989b6bb8..606b058a9 100644 --- a/src/ImageProcessor/Processors/Resize.cs +++ b/src/ImageProcessor/Processors/Resize.cs @@ -17,6 +17,7 @@ namespace ImageProcessor.Processors using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; + using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -171,10 +172,13 @@ namespace ImageProcessor.Processors int defaultMaxWidth; int defaultMaxHeight; + string restrictions; + this.Settings.TryGetValue("RestrictTo", out restrictions); int.TryParse(this.Settings["MaxWidth"], out defaultMaxWidth); int.TryParse(this.Settings["MaxHeight"], out defaultMaxHeight); + List restrictedSizes = this.ParseRestrictions(restrictions); - return this.ResizeImage(factory, width, height, defaultMaxWidth, defaultMaxHeight, backgroundColor, mode, anchor, upscale); + return this.ResizeImage(factory, width, height, defaultMaxWidth, defaultMaxHeight, restrictedSizes, backgroundColor, mode, anchor, upscale); } #endregion @@ -197,6 +201,9 @@ namespace ImageProcessor.Processors /// /// The default max height to resize the image to. /// + /// + /// A containing image resizing restrictions. + /// /// /// The background color to pad the image with. /// @@ -218,6 +225,7 @@ namespace ImageProcessor.Processors int height, int defaultMaxWidth, int defaultMaxHeight, + List restrictedSizes, Color backgroundColor, ResizeMode resizeMode = ResizeMode.Pad, AnchorPosition anchorPosition = AnchorPosition.Center, @@ -342,6 +350,24 @@ namespace ImageProcessor.Processors width = destinationWidth; } + // Restrict sizes + if (restrictedSizes.Any()) + { + bool reject = true; + foreach (Size restrictedSize in restrictedSizes) + { + if (restrictedSize.Width == width || restrictedSize.Height == height) + { + reject = false; + } + } + + if (reject) + { + return image; + } + } + if (width > 0 && height > 0 && width <= maxWidth && height <= maxHeight) { // Exit if upscaling is not allowed. @@ -555,5 +581,20 @@ namespace ImageProcessor.Processors return Color.Transparent; } + + /// + /// Returns a of sizes to restrict resizing to. + /// + /// + /// The input. + /// + /// + /// The to restrict resizing to. + /// + private List ParseRestrictions(string input) + { + string[] splitInput = input.Split(','); + return splitInput.Select(this.ParseSize).ToList(); + } } }