Browse Source

Adding Overlay to ImageProcessor.Web

Former-commit-id: 44c974a8eccc401c5042feafc3296072337b88ca
Former-commit-id: 7ccac61927cde0398d204c868b14890c87ac9071
af/merge-core
James South 12 years ago
parent
commit
897972f2c0
  1. 0
      src/ImageProcessor.UnitTests/Images/imageprocessor/mask/mask.png
  2. 1
      src/ImageProcessor.UnitTests/Images/imageprocessor/overlay/monster.png.REMOVED.git-id
  3. 14
      src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs
  4. 7
      src/ImageProcessor.Web/Configuration/Resources/processing.config
  5. 4
      src/ImageProcessor.Web/Helpers/ImageHelpers.cs
  6. 1
      src/ImageProcessor.Web/ImageProcessor.Web.csproj
  7. 16
      src/ImageProcessor.Web/Processors/Mask.cs
  8. 244
      src/ImageProcessor.Web/Processors/Overlay.cs
  9. 1
      src/ImageProcessor.Web/Processors/Resize.cs
  10. 7
      src/ImageProcessor.Web/Processors/Watermark.cs
  11. 2
      src/ImageProcessor/ImageProcessor.csproj
  12. 7
      src/ImageProcessor/Processors/Overlay.cs

0
src/ImageProcessor.UnitTests/Images/masks/mask.png → src/ImageProcessor.UnitTests/Images/imageprocessor/mask/mask.png

1
src/ImageProcessor.UnitTests/Images/imageprocessor/overlay/monster.png.REMOVED.git-id

@ -0,0 +1 @@
4edf74a6857665c8efe2d3282c25907f5b20ca81

14
src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs

@ -459,6 +459,20 @@ namespace ImageProcessor.Web.UnitTests
DropShadow = true,
FontFamily = new FontFamily("arial")
}
},
{
"watermark=watermark goodness&color=fff&fontsize=36&fontstyle=italic&fontopacity=80&watermark.position=30,150&textshadow=true&fontfamily=arial",
new TextLayer
{
Text = "watermark goodness",
FontColor = ColorTranslator.FromHtml("#" + "ffffff"),
FontSize = 36,
Style = FontStyle.Italic,
Opacity = 80,
Position = new Point(30, 150),
DropShadow = true,
FontFamily = new FontFamily("arial")
}
}
};

7
src/ImageProcessor.Web/Configuration/Resources/processing.config

@ -30,10 +30,15 @@
<plugin name="Hue" type="ImageProcessor.Web.Processors.Hue, ImageProcessor.Web"/>
<plugin name="Mask" type="ImageProcessor.Web.Processors.Mask, ImageProcessor.Web">
<settings>
<setting key="VirtualPath" value="~/images/masks/"/>
<setting key="VirtualPath" value="~/images/imageprocessor/mask/"/>
</settings>
</plugin>
<plugin name="Meta" type="ImageProcessor.Web.Processors.Meta, ImageProcessor.Web"/>
<plugin name="Overlay" type="ImageProcessor.Web.Processors.Overlay, ImageProcessor.Web">
<settings>
<setting key="VirtualPath" value="~/images/imageprocessor/overlay/"/>
</settings>
</plugin>
<plugin name="Pixelate" type="ImageProcessor.Web.Processors.Pixelate, ImageProcessor.Web"/>
<plugin name="Quality" type="ImageProcessor.Web.Processors.Quality, ImageProcessor.Web"/>
<plugin name="ReplaceColor" type="ImageProcessor.Web.Processors.ReplaceColor, ImageProcessor.Web"/>

4
src/ImageProcessor.Web/Helpers/ImageHelpers.cs

