Browse Source

Fixing Watermark parser

Former-commit-id: 0bf67a72e64cd7f34555b0dcdbb1313e626b54d2
pull/17/head
James South 12 years ago
parent
commit
fa2aeff8f2
  1. 56
      src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs
  2. 4
      src/ImageProcessor.Web/Processors/Watermark.cs
  3. 62
      src/ImageProcessor/Imaging/TextLayer.cs
  4. 2
      src/ImageProcessor/Processors/Watermark.cs

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

@ -257,6 +257,15 @@ namespace ImageProcessor.Web.UnitTests
},
{
"height=300&mode=crop", new ResizeLayer(new Size(0, 300), ResizeMode.Crop)
},
{
"width=300&mode=crop", new ResizeLayer(new Size(300, 0), ResizeMode.Crop)
},
{
"width=600&heightratio=0.416", new ResizeLayer(new Size(600, 250))
},
{
"width=600&height=250&mode=max", new ResizeLayer(new Size(600, 250), ResizeMode.Max)
}
};
@ -414,5 +423,52 @@ namespace ImageProcessor.Web.UnitTests
Assert.AreEqual(item.Value, result);
}
}
/// <summary>
/// The watermark regex unit test.
/// </summary>
[Test]
public void TestWaterMarkRegex()
{
Dictionary<string, TextLayer> data = new Dictionary<string, TextLayer>
{
{
"watermark=text-watermark goodness,color-fff,size-36,style-italic,opacity-80,position-30,150,shadow-true,font-arial",
new TextLayer
{
Text = "watermark goodness",
TextColor = ColorTranslator.FromHtml("#" + "ffffff"),
FontSize = 36,
Style = FontStyle.Italic,
Opacity = 80,
Position = new Point(30, 150),
DropShadow = true,
Font = "arial"
}
},
{
"watermark=watermark goodness&color=fff&fontsize=36&fontstyle=italic&fontopacity=80&textposition=30,150&textshadow=true&font=arial",
new TextLayer
{
Text = "watermark goodness",
TextColor = ColorTranslator.FromHtml("#" + "ffffff"),
FontSize = 36,
Style = FontStyle.Italic,
Opacity = 80,
Position = new Point(30, 150),
DropShadow = true,
Font = "arial"
}
}
};
Processors.Watermark watermark = new Processors.Watermark();
foreach (KeyValuePair<string, TextLayer> item in data)
{
watermark.MatchRegexIndex(item.Key);
TextLayer result = watermark.Processor.DynamicParameter;
Assert.AreEqual(item.Value, result);
}
}
}
}

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

@ -308,7 +308,7 @@ namespace ImageProcessor.Web.Processors
{
if (color.A < 255)
{
return color.A;
return (color.A / 255) * 100;
}
foreach (Match match in OpacityRegex.Matches(input))
@ -317,7 +317,7 @@ namespace ImageProcessor.Web.Processors
return int.Parse(match.Value.Split(new[] { '=', '-' })[1], CultureInfo.InvariantCulture);
}
// Full opacity - matches the Textlayer default.
// Full opacity - matches the TextLayer default.
return 100;
}

62
src/ImageProcessor/Imaging/TextLayer.cs

@ -100,17 +100,8 @@ namespace ImageProcessor.Imaging
/// </summary>
public int Opacity
{
get
{
int alpha = (int)Math.Ceiling((this.opacity / 100d) * 255);
return alpha < 255 ? alpha : 255;
}
set
{
this.opacity = value;
}
get { return this.opacity; }
set { this.opacity = value; }
}
/// <summary>
@ -127,5 +118,54 @@ namespace ImageProcessor.Imaging
/// </summary>
public bool DropShadow { get; set; }
#endregion
/// <summary>
/// Returns a value that indicates whether the specified object is an
/// <see cref="TextLayer"/> object that is equivalent to
/// this <see cref="TextLayer"/> object.
/// </summary>
/// <param name="obj">
/// The object to test.
/// </param>
/// <returns>
/// True if the given object is an <see cref="TextLayer"/> object that is equivalent to
/// this <see cref="TextLayer"/> object; otherwise, false.
/// </returns>
public override bool Equals(object obj)
{
TextLayer textLayer = obj as TextLayer;
if (textLayer == null)
{
return false;
}
return this.Text == textLayer.Text
&& this.TextColor == textLayer.TextColor
&& this.Font == textLayer.Font
&& this.FontSize == textLayer.FontSize
&& this.Style == textLayer.Style
&& this.DropShadow == textLayer.DropShadow
&& this.Opacity == textLayer.Opacity
&& this.Position == textLayer.Position;
}
/// <summary>
/// Returns a hash code value that represents this object.
/// </summary>
/// <returns>
/// A hash code that represents this object.
/// </returns>
public override int GetHashCode()
{
return this.Text.GetHashCode() +
this.TextColor.GetHashCode() +
this.Font.GetHashCode() +
this.FontSize.GetHashCode() +
this.Style.GetHashCode() +
this.DropShadow.GetHashCode() +
this.Opacity.GetHashCode() +
this.Position.GetHashCode();
}
}
}

2
src/ImageProcessor/Processors/Watermark.cs

@ -69,7 +69,7 @@ namespace ImageProcessor.Processors
newImage = new Bitmap(image);
TextLayer textLayer = this.DynamicParameter;
string text = textLayer.Text;
int opacity = textLayer.Opacity;
int opacity = Math.Min((int)Math.Ceiling((textLayer.Opacity / 100f) * 255), 255);
int fontSize = textLayer.FontSize;
FontStyle fontStyle = textLayer.Style;

Loading…
Cancel
Save