2020-08-22 19:56:24 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-26 12:16:47 +00:00
|
|
|
using System;
|
2019-01-13 20:01:16 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2018-12-27 23:27:57 +00:00
|
|
|
using MediaBrowser.Model.Drawing;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Drawing
|
|
|
|
{
|
|
|
|
public static class ImageHelper
|
|
|
|
{
|
2019-01-26 12:16:47 +00:00
|
|
|
public static ImageDimensions GetNewImageSize(ImageProcessingOptions options, ImageDimensions? originalImageSize)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
if (originalImageSize.HasValue)
|
|
|
|
{
|
|
|
|
// Determine the output size based on incoming parameters
|
|
|
|
var newSize = DrawingUtils.Resize(originalImageSize.Value, options.Width ?? 0, options.Height ?? 0, options.MaxWidth ?? 0, options.MaxHeight ?? 0);
|
2021-03-16 23:15:09 +00:00
|
|
|
newSize = DrawingUtils.ResizeFill(newSize, options.FillWidth, options.FillHeight);
|
2018-12-27 23:27:57 +00:00
|
|
|
return newSize;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
return GetSizeEstimate(options);
|
|
|
|
}
|
|
|
|
|
2019-01-26 12:16:47 +00:00
|
|
|
private static ImageDimensions GetSizeEstimate(ImageProcessingOptions options)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
if (options.Width.HasValue && options.Height.HasValue)
|
|
|
|
{
|
2019-01-26 12:16:47 +00:00
|
|
|
return new ImageDimensions(options.Width.Value, options.Height.Value);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 12:16:47 +00:00
|
|
|
double aspect = GetEstimatedAspectRatio(options.Image.Type, options.Item);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2019-01-26 12:16:47 +00:00
|
|
|
int? width = options.Width ?? options.MaxWidth;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
if (width.HasValue)
|
|
|
|
{
|
2019-01-26 12:16:47 +00:00
|
|
|
int heightValue = Convert.ToInt32((double)width.Value / aspect);
|
|
|
|
return new ImageDimensions(width.Value, heightValue);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var height = options.Height ?? options.MaxHeight ?? 200;
|
2019-01-26 12:16:47 +00:00
|
|
|
int widthValue = Convert.ToInt32(aspect * height);
|
|
|
|
return new ImageDimensions(widthValue, height);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static double GetEstimatedAspectRatio(ImageType type, BaseItem item)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ImageType.Art:
|
|
|
|
case ImageType.Backdrop:
|
|
|
|
case ImageType.Chapter:
|
|
|
|
case ImageType.Screenshot:
|
|
|
|
case ImageType.Thumb:
|
|
|
|
return 1.78;
|
|
|
|
case ImageType.Banner:
|
|
|
|
return 5.4;
|
|
|
|
case ImageType.Box:
|
|
|
|
case ImageType.BoxRear:
|
|
|
|
case ImageType.Disc:
|
|
|
|
case ImageType.Menu:
|
2020-05-20 16:09:52 +00:00
|
|
|
case ImageType.Profile:
|
2018-12-27 23:27:57 +00:00
|
|
|
return 1;
|
|
|
|
case ImageType.Logo:
|
|
|
|
return 2.58;
|
|
|
|
case ImageType.Primary:
|
2019-01-28 20:55:44 +00:00
|
|
|
double defaultPrimaryImageAspectRatio = item.GetDefaultPrimaryImageAspectRatio();
|
|
|
|
return defaultPrimaryImageAspectRatio > 0 ? defaultPrimaryImageAspectRatio : 2.0 / 3;
|
2018-12-27 23:27:57 +00:00
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|