2
0

ImageHelper.cs 2.6 KB

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