diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2015-10-27 22:30:19 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2015-10-27 22:30:19 -0400 |
| commit | 06efa32e8f83d46bfa482f89be49230782a3bb36 (patch) | |
| tree | e3a24fae2c67008d9e0e4630501a739da55ace8c /Emby.Drawing/ImageProcessor.cs | |
| parent | 3efc6f0005db9a98d9faa079ecdd84c6c5b48dfb (diff) | |
handle GetImageSize failure
Diffstat (limited to 'Emby.Drawing/ImageProcessor.cs')
| -rw-r--r-- | Emby.Drawing/ImageProcessor.cs | 74 |
1 files 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<ImageSize, bool> 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<ImageSize, bool>(newSize, !newSize.Equals(originalImageSize)); + } + catch + { + return new Tuple<ImageSize, bool>(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)) |
