// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Resizes an image to the given dimensions. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Processors { using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using ImageProcessor.Imaging; using ImageProcessor.Processors; using ImageProcessor.Web.Extensions; /// /// Resizes an image to the given dimensions. /// public class Resize : IWebGraphicsProcessor { /// /// The regular expression to search strings for. /// private static readonly Regex QueryRegex = new Regex(@"(width|height)=|(width|height)ratio=|mode=|anchor=|center=|upscale=", RegexOptions.Compiled); /// /// The regular expression to search strings for the size attribute. /// private static readonly Regex SizeRegex = new Regex(@"(width|height)=\d+", RegexOptions.Compiled); /// /// The regular expression to search strings for the ratio attribute. /// private static readonly Regex RatioRegex = new Regex(@"(width|height)ratio=\d+(.\d+)?", RegexOptions.Compiled); /// /// The regular expression to search strings for the mode attribute. /// private static readonly Regex ModeRegex = new Regex(@"mode=(pad|stretch|crop|max)", RegexOptions.Compiled); /// /// The regular expression to search strings for the anchor attribute. /// private static readonly Regex AnchorRegex = new Regex(@"anchor=(top|bottom|left|right|center)", RegexOptions.Compiled); /// /// The regular expression to search strings for the center attribute. /// private static readonly Regex CenterRegex = new Regex(@"center=\d+(.\d+)?[,-]\d+(.\d+)", RegexOptions.Compiled); /// /// The regular expression to search strings for the upscale attribute. /// private static readonly Regex UpscaleRegex = new Regex(@"upscale=false", RegexOptions.Compiled); /// /// Initializes a new instance of the class. /// public Resize() { this.Processor = new ImageProcessor.Processors.Resize(); } /// /// Gets the regular expression to search strings for. /// public Regex RegexPattern { get { return QueryRegex; } } /// /// Gets the order in which this processor is to be used in a chain. /// public int SortOrder { get; private set; } /// /// Gets the associated graphics processor. /// public IGraphicsProcessor Processor { get; private set; } /// /// The position in the original string where the first character of the captured substring was found. /// /// The query string to search. /// /// The zero-based starting position in the original string where the captured substring was found. /// public int MatchRegexIndex(string queryString) { int index = 0; // Set the sort order to max to allow filtering. this.SortOrder = int.MaxValue; // First merge the matches so we can parse . StringBuilder stringBuilder = new StringBuilder(); foreach (Match match in this.RegexPattern.Matches(queryString)) { if (match.Success) { if (index == 0) { // Set the index on the first instance only. this.SortOrder = match.Index; stringBuilder.Append(queryString); } index += 1; } } // Match syntax string toParse = stringBuilder.ToString(); Size size = this.ParseSize(toParse); ResizeLayer resizeLayer = new ResizeLayer(size) { ResizeMode = this.ParseMode(toParse), AnchorPosition = this.ParsePosition(toParse), Upscale = !UpscaleRegex.IsMatch(toParse), CenterCoordinates = this.ParseCoordinates(toParse), }; this.Processor.DynamicParameter = resizeLayer; // Correctly parse any restrictions. string restrictions; this.Processor.Settings.TryGetValue("RestrictTo", out restrictions); ((ImageProcessor.Processors.Resize)this.Processor).RestrictedSizes = this.ParseRestrictions(restrictions); return this.SortOrder; } /// /// Returns the correct for the given string. /// /// /// The input string containing the value to parse. /// /// /// The . /// private Size ParseSize(string input) { const string Width = "width="; const string Height = "height="; const string WidthRatio = "widthratio="; const string HeightRatio = "heightratio="; Size size = new Size(); // First merge the matches so we can parse . StringBuilder stringBuilder = new StringBuilder(); foreach (Match match in SizeRegex.Matches(input)) { stringBuilder.Append(match.Value); } // First cater for single dimensions. string value = stringBuilder.ToString(); if (input.Contains(Width) && !input.Contains(Height)) { size = new Size(value.ToPositiveIntegerArray()[0], 0); } if (input.Contains(Height) && !input.Contains(Width)) { size = new Size(0, value.ToPositiveIntegerArray()[0]); } // Both dimensions supplied. if (input.Contains(Height) && input.Contains(Width)) { int[] dimensions = value.ToPositiveIntegerArray(); // Check the order in which they have been supplied. size = input.IndexOf(Width, StringComparison.Ordinal) < input.IndexOf(Height, StringComparison.Ordinal) ? new Size(dimensions[0], dimensions[1]) : new Size(dimensions[1], dimensions[0]); } // Calculate any ratio driven sizes. if (size.Width == 0 || size.Height == 0) { stringBuilder.Clear(); foreach (Match match in RatioRegex.Matches(input)) { stringBuilder.Append(match.Value); } value = stringBuilder.ToString(); // Replace 0 width if (size.Width == 0 && size.Height > 0 && input.Contains(WidthRatio) && !input.Contains(HeightRatio)) { size.Width = (int)Math.Ceiling(value.ToPositiveFloatArray()[0] * size.Height); } // Replace 0 height if (size.Height == 0 && size.Width > 0 && input.Contains(HeightRatio) && !input.Contains(WidthRatio)) { size.Height = (int)Math.Ceiling(value.ToPositiveFloatArray()[0] * size.Width); } } return size; } /// /// Returns the correct for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct . /// private ResizeMode ParseMode(string input) { foreach (Match match in ModeRegex.Matches(input)) { // Split on = string mode = match.Value.Split('=')[1]; switch (mode) { case "stretch": return ResizeMode.Stretch; case "crop": return ResizeMode.Crop; case "max": return ResizeMode.Max; default: return ResizeMode.Pad; } } return ResizeMode.Pad; } /// /// Returns the correct for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct . /// private AnchorPosition ParsePosition(string input) { foreach (Match match in AnchorRegex.Matches(input)) { // Split on = string anchor = match.Value.Split('=')[1]; switch (anchor) { case "top": return AnchorPosition.Top; case "bottom": return AnchorPosition.Bottom; case "left": return AnchorPosition.Left; case "right": return AnchorPosition.Right; default: return AnchorPosition.Center; } } return AnchorPosition.Center; } /// /// Parses the coordinates. /// /// The input. /// The array containing the coordinates private float[] ParseCoordinates(string input) { float[] floats = { }; foreach (Match match in CenterRegex.Matches(input)) { floats = match.Value.ToPositiveFloatArray(); } return floats; } /// /// Returns a of sizes to restrict resizing to. /// /// /// The input. /// /// /// The to restrict resizing to. /// private List ParseRestrictions(string input) { List sizes = new List(); if (!string.IsNullOrWhiteSpace(input)) { sizes.AddRange(input.Split(',').Select(this.ParseSize)); } return sizes; } } }