Browse Source

Tweaking Flip and Watermark

Former-commit-id: 2b324f703944194940e98c5957433424c30deea2
af/merge-core
James South 12 years ago
parent
commit
b466d400f9
  1. 2
      src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs
  2. 10
      src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs
  3. 17
      src/ImageProcessor.Web/Processors/Watermark.cs
  4. 29
      src/ImageProcessor/ImageFactory.cs
  5. 2
      src/ImageProcessor/Imaging/RoundedCornerLayer.cs
  6. 33
      src/ImageProcessor/Imaging/TextLayer.cs
  7. 14
      src/ImageProcessor/Processors/Watermark.cs

2
src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs

@ -246,7 +246,7 @@ namespace ImageProcessor.UnitTests
Image original = (Image)imageFactory.Image.Clone();
imageFactory.Watermark(new Imaging.TextLayer
{
Font = "Arial",
FontFamily = new FontFamily("Arial"),
FontSize = 10,
Position = new Point(10, 10),
Text = "Lorem ipsum dolor"

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

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

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

@ -11,6 +11,7 @@
namespace ImageProcessor.Web.Processors
{
using System.Drawing;
using System.Drawing.Text;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
@ -53,7 +54,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for the font family attribute.
/// </summary>
private static readonly Regex FontFamilyRegex = new Regex(@"font(=|-)[^/:?#\[\]@!$&'()*%\|,;=0-9]+", RegexOptions.Compiled);
private static readonly Regex FontFamilyRegex = new Regex(@"font(family)?(=|-)[^/:?#\[\]@!$&'()*%\|,;=0-9]+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the opacity attribute.
@ -63,7 +64,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for the shadow attribute.
/// </summary>
private static readonly Regex ShadowRegex = new Regex(@"((text)?)shadow(=|-)true", RegexOptions.Compiled);
private static readonly Regex ShadowRegex = new Regex(@"((text|font|drop)?)shadow(=|-)true", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Watermark"/> class.
@ -123,14 +124,14 @@ namespace ImageProcessor.Web.Processors
{
Text = this.ParseText(queryString),
Position = this.ParsePosition(queryString),
TextColor = this.ParseColor(queryString),
FontColor = this.ParseColor(queryString),
FontSize = this.ParseFontSize(queryString),
Font = this.ParseFontFamily(queryString),
FontFamily = this.ParseFontFamily(queryString),
Style = this.ParseFontStyle(queryString),
DropShadow = this.ParseDropShadow(queryString)
};
textLayer.Opacity = this.ParseOpacity(queryString, textLayer.TextColor);
textLayer.Opacity = this.ParseOpacity(queryString, textLayer.FontColor);
this.Processor.DynamicParameter = textLayer;
}
@ -279,17 +280,17 @@ namespace ImageProcessor.Web.Processors
/// <returns>
/// The correct <see cref="T:System.String"/> containing the font family for the given string.
/// </returns>
private string ParseFontFamily(string input)
private FontFamily ParseFontFamily(string input)
{
foreach (Match match in FontFamilyRegex.Matches(input))
{
// split on font-
string font = match.Value.Split(new[] { '=', '-' })[1].Replace("+", " ");
return font;
return new FontFamily(font);
}
return string.Empty;
return new FontFamily(GenericFontFamilies.SansSerif);
}
/// <summary>

29
src/ImageProcessor/ImageFactory.cs

@ -470,7 +470,7 @@ namespace ImageProcessor
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Flip(bool flipVertically)
public ImageFactory Flip(bool flipVertically = false)
{
if (this.ShouldProcess)
{
@ -687,6 +687,33 @@ namespace ImageProcessor
return this;
}
/// <summary>
/// Adds rounded corners to the current image.
/// </summary>
/// <param name="radius">
/// The radius at which the corner will be rounded.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory RoundedCorners(int radius)
{
if (this.ShouldProcess)
{
if (radius < 0)
{
radius = 0;
}
RoundedCornerLayer roundedCornerLayer = new RoundedCornerLayer(radius);
RoundedCorners roundedCorners = new RoundedCorners { DynamicParameter = roundedCornerLayer };
this.CurrentImageFormat.ApplyProcessor(roundedCorners.ProcessImage, this);
}
return this;
}
/// <summary>
/// Adds rounded corners to the current image.
/// </summary>

2
src/ImageProcessor/Imaging/RoundedCornerLayer.cs

@ -19,7 +19,7 @@ namespace ImageProcessor.Imaging
/// Initializes a new instance of the <see cref="RoundedCornerLayer"/> class.
/// </summary>
/// <param name="radius">
/// The radius at which the corner will be done.
/// The radius at which the corner will be rounded.
/// </param>
/// <param name="topLeft">
/// Set if top left is rounded

33
src/ImageProcessor/Imaging/TextLayer.cs

@ -10,11 +10,8 @@
namespace ImageProcessor.Imaging
{
#region Using
using System;
using System.Drawing;
#endregion
using System.Drawing.Text;
/// <summary>
/// Encapsulates the properties required to add a layer of text to an image.
@ -37,6 +34,11 @@ namespace ImageProcessor.Imaging
/// </summary>
private FontStyle fontStyle = FontStyle.Regular;
/// <summary>
/// The font family to render the text.
/// </summary>
private FontFamily fontFamily = new FontFamily(GenericFontFamilies.SansSerif);
/// <summary>
/// The font size to render the text.
/// </summary>
@ -55,21 +57,28 @@ namespace ImageProcessor.Imaging
public string Text { get; set; }
/// <summary>
/// Gets or sets the Color to render the font.
/// Gets or sets the <see cref="System.Drawing.Color"/> to render the font.
/// <remarks>
/// <para>Defaults to black.</para>
/// </remarks>
/// </summary>
public Color TextColor
public Color FontColor
{
get { return this.textColor; }
set { this.textColor = value; }
}
/// <summary>
/// Gets or sets the name of the font.
/// Gets or sets the name of the font family.
/// <remarks>
/// <para>Defaults to generic sans-serif font family.</para>
/// </remarks>
/// </summary>
public string Font { get; set; }
public FontFamily FontFamily
{
get { return this.fontFamily; }
set { this.fontFamily = value; }
}
/// <summary>
/// Gets or sets the size of the font in pixels.
@ -141,8 +150,8 @@ namespace ImageProcessor.Imaging
}
return this.Text == textLayer.Text
&& this.TextColor == textLayer.TextColor
&& this.Font == textLayer.Font
&& this.FontColor == textLayer.FontColor
&& this.FontFamily.Equals(textLayer.FontFamily)
&& this.FontSize == textLayer.FontSize
&& this.Style == textLayer.Style
&& this.DropShadow == textLayer.DropShadow
@ -159,8 +168,8 @@ namespace ImageProcessor.Imaging
public override int GetHashCode()
{
return this.Text.GetHashCode() +
this.TextColor.GetHashCode() +
this.Font.GetHashCode() +
this.FontColor.GetHashCode() +
this.FontFamily.GetHashCode() +
this.FontSize.GetHashCode() +
this.Style.GetHashCode() +
this.DropShadow.GetHashCode() +

14
src/ImageProcessor/Processors/Watermark.cs

@ -75,11 +75,11 @@ namespace ImageProcessor.Processors
using (Graphics graphics = Graphics.FromImage(newImage))
{
using (Font font = this.GetFont(textLayer.Font, fontSize, fontStyle))
using (Font font = this.GetFont(textLayer.FontFamily, fontSize, fontStyle))
{
using (StringFormat drawFormat = new StringFormat())
{
using (Brush brush = new SolidBrush(Color.FromArgb(opacity, textLayer.TextColor)))
using (Brush brush = new SolidBrush(Color.FromArgb(opacity, textLayer.FontColor)))
{
Point origin = textLayer.Position;
@ -148,7 +148,7 @@ namespace ImageProcessor.Processors
/// <summary>
/// Returns the correct <see cref="T:System.Drawing.Font"/> for the given parameters.
/// </summary>
/// <param name="font">
/// <param name="fontFamily">
/// The name of the font.
/// </param>
/// <param name="fontSize">
@ -160,20 +160,20 @@ namespace ImageProcessor.Processors
/// <returns>
/// The correct <see cref="T:System.Drawing.Font"/>
/// </returns>
private Font GetFont(string font, int fontSize, FontStyle fontStyle)
private Font GetFont(FontFamily fontFamily, int fontSize, FontStyle fontStyle)
{
try
{
using (FontFamily fontFamily = new FontFamily(font))
using (fontFamily)
{
return new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel);
}
}
catch
{
using (FontFamily fontFamily = FontFamily.GenericSansSerif)
using (FontFamily genericFontFamily = FontFamily.GenericSansSerif)
{
return new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel);
return new Font(genericFontFamily, fontSize, fontStyle, GraphicsUnit.Pixel);
}
}
}

Loading…
Cancel
Save