ImageHelper.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 ImageSize GetNewImageSize(ImageProcessingOptions options, ImageSize? 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. public static IImageProcessor ImageProcessor { get; set; }
  20. private static ImageSize GetSizeEstimate(ImageProcessingOptions options)
  21. {
  22. if (options.Width.HasValue && options.Height.HasValue)
  23. {
  24. return new ImageSize(options.Width.Value, options.Height.Value);
  25. }
  26. var aspect = GetEstimatedAspectRatio(options.Image.Type, options.Item);
  27. var width = options.Width ?? options.MaxWidth;
  28. if (width.HasValue)
  29. {
  30. var heightValue = width.Value / aspect;
  31. return new ImageSize(width.Value, heightValue);
  32. }
  33. var height = options.Height ?? options.MaxHeight ?? 200;
  34. var widthValue = aspect * height;
  35. return new ImageSize(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. return 1;
  54. case ImageType.Logo:
  55. return 2.58;
  56. case ImageType.Primary:
  57. return item.GetDefaultPrimaryImageAspectRatio();
  58. default:
  59. return 1;
  60. }
  61. }
  62. }
  63. }