// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Crops an image to the given directions. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Processors { using System.Text; using System.Text.RegularExpressions; using ImageProcessor.Imaging; using ImageProcessor.Processors; using ImageProcessor.Web.Extensions; /// /// Crops an image to the given directions. /// public class Crop : IWebGraphicsProcessor { /// /// The regular expression to search strings for. /// /// private static readonly Regex QueryRegex = new Regex(@"(crop=|cropmode=)[^&]+", RegexOptions.Compiled); /// /// The coordinate regex. /// private static readonly Regex CoordinateRegex = new Regex(@"crop=\d+(.\d+)?[,-]\d+(.\d+)?[,-]\d+(.\d+)?[,-]\d+(.\d+)?", RegexOptions.Compiled); /// /// The mode regex. /// private static readonly Regex ModeRegex = new Regex(@"cropmode=(pixels|percent)", RegexOptions.Compiled); /// /// Initializes a new instance of the class. /// public Crop() { this.Processor = new ImageProcessor.Processors.Crop(); } /// /// 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)) { // Match but ignore entropy if (match.Success && !queryString.ToUpperInvariant().Contains("ENTROPYCROP=")) { if (index == 0) { // Set the index on the first instance only. this.SortOrder = match.Index; } stringBuilder.Append(match.Value); index += 1; } } if (this.SortOrder < int.MaxValue) { // Match syntax string toParse = stringBuilder.ToString(); float[] coordinates = this.ParseCoordinates(toParse); CropMode cropMode = this.ParseMode(toParse); CropLayer cropLayer = new CropLayer(coordinates[0], coordinates[1], coordinates[2], coordinates[3], cropMode); this.Processor.DynamicParameter = cropLayer; } return this.SortOrder; } /// /// Returns the correct for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct . /// private CropMode ParseMode(string input) { foreach (Match match in ModeRegex.Matches(input)) { // Split on = string mode = match.Value.Split('=')[1]; switch (mode) { case "percent": return CropMode.Percentage; case "pixels": return CropMode.Pixels; } } return CropMode.Pixels; } /// /// Returns the crop coordinates for the given string. /// /// /// The input string containing the value to parse. /// /// /// The array containing the crop coordinates. /// private float[] ParseCoordinates(string input) { float[] floats = { }; foreach (Match match in CoordinateRegex.Matches(input)) { floats = match.Value.ToPositiveFloatArray(); } return floats; } } }