From 06efa32e8f83d46bfa482f89be49230782a3bb36 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 27 Oct 2015 22:30:19 -0400 Subject: [PATCH] handle GetImageSize failure --- Emby.Drawing/ImageProcessor.cs | 74 +++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/Emby.Drawing/ImageProcessor.cs b/Emby.Drawing/ImageProcessor.cs index 05d89406b..ec5d66cba 100644 --- a/Emby.Drawing/ImageProcessor.cs +++ b/Emby.Drawing/ImageProcessor.cs @@ -214,12 +214,11 @@ namespace Emby.Drawing dateModified = tuple.Item2; } - var originalImageSize = GetImageSize(originalImagePath, dateModified, true); + var newSizeInfo = GetNewImageSize(originalImagePath, dateModified, options); + var newSize = newSizeInfo.Item1; + var isSizeChanged = newSizeInfo.Item2; - // Determine the output size based on incoming parameters - var newSize = DrawingUtils.Resize(originalImageSize, options.Width, options.Height, options.MaxWidth, options.MaxHeight); - - if (options.HasDefaultOptionsWithoutSize(originalImagePath) && newSize.Equals(originalImageSize) && options.Enhancers.Count == 0) + if (options.HasDefaultOptionsWithoutSize(originalImagePath) && !isSizeChanged && options.Enhancers.Count == 0) { // Just spit out the original file if the new size equals the old return originalImagePath; @@ -267,6 +266,71 @@ namespace Emby.Drawing return cacheFilePath; } + private Tuple GetNewImageSize(string originalImagePath, DateTime dateModified, ImageProcessingOptions options) + { + try + { + var originalImageSize = GetImageSize(originalImagePath, dateModified, true); + + // Determine the output size based on incoming parameters + var newSize = DrawingUtils.Resize(originalImageSize, options.Width, options.Height, options.MaxWidth, options.MaxHeight); + + return new Tuple(newSize, !newSize.Equals(originalImageSize)); + } + catch + { + return new Tuple(GetSizeEstimage(options), true); + } + } + + private ImageSize GetSizeEstimage(ImageProcessingOptions options) + { + if (options.Width.HasValue && options.Height.HasValue) + { + return new ImageSize(options.Width.Value, options.Height.Value); + } + + var aspect = GetEstimatedAspectRatio(options.Image.Type); + + var width = options.Width ?? options.MaxWidth; + + if (width.HasValue) + { + var heightValue = aspect / width.Value; + return new ImageSize(width.Value, Convert.ToInt32(heightValue)); + } + + var height = options.Height ?? options.MaxHeight ?? 200; + var widthValue = aspect * height; + return new ImageSize(Convert.ToInt32(widthValue), height); + } + + private double GetEstimatedAspectRatio(ImageType type) + { + switch (type) + { + case ImageType.Art: + case ImageType.Backdrop: + case ImageType.Chapter: + case ImageType.Screenshot: + case ImageType.Thumb: + return 1.78; + case ImageType.Banner: + return 5.4; + case ImageType.Box: + case ImageType.BoxRear: + case ImageType.Disc: + case ImageType.Menu: + return 1; + case ImageType.Logo: + return 2.58; + case ImageType.Primary: + return .667; + default: + return 1; + } + } + private ImageFormat GetOutputFormat(ImageFormat requestedFormat) { if (requestedFormat == ImageFormat.Webp && !_imageEncoder.SupportedOutputFormats.Contains(ImageFormat.Webp))