2
0

ImageHelper.cs 2.6 KB

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