Added image output format param

pull/702/head
Luke Pulverenti 11 years ago
parent 8136647a0a
commit cb839f9f25

@ -1,4 +1,5 @@
using MediaBrowser.Model.Entities;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Model.Entities;
using ServiceStack.ServiceHost;
namespace MediaBrowser.Api.Images
@ -52,9 +53,14 @@ namespace MediaBrowser.Api.Images
[ApiMember(Name = "EnableImageEnhancers", Description = "Enable or disable image enhancers such as cover art.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool EnableImageEnhancers { get; set; }
[ApiMember(Name = "Format", Description = "Determines the output foramt of the image - original,gif,jpg,png", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public ImageOutputFormat Format { get; set; }
public ImageRequest()
{
EnableImageEnhancers = true;
Format = ImageOutputFormat.Original;
}
}

@ -1,7 +1,6 @@
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;

@ -75,9 +75,24 @@ namespace MediaBrowser.Api.Images
cropwhitespace = Request.CropWhitespace.Value;
}
return ImageProcessor.ProcessImage(Item, Request.Type, Request.Index ?? 0, OriginalImagePath, cropwhitespace,
OriginalImageDateModified, responseStream, Request.Width, Request.Height, Request.MaxWidth,
Request.MaxHeight, Request.Quality, Enhancers);
var options = new ImageProcessingOptions
{
CropWhiteSpace = cropwhitespace,
Enhancers = Enhancers,
Height = Request.Height,
ImageIndex = Request.Index ?? 0,
ImageType = Request.Type,
Item = Item,
MaxHeight = Request.MaxHeight,
MaxWidth = Request.MaxWidth,
OriginalImageDateModified = OriginalImageDateModified,
OriginalImagePath = OriginalImagePath,
Quality = Request.Quality,
Width = Request.Width,
OutputFormat = Request.Format
};
return ImageProcessor.ProcessImage(options, responseStream);
}
}
}

@ -221,7 +221,7 @@ namespace MediaBrowser.Api.UserLibrary
/// <returns>System.Object.</returns>
public object Get(GetItems request)
{
var result = GetItems(request).Result;
var result = GetItems(request);
return ToOptimizedResult(result);
}
@ -231,7 +231,7 @@ namespace MediaBrowser.Api.UserLibrary
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task{ItemsResult}.</returns>
private async Task<ItemsResult> GetItems(GetItems request)
private ItemsResult GetItems(GetItems request)
{
var user = _userManager.GetUserById(request.UserId);

@ -73,22 +73,9 @@ namespace MediaBrowser.Controller.Drawing
/// <summary>
/// Processes the image.
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="imageType">Type of the image.</param>
/// <param name="imageIndex">Index of the image.</param>
/// <param name="originalImagePath">The original image path.</param>
/// <param name="cropWhitespace">if set to <c>true</c> [crop whitespace].</param>
/// <param name="dateModified">The date modified.</param>
/// <param name="options">The options.</param>
/// <param name="toStream">To stream.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="maxWidth">Width of the max.</param>
/// <param name="maxHeight">Height of the max.</param>
/// <param name="quality">The quality.</param>
/// <param name="enhancers">The enhancers.</param>
/// <returns>Task.</returns>
Task ProcessImage(BaseItem entity, ImageType imageType, int imageIndex, string originalImagePath, bool cropWhitespace,
DateTime dateModified, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight,
int? quality, List<IImageEnhancer> enhancers);
Task ProcessImage(ImageProcessingOptions options, Stream toStream);
}
}

@ -0,0 +1,46 @@
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
namespace MediaBrowser.Controller.Drawing
{
public class ImageProcessingOptions
{
public BaseItem Item { get; set; }
public ImageType ImageType { get; set; }
public int ImageIndex { get; set; }
public string OriginalImagePath { get; set; }
public DateTime OriginalImageDateModified { get; set; }
public bool CropWhiteSpace { get; set; }
public int? Width { get; set; }
public int? Height { get; set; }
public int? MaxWidth { get; set; }
public int? MaxHeight { get; set; }
public int? Quality { get; set; }
public List<IImageEnhancer> Enhancers { get; set; }
public ImageOutputFormat OutputFormat { get; set; }
}
public enum ImageOutputFormat
{
Original,
Bmp,
Gif,
Jpg,
Png
}
}

