ImageHelper.cs 2.4 KB

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