aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Drawing/DrawingUtils.cs
diff options
context:
space:
mode:
authorOdd Stråbø <oddstr13@openshell.no>2021-03-17 00:15:09 +0100
committerOdd Stråbø <oddstr13@openshell.no>2021-04-11 08:20:47 +0200
commit383aa4e4d9cf4b0997b9277df838728102e0db3c (patch)
tree95bd4fc8273f888abc709e7915ae834c97d43c73 /MediaBrowser.Model/Drawing/DrawingUtils.cs
parent240e67d48568e1bedddd1658a68e0dac0a2f8699 (diff)
Add Resize to fill box alternative to image endpoints
Diffstat (limited to 'MediaBrowser.Model/Drawing/DrawingUtils.cs')
-rw-r--r--MediaBrowser.Model/Drawing/DrawingUtils.cs48
1 files changed, 48 insertions, 0 deletions
diff --git a/MediaBrowser.Model/Drawing/DrawingUtils.cs b/MediaBrowser.Model/Drawing/DrawingUtils.cs
index 1512c5233..8fb9bdc0a 100644
--- a/MediaBrowser.Model/Drawing/DrawingUtils.cs
+++ b/MediaBrowser.Model/Drawing/DrawingUtils.cs
@@ -58,6 +58,54 @@ namespace MediaBrowser.Model.Drawing
}
/// <summary>
+ /// Resizes to fill box.
+ /// Returns original size if both width and height are null or zero.
+ /// </summary>
+ /// <param name="size">The original size object.</param>
+ /// <param name="fillWidth">A new fixed width, if desired.</param>
+ /// <param name="fillHeight">A new fixed height, if desired.</param>
+ /// <returns>A new size object or size.</returns>
+ public static ImageDimensions ResizeFill(
+ ImageDimensions size,
+ int? fillWidth,
+ int? fillHeight)
+ {
+ // Return original size if input is invalid.
+ if (
+ (fillWidth == null && fillHeight == null)
+ || (fillWidth == 0 || fillHeight == 0))
+ {
+ return size;
+ }
+
+ if (fillWidth == null || fillWidth == 0)
+ {
+ fillWidth = 1;
+ }
+
+ if (fillHeight == null || fillHeight == 0)
+ {
+ fillHeight = 1;
+ }
+
+ double widthRatio = (double)size.Width / (double)fillWidth;
+ double heightRatio = (double)size.Height / (double)fillHeight!;
+ // min()
+ double scaleRatio = widthRatio > heightRatio ? heightRatio : widthRatio;
+
+ // Clamp to current size.
+ if (scaleRatio < 1)
+ {
+ return size;
+ }
+
+ int newWidth = Convert.ToInt32(Math.Ceiling((double)size.Width / scaleRatio));
+ int newHeight = Convert.ToInt32(Math.Ceiling((double)size.Height / scaleRatio));
+
+ return new ImageDimensions(newWidth, newHeight);
+ }
+
+ /// <summary>
/// Gets the new width.
/// </summary>
/// <param name="currentHeight">Height of the current.</param>