update image processing

This commit is contained in:
Luke Pulverenti
2017-05-14 22:27:58 -04:00
parent bdc546ed85
commit 2f4f8c105e
9 changed files with 121 additions and 91 deletions

View File

@@ -227,7 +227,7 @@ namespace Emby.Drawing
originalImageSize = null;
}
var newSize = GetNewImageSize(options, originalImageSize);
var newSize = ImageHelper.GetNewImageSize(options, originalImageSize);
var quality = options.Quality;
var outputFormat = GetOutputFormat(options.SupportedOutputFormats[0]);
@@ -239,14 +239,11 @@ namespace Emby.Drawing
if (!_fileSystem.FileExists(cacheFilePath))
{
var newWidth = Convert.ToInt32(Math.Round(newSize.Width));
var newHeight = Convert.ToInt32(Math.Round(newSize.Height));
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(cacheFilePath));
var tmpPath = Path.ChangeExtension(Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N")), Path.GetExtension(cacheFilePath));
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(tmpPath));
_imageEncoder.EncodeImage(originalImagePath, tmpPath, AutoOrient(options.Item), newWidth, newHeight, quality, options, outputFormat);
_imageEncoder.EncodeImage(originalImagePath, originalImageSize, tmpPath, AutoOrient(options.Item), quality, options, outputFormat);
CopyFile(tmpPath, cacheFilePath);
return new Tuple<string, string, DateTime>(tmpPath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(tmpPath));
@@ -328,66 +325,6 @@ namespace Emby.Drawing
return MimeTypes.GetMimeType(path);
}
private ImageSize GetNewImageSize(ImageProcessingOptions options, ImageSize? originalImageSize)
{
if (originalImageSize.HasValue)
{
// Determine the output size based on incoming parameters
var newSize = DrawingUtils.Resize(originalImageSize.Value, options.Width, options.Height, options.MaxWidth, options.MaxHeight);
return newSize;
}
return GetSizeEstimate(options);
}
private ImageSize GetSizeEstimate(ImageProcessingOptions options)
{
if (options.Width.HasValue && options.Height.HasValue)
{
return new ImageSize(options.Width.Value, options.Height.Value);
}
var aspect = GetEstimatedAspectRatio(options.Image.Type, options.Item);
var width = options.Width ?? options.MaxWidth;
if (width.HasValue)
{
var heightValue = width.Value / aspect;
return new ImageSize(width.Value, heightValue);
}
var height = options.Height ?? options.MaxHeight ?? 200;
var widthValue = aspect * height;
return new ImageSize(widthValue, height);
}
private double GetEstimatedAspectRatio(ImageType type, IHasImages item)
{
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 item.GetDefaultPrimaryImageAspectRatio() ?? .667;
default:
return 1;
}
}
private ImageFormat GetOutputFormat(ImageFormat requestedFormat)
{
if (requestedFormat == ImageFormat.Webp && !_imageEncoder.SupportedOutputFormats.Contains(ImageFormat.Webp))