diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-08-28 00:32:45 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-08-28 00:32:45 -0400 |
| commit | d2494148f78d258ea59809c3b296234bdc64d73a (patch) | |
| tree | c763b372c5ff90489a18324ca131d7712f62be1a /MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs | |
| parent | f4781b0bae28ad0c6f6d324e74c8cd0bc64a14a9 (diff) | |
Support webp image output
Diffstat (limited to 'MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs | 46 |
1 files changed, 17 insertions, 29 deletions
diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs index 803b4389f..e66899efa 100644 --- a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs +++ b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Extensions; +using Imazen.WebP; +using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; using MediaBrowser.Controller; using MediaBrowser.Controller.Drawing; @@ -210,7 +211,12 @@ namespace MediaBrowser.Server.Implementations.Drawing var newHeight = Convert.ToInt32(newSize.Height); // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here - using (var thumbnail = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppPArgb)) + // Also, Webp only supports Format32bppArgb and Format32bppRgb + var pixelFormat = options.OutputFormat == ImageOutputFormat.Webp + ? PixelFormat.Format32bppArgb + : PixelFormat.Format32bppPArgb; + + using (var thumbnail = new Bitmap(newWidth, newHeight, pixelFormat)) { // Mono throw an exeception if assign 0 to SetResolution if (originalImage.HorizontalResolution > 0 && originalImage.VerticalResolution > 0) @@ -242,8 +248,15 @@ namespace MediaBrowser.Server.Implementations.Drawing // Save to the cache location using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false)) { - // Save to the memory stream - thumbnail.Save(outputFormat, cacheFileStream, quality); + if (options.OutputFormat == ImageOutputFormat.Webp) + { + new SimpleEncoder().Encode(thumbnail, cacheFileStream, quality, false); + } + else + { + // Save to the memory stream + thumbnail.Save(outputFormat, cacheFileStream, quality); + } } return cacheFilePath; @@ -261,31 +274,6 @@ namespace MediaBrowser.Server.Implementations.Drawing } /// <summary> - /// Caches the resized image. - /// </summary> - /// <param name="cacheFilePath">The cache file path.</param> - /// <param name="bytes">The bytes.</param> - /// <returns>Task.</returns> - private async Task CacheResizedImage(string cacheFilePath, byte[] bytes) - { - try - { - Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath)); - - // Save to the cache location - using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true)) - { - // Save to the filestream - await cacheFileStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); - } - } - catch (Exception ex) - { - _logger.ErrorException("Error writing to image cache file {0}", ex, cacheFilePath); - } - } - - /// <summary> /// Sets the color of the background. /// </summary> /// <param name="graphics">The graphics.</param> |
