Skip to content

Commit

Permalink
Fixing Watermark parser
Browse files Browse the repository at this point in the history
Former-commit-id: 4e64fb4957266ac066a48c2bd6598ff1185f6f79
  • Loading branch information
JimBobSquarePants committed Aug 18, 2014
1 parent e92ebb8 commit 7b6ecb9
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 14 deletions.
56 changes: 56 additions & 0 deletions src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ public void TestResizeRegex()
},
{
"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)
}
};

Expand Down Expand Up @@ -414,5 +423,52 @@ public void TestVignetteRegex()
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 changes: 2 additions & 2 deletions src/ImageProcessor.Web/Processors/Watermark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private int ParseOpacity(string input, Color color)
{
if (color.A < 255)
{
return color.A;
return (color.A / 255) * 100;
}

foreach (Match match in OpacityRegex.Matches(input))
Expand All @@ -317,7 +317,7 @@ private int ParseOpacity(string input, Color color)
return int.Parse(match.Value.Split(new[] { '=', '-' })[1], CultureInfo.InvariantCulture);
}

// Full opacity - matches the Textlayer default.
// Full opacity - matches the TextLayer default.
return 100;
}

Expand Down
62 changes: 51 additions & 11 deletions src/ImageProcessor/Imaging/TextLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,8 @@ public FontStyle Style
/// </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>
Expand All @@ -127,5 +118,54 @@ public Point Position
/// </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 changes: 1 addition & 1 deletion src/ImageProcessor/Processors/Watermark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Image ProcessImage(ImageFactory factory)
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;

Expand Down

0 comments on commit 7b6ecb9

Please sign in to comment.