|
|
|
@ -11,16 +11,15 @@ |
|
|
|
|
|
|
|
namespace ImageProcessor.Web.Processors |
|
|
|
{ |
|
|
|
using System; |
|
|
|
using System.Collections.Specialized; |
|
|
|
using System.Drawing; |
|
|
|
using System.IO; |
|
|
|
using System.Text; |
|
|
|
using System.Text.RegularExpressions; |
|
|
|
using System.Web; |
|
|
|
using System.Web.Hosting; |
|
|
|
|
|
|
|
using ImageProcessor.Imaging; |
|
|
|
using ImageProcessor.Processors; |
|
|
|
using ImageProcessor.Web.Extensions; |
|
|
|
using ImageProcessor.Web.Helpers; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -32,27 +31,7 @@ namespace ImageProcessor.Web.Processors |
|
|
|
/// <summary>
|
|
|
|
/// The regular expression to search strings for.
|
|
|
|
/// </summary>
|
|
|
|
private static readonly Regex QueryRegex = new Regex(@"(overlay=|overlay.\w+=)[^&]+", RegexOptions.Compiled); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The overlay image regex.
|
|
|
|
/// </summary>
|
|
|
|
private static readonly Regex ImageRegex = new Regex(@"overlay=[\w+-]+." + ImageHelpers.ExtensionRegexPattern); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The point regex.
|
|
|
|
/// </summary>
|
|
|
|
private static readonly Regex PointRegex = new Regex(@"overlay.position=\d+,\d+", RegexOptions.Compiled); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The size regex.
|
|
|
|
/// </summary>
|
|
|
|
private static readonly Regex SizeRegex = new Regex(@"overlay.size=\d+,\d+", RegexOptions.Compiled); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The opacity regex.
|
|
|
|
/// </summary>
|
|
|
|
private static readonly Regex OpacityRegex = new Regex(@"overlay.opacity=\d+", RegexOptions.Compiled); |
|
|
|
private static readonly Regex QueryRegex = new Regex(@"overlay=[\w+-]+." + ImageHelpers.ExtensionRegexPattern); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="Overlay"/> class.
|
|
|
|
@ -92,40 +71,30 @@ namespace ImageProcessor.Web.Processors |
|
|
|
/// </returns>
|
|
|
|
public int MatchRegexIndex(string queryString) |
|
|
|
{ |
|
|
|
int index = 0; |
|
|
|
|
|
|
|
// Set the sort order to max to allow filtering.
|
|
|
|
this.SortOrder = int.MaxValue; |
|
|
|
Match match = this.RegexPattern.Match(queryString); |
|
|
|
|
|
|
|
// First merge the matches so we can parse .
|
|
|
|
StringBuilder stringBuilder = new StringBuilder(); |
|
|
|
|
|
|
|
foreach (Match match in this.RegexPattern.Matches(queryString)) |
|
|
|
if (match.Success) |
|
|
|
{ |
|
|
|
if (match.Success) |
|
|
|
{ |
|
|
|
if (index == 0) |
|
|
|
{ |
|
|
|
// Set the index on the first instance only.
|
|
|
|
this.SortOrder = match.Index; |
|
|
|
} |
|
|
|
|
|
|
|
stringBuilder.Append(match.Value); |
|
|
|
|
|
|
|
index += 1; |
|
|
|
} |
|
|
|
} |
|
|
|
this.SortOrder = match.Index; |
|
|
|
NameValueCollection queryCollection = HttpUtility.ParseQueryString(queryString); |
|
|
|
Image image = this.ParseImage(queryCollection["overlay"]); |
|
|
|
|
|
|
|
Point? position = queryCollection["overlay.position"] != null |
|
|
|
? QueryParamParser.Instance.ParseValue<Point>(queryCollection["overlay.position"]) |
|
|
|
: (Point?)null; |
|
|
|
|
|
|
|
int opacity = queryCollection["overlay.opacity"] != null |
|
|
|
? QueryParamParser.Instance.ParseValue<int>(queryCollection["overlay.opacity"]) |
|
|
|
: 100; |
|
|
|
Size size = QueryParamParser.Instance.ParseValue<Size>(queryCollection["overlay.size"]); |
|
|
|
|
|
|
|
if (this.SortOrder < int.MaxValue) |
|
|
|
{ |
|
|
|
// Match syntax
|
|
|
|
string toParse = stringBuilder.ToString(); |
|
|
|
this.Processor.DynamicParameter = new ImageLayer |
|
|
|
{ |
|
|
|
Image = this.ParseImage(toParse), |
|
|
|
Position = this.ParsePoint(toParse), |
|
|
|
Opacity = this.ParseOpacity(toParse), |
|
|
|
Size = this.ParseSize(toParse) |
|
|
|
Image = image, |
|
|
|
Position = position, |
|
|
|
Opacity = opacity, |
|
|
|
Size = size |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
@ -133,13 +102,13 @@ namespace ImageProcessor.Web.Processors |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the correct size of pixels.
|
|
|
|
/// Returns an image from the given input path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="input">
|
|
|
|
/// The input containing the value to parse.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// The <see cref="int"/> representing the pixel size.
|
|
|
|
/// The <see cref="Image"/> representing the given image path.
|
|
|
|
/// </returns>
|
|
|
|
public Image ParseImage(string input) |
|
|
|
{ |
|
|
|
@ -151,94 +120,19 @@ namespace ImageProcessor.Web.Processors |
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(path) && path.StartsWith("~/")) |
|
|
|
{ |
|
|
|
Match match = ImageRegex.Match(input); |
|
|
|
|
|
|
|
if (match.Success) |
|
|
|
string imagePath = HostingEnvironment.MapPath(path); |
|
|
|
if (imagePath != null) |
|
|
|
{ |
|
|
|
string imagePath = HostingEnvironment.MapPath(path); |
|
|
|
if (imagePath != null) |
|
|
|
imagePath = Path.Combine(imagePath, input); |
|
|
|
using (ImageFactory factory = new ImageFactory()) |
|
|
|
{ |
|
|
|
imagePath = Path.Combine(imagePath, match.Value.Split('=')[1]); |
|
|
|
using (ImageFactory factory = new ImageFactory()) |
|
|
|
{ |
|
|
|
factory.Load(imagePath); |
|
|
|
image = new Bitmap(factory.Image); |
|
|
|
} |
|
|
|
factory.Load(imagePath); |
|
|
|
image = new Bitmap(factory.Image); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return image; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the correct <see cref="Nullable{Point}"/> for the given string.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="input">
|
|
|
|
/// The input string containing the value to parse.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// The correct <see cref="Nullable{Point}"/>
|
|
|
|
/// </returns>
|
|
|
|
private Point? ParsePoint(string input) |
|
|
|
{ |
|
|
|
int[] dimensions = { }; |
|
|
|
|
|
|
|
Match match = PointRegex.Match(input); |
|
|
|
if (match.Success) |
|
|
|
{ |
|
|
|
dimensions = match.Value.ToPositiveIntegerArray(); |
|
|
|
} |
|
|
|
|
|
|
|
if (dimensions.Length == 2) |
|
|
|
{ |
|
|
|
return new Point(dimensions[0], dimensions[1]); |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the correct <see cref="int"/> for the given string.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="input">
|
|
|
|
/// The input string containing the value to parse.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// The correct <see cref="int"/>
|
|
|
|
/// </returns>
|
|
|
|
private int ParseOpacity(string input) |
|
|
|
{ |
|
|
|
int opacity = 100; |
|
|
|
Match match = OpacityRegex.Match(input); |
|
|
|
if (match.Success) |
|
|
|
{ |
|
|
|
opacity = Math.Abs(CommonParameterParserUtility.ParseIn100Range(match.Value.Split('=')[1])); |
|
|
|
} |
|
|
|
|
|
|
|
return opacity; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the correct <see cref="Size"/> for the given string.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="input">
|
|
|
|
/// The input string containing the value to parse.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// The <see cref="Size"/>.
|
|
|
|
/// </returns>
|
|
|
|
private Size ParseSize(string input) |
|
|
|
{ |
|
|
|
Size size = Size.Empty; |
|
|
|
Match match = SizeRegex.Match(input); |
|
|
|
if (match.Success) |
|
|
|
{ |
|
|
|
int[] dimensions = match.Value.ToPositiveIntegerArray(); |
|
|
|
size = new Size(dimensions[0], dimensions[1]); |
|
|
|
} |
|
|
|
|
|
|
|
return size; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|