mirror of https://github.com/SixLabors/ImageSharp
13 changed files with 351 additions and 226 deletions
@ -0,0 +1,103 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="Flip.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// Flips an image horizontally or vertically.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Web.Processors |
|||
{ |
|||
using System.Drawing; |
|||
using System.Text.RegularExpressions; |
|||
using ImageProcessor.Processors; |
|||
|
|||
/// <summary>
|
|||
/// Flips an image horizontally or vertically.
|
|||
/// </summary>
|
|||
public class Flip : IWebGraphicsProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// The regular expression to search strings for.
|
|||
/// </summary>
|
|||
private static readonly Regex QueryRegex = new Regex(@"flip=(horizontal|vertical|both)", RegexOptions.Compiled); |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Flip"/> class.
|
|||
/// </summary>
|
|||
public Flip() |
|||
{ |
|||
this.Processor = new ImageProcessor.Processors.Flip(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the regular expression to search strings for.
|
|||
/// </summary>
|
|||
public Regex RegexPattern |
|||
{ |
|||
get |
|||
{ |
|||
return QueryRegex; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the order in which this processor is to be used in a chain.
|
|||
/// </summary>
|
|||
public int SortOrder { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the associated graphics processor.
|
|||
/// </summary>
|
|||
public IGraphicsProcessor Processor { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// The position in the original string where the first character of the captured substring was found.
|
|||
/// </summary>
|
|||
/// <param name="queryString">
|
|||
/// The query string to search.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The zero-based starting position in the original string where the captured substring was found.
|
|||
/// </returns>
|
|||
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 direction = match.Value.Split('=')[1]; |
|||
|
|||
switch (direction) |
|||
{ |
|||
case "horizontal": |
|||
this.Processor.DynamicParameter = RotateFlipType.RotateNoneFlipX; |
|||
break; |
|||
case "vertical": |
|||
this.Processor.DynamicParameter = RotateFlipType.RotateNoneFlipY; |
|||
break; |
|||
default: |
|||
this.Processor.DynamicParameter = RotateFlipType.RotateNoneFlipXY; |
|||
break; |
|||
} |
|||
} |
|||
|
|||
index += 1; |
|||
} |
|||
} |
|||
|
|||
return this.SortOrder; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,156 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="Format.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// Sets the output of the image to a specific format.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Web.Processors |
|||
{ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Text.RegularExpressions; |
|||
using ImageProcessor.Configuration; |
|||
using ImageProcessor.Imaging.Formats; |
|||
using ImageProcessor.Processors; |
|||
|
|||
/// <summary>
|
|||
/// Sets the output of the image to a specific format.
|
|||
/// </summary>
|
|||
public class Format : IWebGraphicsProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// The regular expression to search strings for.
|
|||
/// </summary>
|
|||
private static readonly Regex QueryRegex = BuildRegex(); |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Format"/> class.
|
|||
/// </summary>
|
|||
public Format() |
|||
{ |
|||
this.Processor = new ImageProcessor.Processors.Format(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the regular expression to search strings for.
|
|||
/// </summary>
|
|||
public Regex RegexPattern |
|||
{ |
|||
get |
|||
{ |
|||
return QueryRegex; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the order in which this processor is to be used in a chain.
|
|||
/// </summary>
|
|||
public int SortOrder { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the associated graphics processor.
|
|||
/// </summary>
|
|||
public IGraphicsProcessor Processor { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// The position in the original string where the first character of the captured substring was found.
|
|||
/// </summary>
|
|||
/// <param name="queryString">
|
|||
/// The query string to search.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The zero-based starting position in the original string where the captured substring was found.
|
|||
/// </returns>
|
|||
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; |
|||
|
|||
ISupportedImageFormat format = this.ParseFormat(match.Value.Split('=')[1]); |
|||
if (format != null) |
|||
{ |
|||
this.Processor.DynamicParameter = format; |
|||
} |
|||
} |
|||
|
|||
index += 1; |
|||
} |
|||
} |
|||
|
|||
return this.SortOrder; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Builds a regular expression from the <see cref="T:ImageProcessor.Imaging.Formats.ISupportedImageFormat"/> type, this allows extensibility.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// The <see cref="Regex"/> to match matrix filters.
|
|||
/// </returns>
|
|||
private static Regex BuildRegex() |
|||
{ |
|||
StringBuilder stringBuilder = new StringBuilder(); |
|||
|
|||
// png8 is a special case for determining indexed pngs.
|
|||
stringBuilder.Append("format=(png8"); |
|||
foreach (ISupportedImageFormat imageFormat in ImageProcessorBootstrapper.Instance.SupportedImageFormats) |
|||
{ |
|||
foreach (string fileExtension in imageFormat.FileExtensions) |
|||
{ |
|||
stringBuilder.AppendFormat("|{0}", fileExtension.ToLowerInvariant()); |
|||
} |
|||
} |
|||
|
|||
stringBuilder.Append(")"); |
|||
|
|||
return new Regex(stringBuilder.ToString(), RegexOptions.IgnoreCase); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Parses the input string to return the correct <see cref="ISupportedImageFormat"/>.
|
|||
/// </summary>
|
|||
/// <param name="identifier">
|
|||
/// The identifier.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The <see cref="ISupportedImageFormat"/>.
|
|||
/// </returns>
|
|||
private ISupportedImageFormat ParseFormat(string identifier) |
|||
{ |
|||
identifier = identifier.ToLowerInvariant(); |
|||
ISupportedImageFormat format = ImageProcessorBootstrapper.Instance.SupportedImageFormats |
|||
.FirstOrDefault(f => f.FileExtensions.Any(e => e.Equals(identifier, StringComparison.InvariantCultureIgnoreCase))); |
|||
|
|||
if (format != null) |
|||
{ |
|||
// I wish this wasn't hardcoded but there's no way I can
|
|||
// find to preserve the pallete.
|
|||
if (identifier.Equals("png8")) |
|||
{ |
|||
format.IsIndexed = true; |
|||
} |
|||
else if (identifier.Equals("png")) |
|||
{ |
|||
format.IsIndexed = false; |
|||
} |
|||
} |
|||
|
|||
return format; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue