ImageHelper.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Model.Drawing;
  3. using MediaBrowser.Model.Entities;
  4. namespace MediaBrowser.Controller.Drawing
  5. {
  6. public static class ImageHelper
  7. {
  8. public static ImageSize GetNewImageSize(ImageProcessingOptions options, ImageSize? originalImageSize)
  9. {
  10. if (originalImageSize.HasValue)
  11. {
  12. // Determine the output size based on incoming parameters
  13. var newSize = DrawingUtils.Resize(originalImageSize.Value, options.Width, options.Height, options.MaxWidth, options.MaxHeight);
  14. return newSize;
  15. }
  16. return GetSizeEstimate(options);
  17. }
  18. private static ImageSize GetSizeEstimate(ImageProcessingOptions options)
  19. {
  20. if (options.Width.HasValue && options.Height.HasValue)
  21. {
  22. return new ImageSize(options.Width.Value, options.Height.Value);
  23. }
  24. var aspect = GetEstimatedAspectRatio(options.Image.Type, options.Item);
  25. var width = options.Width ?? options.MaxWidth;
  26. if (width.HasValue)
  27. {
  28. var heightValue = width.Value / aspect;
  29. return new ImageSize(width.Value, heightValue);
  30. }
  31. var height = options.Height ?? options.MaxHeight ?? 200;
  32. var widthValue = aspect * height;
  33. return new ImageSize(widthValue, height);
  34. }
  35. private static double GetEstimatedAspectRatio(ImageType type, IHasImages item)
  36. {
  37. switch (type)
  38. {
  39. case ImageType.Art:
  40. case ImageType.Backdrop:
  41. case ImageType.Chapter:
  42. case ImageType.Screenshot:
  43. case ImageType.Thumb:
  44. return 1.78;
  45. case ImageType.Banner:
  46. return 5.4;
  47. case ImageType.Box:
  48. case ImageType.BoxRear:
  49. case ImageType.Disc:
  50. case ImageType.Menu:
  51. return 1;
  52. case ImageType.Logo:
  53. return 2.58;
  54. case ImageType.Primary:
  55. return item.GetDefaultPrimaryImageAspectRatio() ?? .667;
  56. default:
  57. return 1;
  58. }
  59. }
  60. }
  61. }