aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Images
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Api/Images')
-rw-r--r--MediaBrowser.Api/Images/ImageRequest.cs7
-rw-r--r--MediaBrowser.Api/Images/ImageService.cs55
2 files changed, 54 insertions, 8 deletions
diff --git a/MediaBrowser.Api/Images/ImageRequest.cs b/MediaBrowser.Api/Images/ImageRequest.cs
index 718d5f402..cdd348bb5 100644
--- a/MediaBrowser.Api/Images/ImageRequest.cs
+++ b/MediaBrowser.Api/Images/ImageRequest.cs
@@ -1,5 +1,4 @@
-using MediaBrowser.Model.Drawing;
-using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.Entities;
using ServiceStack;
namespace MediaBrowser.Api.Images
@@ -54,7 +53,7 @@ namespace MediaBrowser.Api.Images
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 string Format { get; set; }
[ApiMember(Name = "AddPlayedIndicator", Description = "Optional. Add a played indicator", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool AddPlayedIndicator { get; set; }
@@ -71,8 +70,6 @@ namespace MediaBrowser.Api.Images
public ImageRequest()
{
EnableImageEnhancers = true;
-
- Format = ImageOutputFormat.Original;
}
}
diff --git a/MediaBrowser.Api/Images/ImageService.cs b/MediaBrowser.Api/Images/ImageService.cs
index ca54249b3..7fc43e164 100644
--- a/MediaBrowser.Api/Images/ImageService.cs
+++ b/MediaBrowser.Api/Images/ImageService.cs
@@ -542,7 +542,8 @@ namespace MediaBrowser.Api.Images
}).ToList() : new List<IImageEnhancer>();
- var contentType = GetMimeType(request.Format, imageInfo.Path);
+ var format = GetOutputFormat(request, imageInfo, supportedImageEnhancers);
+ var contentType = GetMimeType(format, imageInfo.Path);
var cacheGuid = new Guid(_imageProcessor.GetImageCacheTag(item, imageInfo, supportedImageEnhancers));
@@ -562,6 +563,7 @@ namespace MediaBrowser.Api.Images
return GetImageResult(item,
request,
imageInfo,
+ format,
supportedImageEnhancers,
contentType,
cacheDuration,
@@ -573,6 +575,7 @@ namespace MediaBrowser.Api.Images
private async Task<object> GetImageResult(IHasImages item,
ImageRequest request,
ItemImageInfo image,
+ ImageOutputFormat format,
List<IImageEnhancer> enhancers,
string contentType,
TimeSpan? cacheDuration,
@@ -598,11 +601,11 @@ namespace MediaBrowser.Api.Images
MaxWidth = request.MaxWidth,
Quality = request.Quality,
Width = request.Width,
- OutputFormat = request.Format,
AddPlayedIndicator = request.AddPlayedIndicator,
PercentPlayed = request.PercentPlayed ?? 0,
UnplayedCount = request.UnplayedCount,
- BackgroundColor = request.BackgroundColor
+ BackgroundColor = request.BackgroundColor,
+ OutputFormat = format
};
var file = await _imageProcessor.ProcessImage(options).ConfigureAwait(false);
@@ -617,6 +620,52 @@ namespace MediaBrowser.Api.Images
});
}
+ private ImageOutputFormat GetOutputFormat(ImageRequest request, ItemImageInfo image, List<IImageEnhancer> enhancers)
+ {
+ if (!string.IsNullOrWhiteSpace(request.Format))
+ {
+ ImageOutputFormat format;
+ if (Enum.TryParse(request.Format, true, out format))
+ {
+ return format;
+ }
+ }
+
+ var serverFormats = _imageProcessor.GetSupportedImageOutputFormats();
+
+ var clientFormats = GetClientSupportedFormats();
+
+ if (serverFormats.Contains(ImageOutputFormat.Webp) &&
+ clientFormats.Contains(ImageOutputFormat.Webp))
+ {
+ return ImageOutputFormat.Webp;
+ }
+
+ if (enhancers.Count > 0)
+ {
+ return ImageOutputFormat.Png;
+ }
+
+ if (string.Equals(Path.GetExtension(image.Path), ".jpg", StringComparison.OrdinalIgnoreCase) ||
+ string.Equals(Path.GetExtension(image.Path), ".jpeg", StringComparison.OrdinalIgnoreCase))
+ {
+ return ImageOutputFormat.Jpg;
+ }
+
+ // We can't predict if there will be transparency or not, so play it safe
+ return ImageOutputFormat.Png;
+ }
+
+ private ImageOutputFormat[] GetClientSupportedFormats()
+ {
+ if (Request.AcceptTypes.Contains("image/webp", StringComparer.OrdinalIgnoreCase))
+ {
+ return new[] { ImageOutputFormat.Webp, ImageOutputFormat.Jpg, ImageOutputFormat.Png };
+ }
+
+ return new[] { ImageOutputFormat.Jpg, ImageOutputFormat.Png };
+ }
+
private string GetMimeType(ImageOutputFormat format, string path)
{
if (format == ImageOutputFormat.Bmp)