@ -72,6 +72,7 @@
<Link>Properties\SharedVersion.cs</Link>
</Compile>
<Compile Include="Drawing\IImageProcessor.cs" />
<Compile Include="Drawing\ImageProcessingOptions.cs" />
<Compile Include="Dto\IDtoService.cs" />
<Compile Include="Entities\AdultVideo.cs" />
<Compile Include="Entities\Audio\IHasAlbumArtist.cs" />

@ -1,24 +1,23 @@
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Drawing
{
@ -74,11 +73,11 @@ namespace MediaBrowser.Server.Implementations.Drawing
ImageEnhancers = enhancers.ToArray();
}
public async Task ProcessImage(BaseItem entity, ImageType imageType, int imageIndex, string originalImagePath, bool cropWhitespace, DateTime dateModified, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality, List<IImageEnhancer> enhancers)
public async Task ProcessImage(ImageProcessingOptions options, Stream toStream)
{
if (entity == null)
if (options == null)
{
throw new ArgumentNullException("entity");
throw new ArgumentNullException("options");
}
if (toStream == null)
@ -86,43 +85,31 @@ namespace MediaBrowser.Server.Implementations.Drawing
throw new ArgumentNullException("toStream");
}
if (cropWhitespace)
var originalImagePath = options.OriginalImagePath;
var dateModified = options.OriginalImageDateModified;
if (options.CropWhiteSpace)
{
originalImagePath = await GetWhitespaceCroppedImage(originalImagePath, dateModified).ConfigureAwait(false);
}
// No enhancement - don't cache
if (enhancers.Count > 0)
if (options.Enhancers.Count > 0)
{
try
{
// Enhance if we have enhancers
var ehnancedImagePath = await GetEnhancedImage(originalImagePath, dateModified, entity, imageType, imageIndex, enhancers).ConfigureAwait(false);
var tuple = await GetEnhancedImage(originalImagePath, dateModified, options.Item, options.ImageType, options.ImageIndex, options.Enhancers).ConfigureAwait(false);
// If the path changed update dateModified
if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase))
{
dateModified = File.GetLastWriteTimeUtc(ehnancedImagePath);
originalImagePath = ehnancedImagePath;
}
}
catch (Exception ex)
{
_logger.Error("Error enhancing image", ex);
}
originalImagePath = tuple.Item1;
dateModified = tuple.Item2;
}
var originalImageSize = GetImageSize(originalImagePath, dateModified);
// Determine the output size based on incoming parameters
var newSize = DrawingUtils.Resize(originalImageSize, width, height, maxWidth, maxHeight);
var newSize = DrawingUtils.Resize(originalImageSize, options.Width, options.Height, options.MaxWidth, options.MaxHeight);
if (!quality.HasValue)
{
quality = 90;
}
var quality = options.Quality ?? 90;
var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality.Value, dateModified);
var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat);
try
{
@ -188,12 +175,12 @@ namespace MediaBrowser.Server.Implementations.Drawing
thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);
var outputFormat = originalImage.RawFormat;
var outputFormat = GetOutputFormat(originalImage, options.OutputFormat);
using (var outputMemoryStream = new MemoryStream())
{
// Save to the memory stream
thumbnail.Save(outputFormat, outputMemoryStream, quality.Value);
thumbnail.Save(outputFormat, outputMemoryStream, quality);
var bytes = outputMemoryStream.ToArray();
@ -217,6 +204,29 @@ namespace MediaBrowser.Server.Implementations.Drawing
}
}
/// <summary>
/// Gets the output format.
/// </summary>
/// <param name="image">The image.</param>
/// <param name="outputFormat">The output format.</param>
/// <returns>ImageFormat.</returns>
private ImageFormat GetOutputFormat(Image image, ImageOutputFormat outputFormat)
{
switch (outputFormat)
{
case ImageOutputFormat.Bmp:
return ImageFormat.Bmp;
case ImageOutputFormat.Gif:
return ImageFormat.Gif;
case ImageOutputFormat.Jpg:
return ImageFormat.Jpeg;
case ImageOutputFormat.Png:
return ImageFormat.Png;
default:
return image.RawFormat;
}
}
/// <summary>
/// Crops whitespace from an image, caches the result, and returns the cached path
/// </summary>
@ -317,7 +327,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
/// <param name="dateModified">The last modified date of the image</param>
/// <returns>System.String.</returns>
private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified)
private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageOutputFormat format)
{
var filename = originalPath;
@ -329,6 +339,11 @@ namespace MediaBrowser.Server.Implementations.Drawing
filename += "datemodified=" + dateModified.Ticks;
if (format != ImageOutputFormat.Original)
{
filename += "format=" + format;
}
return GetCachePath(_resizedImageCachePath, filename, Path.GetExtension(originalPath));
}
@ -452,7 +467,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
var dateModified = item.GetImageDateModified(imagePath);
var supportedEnhancers = GetSupportedEnhancers(item, imageType).ToList();
var supportedEnhancers = GetSupportedEnhancers(item, imageType);
return GetImageCacheTag(item, imageType, imagePath, dateModified, supportedEnhancers);
}
@ -491,39 +506,29 @@ namespace MediaBrowser.Server.Implementations.Drawing
return string.Join("|", cacheKeys.ToArray()).GetMD5();
}
/// <summary>
/// Gets the enhanced image.
/// </summary>
/// <param name="originalImagePath">The original image path.</param>
/// <param name="dateModified">The date modified.</param>
/// <param name="item">The item.</param>
/// <param name="imageType">Type of the image.</param>
/// <param name="imageIndex">Index of the image.</param>
/// <returns>Task{System.String}.</returns>
/// <exception cref="System.ArgumentNullException">item</exception>
public Task<string> GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item, ImageType imageType, int imageIndex)
private async Task<Tuple<string,DateTime>> GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item,
ImageType imageType, int imageIndex,
List<IImageEnhancer> enhancers)
{
if (item == null)
try
{
throw new ArgumentNullException("item");
}
// Enhance if we have enhancers
var ehnancedImagePath = await GetEnhancedImageInternal(originalImagePath, dateModified, item, imageType, imageIndex, enhancers).ConfigureAwait(false);
var supportedImageEnhancers = ImageEnhancers.Where(i =>
{
try
// If the path changed update dateModified
if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase))
{
return i.Supports(item, imageType);
}
catch (Exception ex)
{
_logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
dateModified = File.GetLastWriteTimeUtc(ehnancedImagePath);
return false;
return new Tuple<string, DateTime>(ehnancedImagePath, dateModified);
}
}
catch (Exception ex)
{
_logger.Error("Error enhancing image", ex);
}
}).ToList();
return GetEnhancedImage(originalImagePath, dateModified, item, imageType, imageIndex, supportedImageEnhancers);
return new Tuple<string, DateTime>(originalImagePath, dateModified);
}
/// <summary>
@ -537,7 +542,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
/// <param name="supportedEnhancers">The supported enhancers.</param>
/// <returns>System.String.</returns>
/// <exception cref="System.ArgumentNullException">originalImagePath</exception>
public async Task<string> GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item, ImageType imageType, int imageIndex, List<IImageEnhancer> supportedEnhancers)
private async Task<string> GetEnhancedImageInternal(string originalImagePath, DateTime dateModified, BaseItem item, ImageType imageType, int imageIndex, List<IImageEnhancer> supportedEnhancers)
{
if (string.IsNullOrEmpty(originalImagePath))
{

Loading…
Cancel
Save