@ -28,9 +28,9 @@ namespace ImageProcessor.Web.Helpers
/// <summary>
/// The exclude regex for matching things to ignore when parsing image extensions.
/// I'd like to make something more extensible than this.
/// TODO: This is hacky and awful and should go.
/// </summary>
private static readonly Regex ExcludeRegex = new Regex(@"mask=[\w+-]+.", RegexOptions.IgnoreCase);
private static readonly Regex ExcludeRegex = new Regex(@"(mask|overlay)=[\w+-]+.", RegexOptions.IgnoreCase);
/// <summary>
/// The image format regex.

1
src/ImageProcessor.Web/ImageProcessor.Web.csproj

@ -50,6 +50,7 @@
<Compile Include="Processors\EntropyCrop.cs" />
<Compile Include="Processors\Hue.cs" />
<Compile Include="Processors\Mask.cs" />
<Compile Include="Processors\Overlay.cs" />
<Compile Include="Processors\Pixelate.cs" />
<Compile Include="Processors\ReplaceColor.cs" />
<Compile Include="Services\IImageService.cs" />

16
src/ImageProcessor.Web/Processors/Mask.cs

@ -31,17 +31,17 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"(mask=|maskposition=)[^&]+", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"(mask=|mask.\w+=)[^&]+", RegexOptions.Compiled);
/// <summary>
/// The mask image regex.
/// </summary>
private static readonly Regex PixelRegex = new Regex(@"mask=[\w+-]+." + ImageHelpers.ExtensionRegexPattern);
private static readonly Regex ImageRegex = new Regex(@"mask=[\w+-]+." + ImageHelpers.ExtensionRegexPattern);
/// <summary>
/// The point regex.
/// </summary>
private static readonly Regex PointRegex = new Regex(@"maskposition=\d+,\d+", RegexOptions.Compiled);
private static readonly Regex PointRegex = new Regex(@"mask.position=\d+,\d+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Mask"/> class.
@ -110,8 +110,8 @@ namespace ImageProcessor.Web.Processors
// Match syntax
string toParse = stringBuilder.ToString();
Image image = this.ParseImage(toParse);
Point? rectangle = this.ParsePoint(toParse);
this.Processor.DynamicParameter = new Tuple<Image, Point?>(image, rectangle);
Point? position = this.ParsePoint(toParse);
this.Processor.DynamicParameter = new Tuple<Image, Point?>(image, position);
}
return this.SortOrder;
@ -136,7 +136,7 @@ namespace ImageProcessor.Web.Processors
if (!string.IsNullOrWhiteSpace(path) && path.StartsWith("~/"))
{
Match match = PixelRegex.Match(input);
Match match = ImageRegex.Match(input);
if (match.Success)
{
@ -168,8 +168,8 @@ namespace ImageProcessor.Web.Processors
private Point? ParsePoint(string input)
{
int[] dimensions = { };
foreach (Match match in PointRegex.Matches(input))
Match match = PointRegex.Match(input);
if (match.Success)
{
dimensions = match.Value.ToPositiveIntegerArray();
}

244
src/ImageProcessor.Web/Processors/Overlay.cs

@ -0,0 +1,244 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Overlay.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Adds an image overlay to the current image.
// If the overlay is larger than the image it will be scaled to match the image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Hosting;
using ImageProcessor.Imaging;
using ImageProcessor.Processors;
using ImageProcessor.Web.Extensions;
using ImageProcessor.Web.Helpers;
/// <summary>
/// Adds an image overlay to the current image.
/// If the overlay is larger than the image it will be scaled to match the image.
/// </summary>
public class Overlay : IWebGraphicsProcessor
{
/// <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);
/// <summary>
/// Initializes a new instance of the <see cref="Overlay"/> class.
/// </summary>
public Overlay()
{
this.Processor = new ImageProcessor.Processors.Overlay();
}
/// <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;
// 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(match.Value);
index += 1;
}
}
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)
};
}
return this.SortOrder;
}
/// <summary>
/// Returns the correct size of pixels.
/// </summary>
/// <param name="input">
/// The input containing the value to parse.
/// </param>
/// <returns>
/// The <see cref="int"/> representing the pixel size.
/// </returns>
public Image ParseImage(string input)
{
Image image = null;
// Correctly parse the path.
string path;
this.Processor.Settings.TryGetValue("VirtualPath", out path);
if (!string.IsNullOrWhiteSpace(path) && path.StartsWith("~/"))
{
Match match = ImageRegex.Match(input);
if (match.Success)
{
string imagePath = HostingEnvironment.MapPath(path);
if (imagePath != null)
{
imagePath = Path.Combine(imagePath, match.Value.Split('=')[1]);
using (ImageFactory factory = new ImageFactory())
{
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;
}
}
}

1
src/ImageProcessor.Web/Processors/Resize.cs

@ -112,6 +112,7 @@ namespace ImageProcessor.Web.Processors
if (match.Success)
{
// We don't want any resize carve or percentile crops requests to interfere.
// TODO: This is hacky and awful and should go.
if (match.Value.ToUpperInvariant().Contains("CARVE") || match.Value.ToUpperInvariant().Contains("PERCENT"))
{
break;

7
src/ImageProcessor.Web/Processors/Watermark.cs

@ -39,7 +39,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for the position attribute.
/// </summary>
private static readonly Regex PositionRegex = new Regex(@"(text)?position(=|-)\d+[-,]\d+", RegexOptions.Compiled);
private static readonly Regex PositionRegex = new Regex(@"(watermark.position|textposition|[^.](&,=)?position)(=|-)\d+[-,]\d+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the font size attribute.
@ -59,7 +59,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for the opacity attribute.
/// </summary>
private static readonly Regex OpacityRegex = new Regex(@"((font)?)opacity(=|-)(?:100|[1-9]?[0-9])", RegexOptions.Compiled);
private static readonly Regex OpacityRegex = new Regex(@"(watermark.opacity|fontopacity|[^.](&,=)?opacity)(=|-)(?:100|[1-9]?[0-9])", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the shadow attribute.
@ -182,7 +182,8 @@ namespace ImageProcessor.Web.Processors
{
foreach (Match match in PositionRegex.Matches(input))
{
int[] position = match.Value.ToPositiveIntegerArray();
// Chop off the leading legacy support '='
int[] position = match.Value.TrimStart('=').ToPositiveIntegerArray();
if (position != null)
{

2
src/ImageProcessor/ImageProcessor.csproj

@ -159,7 +159,7 @@
<Compile Include="Common\Exceptions\ImageFormatException.cs" />
<Compile Include="ImageFactory.cs" />
<Compile Include="Imaging\AnchorPosition.cs" />
<Compile Include="Imaging\Formats\IIndexedImageFormat.cs" />
<Compile Include="Imaging\Formats\IQuantizableImageFormat.cs" />
<Compile Include="Imaging\Helpers\ImageMaths.cs" />
<Compile Include="Imaging\ImageLayer.cs" />
<Compile Include="Imaging\MetaData\ExifPropertyTagType.cs" />

7
src/ImageProcessor/Processors/Overlay.cs

@ -20,7 +20,8 @@ namespace ImageProcessor.Processors
using ImageProcessor.Imaging.Helpers;
/// <summary>
/// Adds an image overlay to the current image.
/// Adds an image overlay to the current image.
/// If the overlay is larger than the image it will be scaled to match the image.
/// </summary>
public class Overlay : IGraphicsProcessor
{
@ -77,8 +78,8 @@ namespace ImageProcessor.Processors
Size size = imageLayer.Size;
int width = image.Width;
int height = image.Height;
int overlayWidth = Math.Min(image.Size.Width, size.Width);
int overlayHeight = Math.Min(image.Size.Height, size.Height);
int overlayWidth = size != Size.Empty ? Math.Min(image.Size.Width, size.Width) : image.Size.Width;
int overlayHeight = size != Size.Empty ? Math.Min(image.Size.Height, size.Height) : image.Size.Height;
Point? position = imageLayer.Position;
int opacity = imageLayer.Opacity;

Loading…
Cancel
Save