diff options
| author | Cody Robibero <cody@robibe.ro> | 2026-08-10 16:10:17 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-10 16:10:17 -0400 |
| commit | d0e0e291f23db227d4521012100628db422467c1 (patch) | |
| tree | 285d8e197150b399243707ee5a3aaa5f46945a50 /MediaBrowser.Model/Drawing/DrawingUtils.cs | |
| parent | 7be16e9f74b1d240ecbfe81547c7a43c9345d737 (diff) | |
| parent | 82ecfde2f7bdd9d91ec403749c124ef96f64cea3 (diff) | |
Merge pull request #17569 from vavallee/fix/17056-no-image-upscaling
Stop image endpoints from upscaling beyond the source resolution
Diffstat (limited to 'MediaBrowser.Model/Drawing/DrawingUtils.cs')
| -rw-r--r-- | MediaBrowser.Model/Drawing/DrawingUtils.cs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/MediaBrowser.Model/Drawing/DrawingUtils.cs b/MediaBrowser.Model/Drawing/DrawingUtils.cs index 2040d26bbb..1fdd6a4a49 100644 --- a/MediaBrowser.Model/Drawing/DrawingUtils.cs +++ b/MediaBrowser.Model/Drawing/DrawingUtils.cs @@ -104,6 +104,35 @@ namespace MediaBrowser.Model.Drawing } /// <summary> + /// Scales a size down uniformly until it fits inside a bounding box. + /// Returns the original size if it already fits, so this never upscales. + /// </summary> + /// <param name="size">The size object.</param> + /// <param name="boundingBox">The box the result has to fit inside.</param> + /// <returns>A new size object, or <paramref name="size"/> if it already fits.</returns> + public static ImageDimensions ScaleDownToFit(ImageDimensions size, ImageDimensions boundingBox) + { + if (size.Width <= 0 || size.Height <= 0 || boundingBox.Width <= 0 || boundingBox.Height <= 0) + { + return size; + } + + double widthRatio = size.Width / (double)boundingBox.Width; + double heightRatio = size.Height / (double)boundingBox.Height; + double scaleRatio = Math.Max(widthRatio, heightRatio); + + if (scaleRatio <= 1) + { + return size; + } + + var newWidth = Math.Clamp(Convert.ToInt32(Math.Round(size.Width / scaleRatio)), 1, boundingBox.Width); + var newHeight = Math.Clamp(Convert.ToInt32(Math.Round(size.Height / scaleRatio)), 1, boundingBox.Height); + + return new ImageDimensions(newWidth, newHeight); + } + + /// <summary> /// Gets the new width. /// </summary> /// <param name="currentHeight">Height of the current.</param> |
