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(); Image original = (Image)imageFactory.Image.Clone();
imageFactory.Watermark(new Imaging.TextLayer imageFactory.Watermark(new Imaging.TextLayer
{ {
Font = "Arial", FontFamily = new FontFamily("Arial"),
FontSize = 10, FontSize = 10,
Position = new Point(10, 10), Position = new Point(10, 10),
Text = "Lorem ipsum dolor" Text = "Lorem ipsum dolor"

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

@ -437,27 +437,27 @@ namespace ImageProcessor.Web.UnitTests
new TextLayer new TextLayer
{ {
Text = "watermark goodness", Text = "watermark goodness",
TextColor = ColorTranslator.FromHtml("#" + "ffffff"), FontColor = ColorTranslator.FromHtml("#" + "ffffff"),
FontSize = 36, FontSize = 36,
Style = FontStyle.Italic, Style = FontStyle.Italic,
Opacity = 80, Opacity = 80,
Position = new Point(30, 150), Position = new Point(30, 150),
DropShadow = true, 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 new TextLayer
{ {
Text = "watermark goodness", Text = "watermark goodness",
TextColor = ColorTranslator.FromHtml("#" + "ffffff"), FontColor = ColorTranslator.FromHtml("#" + "ffffff"),
FontSize = 36, FontSize = 36,
Style = FontStyle.Italic, Style = FontStyle.Italic,
Opacity = 80, Opacity = 80,
Position = new Point(30, 150), Position = new Point(30, 150),
DropShadow = true, DropShadow = true,
Font = "arial" FontFamily = new FontFamily("arial")
} }
} }
}; };

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

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

29
src/ImageProcessor/ImageFactory.cs

@ -470,7 +470,7 @@ namespace ImageProcessor
/// <returns> /// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns> /// </returns>
public ImageFactory Flip(bool flipVertically) public ImageFactory Flip(bool flipVertically = false)
{ {
if (this.ShouldProcess) if (this.ShouldProcess)
{ {
@ -687,6 +687,33 @@ namespace ImageProcessor
return this; 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> /// <summary>
/// Adds rounded corners to the current image. /// Adds rounded corners to the current image.
/// </summary> /// </summary>

2
src/ImageProcessor/Imaging/RoundedCornerLayer.cs

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

33
src/ImageProcessor/Imaging/TextLayer.cs

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

14
src/ImageProcessor/Processors/Watermark.cs

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