From f200e0d816451f27a975cc75ec23b757b2dd292f Mon Sep 17 00:00:00 2001 From: James South Date: Mon, 9 Dec 2013 00:34:50 +0000 Subject: [PATCH] Adding preset () Former-commit-id: 49f1164764e485fde0c7fc54a181e35be212b056 --- .../NET45/ImageFactoryExtensions.cs | 6 + .../NET45/ImageProcessor.Web_NET45.csproj | 1 + src/ImageProcessor.Web/NET45/Preset.cs | 121 ++++++++++++++++++ .../Views/Home/Index.cshtml | 1 + .../config/imageprocessor/processing.config | 5 + 5 files changed, 134 insertions(+) create mode 100644 src/ImageProcessor.Web/NET45/Preset.cs diff --git a/src/ImageProcessor.Web/NET45/ImageFactoryExtensions.cs b/src/ImageProcessor.Web/NET45/ImageFactoryExtensions.cs index bbf71e4d8f..a8d4bf7836 100644 --- a/src/ImageProcessor.Web/NET45/ImageFactoryExtensions.cs +++ b/src/ImageProcessor.Web/NET45/ImageFactoryExtensions.cs @@ -54,6 +54,12 @@ namespace ImageProcessor.Web { Image img = graphicsProcessor.ProcessImage(factory); factory.Update(img); + + // Break to prevent loop as Preset calls AutoProcess internally. + if (graphicsProcessor.GetType() == typeof(Preset)) + { + break; + } } } } diff --git a/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj b/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj index d9a2832fd6..59cd99bfc3 100644 --- a/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj +++ b/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj @@ -63,6 +63,7 @@ + diff --git a/src/ImageProcessor.Web/NET45/Preset.cs b/src/ImageProcessor.Web/NET45/Preset.cs new file mode 100644 index 0000000000..769049dcab --- /dev/null +++ b/src/ImageProcessor.Web/NET45/Preset.cs @@ -0,0 +1,121 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Encapsulates methods to that allow the processing of preset image processing instructions. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Web +{ + using System.Collections.Generic; + using System.Drawing; + using System.Text.RegularExpressions; + using ImageProcessor.Processors; + + /// + /// Encapsulates methods to that allow the processing of preset image processing instructions. + /// + public class Preset : IGraphicsProcessor + { + /// + /// The regular expression to search strings for. + /// + private static readonly Regex QueryRegex = new Regex(@"preset=[^&]*", RegexOptions.Compiled); + + /// + /// Gets the regular expression to search strings for. + /// + public Regex RegexPattern + { + get + { + return QueryRegex; + } + } + + /// + /// Gets DynamicParameter. + /// + public dynamic DynamicParameter + { + get; + private set; + } + + /// + /// Gets the order in which this processor is to be used in a chain. + /// + public int SortOrder + { + get; + private set; + } + + /// + /// Gets or sets any additional settings required by the processor. + /// + public Dictionary Settings + { + get; + set; + } + + /// + /// The match regex index. + /// + /// + /// The query string. + /// + /// + /// The . + /// + public int MatchRegexIndex(string queryString) + { + int index = 0; + + // Set the sort order to max to allow filtering. + this.SortOrder = int.MaxValue; + + 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; + string preset = match.Value; + this.DynamicParameter = preset; + } + + index += 1; + } + } + + return this.SortOrder; + } + + /// + /// Processes the image. + /// + /// The the current instance of the class containing + /// the image to process. + /// + /// The processed image from the current instance of the class. + /// + public Image ProcessImage(ImageFactory factory) + { + string preset = this.DynamicParameter; + string querystring; + this.Settings.TryGetValue(preset.Split('=')[1], out querystring); + + string oldQueryString = factory.QueryString; + string newQueryString = Regex.Replace(oldQueryString, preset, querystring ?? string.Empty); + + return factory.AddQueryString(newQueryString).AutoProcess().Image; + } + } +} diff --git a/src/TestWebsites/NET45/Test_Website_NET45/Views/Home/Index.cshtml b/src/TestWebsites/NET45/Test_Website_NET45/Views/Home/Index.cshtml index 83e5d4cbab..52734d05a3 100644 --- a/src/TestWebsites/NET45/Test_Website_NET45/Views/Home/Index.cshtml +++ b/src/TestWebsites/NET45/Test_Website_NET45/Views/Home/Index.cshtml @@ -10,6 +10,7 @@

Foreign language test.

+

Cropped

diff --git a/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config b/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config index 1ad37e85a6..30e796783c 100644 --- a/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config +++ b/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config @@ -13,6 +13,11 @@ + + + + +