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. public static IImageProcessor ImageProcessor { get; set; }
  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. 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. }