diff --git a/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs b/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs index 781cd89f6..e76c7b63a 100644 --- a/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs +++ b/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); } } + + /// + /// The watermark regex unit test. + /// + [Test] + public void TestWaterMarkRegex() + { + Dictionary data = new Dictionary + { + { + "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 item in data) + { + watermark.MatchRegexIndex(item.Key); + TextLayer result = watermark.Processor.DynamicParameter; + Assert.AreEqual(item.Value, result); + } + } } } \ No newline at end of file diff --git a/src/ImageProcessor.Web/Processors/Watermark.cs b/src/ImageProcessor.Web/Processors/Watermark.cs index d87f09388..a2cc28770 100644 --- a/src/ImageProcessor.Web/Processors/Watermark.cs +++ b/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; } diff --git a/src/ImageProcessor/Imaging/TextLayer.cs b/src/ImageProcessor/Imaging/TextLayer.cs index ae72fd9ae..501bb2aa3 100644 --- a/src/ImageProcessor/Imaging/TextLayer.cs +++ b/src/ImageProcessor/Imaging/TextLayer.cs @@ -100,17 +100,8 @@ namespace ImageProcessor.Imaging /// 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; } } /// @@ -127,5 +118,54 @@ namespace ImageProcessor.Imaging /// public bool DropShadow { get; set; } #endregion + + /// + /// Returns a value that indicates whether the specified object is an + /// object that is equivalent to + /// this object. + /// + /// + /// The object to test. + /// + /// + /// True if the given object is an object that is equivalent to + /// this object; otherwise, false. + /// + 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; + } + + /// + /// Returns a hash code value that represents this object. + /// + /// + /// A hash code that represents this object. + /// + 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(); + } } } diff --git a/src/ImageProcessor/Processors/Watermark.cs b/src/ImageProcessor/Processors/Watermark.cs index e06eb812a..7e449281c 100644 --- a/src/ImageProcessor/Processors/Watermark.cs +++ b/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;