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; using ServiceStack.ServiceHost;
namespace MediaBrowser.Api.Images 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")] [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; } 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() public ImageRequest()
{ {
EnableImageEnhancers = true; EnableImageEnhancers = true;
Format = ImageOutputFormat.Original;
} }
} }

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

@ -75,9 +75,24 @@ namespace MediaBrowser.Api.Images
cropwhitespace = Request.CropWhitespace.Value; cropwhitespace = Request.CropWhitespace.Value;
} }
return ImageProcessor.ProcessImage(Item, Request.Type, Request.Index ?? 0, OriginalImagePath, cropwhitespace, var options = new ImageProcessingOptions
OriginalImageDateModified, responseStream, Request.Width, Request.Height, Request.MaxWidth, {
Request.MaxHeight, Request.Quality, Enhancers); 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> /// <returns>System.Object.</returns>
public object Get(GetItems request) public object Get(GetItems request)
{ {
var result = GetItems(request).Result; var result = GetItems(request);
return ToOptimizedResult(result); return ToOptimizedResult(result);
} }
@ -231,7 +231,7 @@ namespace MediaBrowser.Api.UserLibrary
/// </summary> /// </summary>
/// <param name="request">The request.</param> /// <param name="request">The request.</param>
/// <returns>Task{ItemsResult}.</returns> /// <returns>Task{ItemsResult}.</returns>
private async Task<ItemsResult> GetItems(GetItems request) private ItemsResult GetItems(GetItems request)
{ {
var user = _userManager.GetUserById(request.UserId); var user = _userManager.GetUserById(request.UserId);

@ -73,22 +73,9 @@ namespace MediaBrowser.Controller.Drawing
/// <summary> /// <summary>
/// Processes the image. /// Processes the image.
/// </summary> /// </summary>
/// <param name="entity">The entity.</param> /// <param name="options">The options.</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="toStream">To stream.</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> /// <returns>Task.</returns>
Task ProcessImage(BaseItem entity, ImageType imageType, int imageIndex, string originalImagePath, bool cropWhitespace, Task ProcessImage(ImageProcessingOptions options, Stream toStream);
DateTime dateModified, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight,
int? quality, List<IImageEnhancer> enhancers);
} }
} }

@ -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> <Link>Properties\SharedVersion.cs</Link>
</Compile> </Compile>
<Compile Include="Drawing\IImageProcessor.cs" /> <Compile Include="Drawing\IImageProcessor.cs" />
<Compile Include="Drawing\ImageProcessingOptions.cs" />
<Compile Include="Dto\IDtoService.cs" /> <Compile Include="Dto\IDtoService.cs" />
<Compile Include="Entities\AdultVideo.cs" /> <Compile Include="Entities\AdultVideo.cs" />
<Compile Include="Entities\Audio\IHasAlbumArtist.cs" /> <Compile Include="Entities\Audio\IHasAlbumArtist.cs" />

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

Loading…
Cancel
Save