Merge remote-tracking branch 'upstream/master' into safeguard-invalid-provider-ids
This commit is contained in:
@@ -103,6 +103,35 @@ namespace MediaBrowser.Model.Drawing
|
||||
return new ImageDimensions(newWidth, newHeight);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
|
||||
Reference in New Issue
Block a user