From 44d0e30e9de8d2eef957eca1f97ee3fd5aa71810 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 17 Mar 2015 12:47:56 -0400 Subject: [PATCH] perform webp test at startup --- .../Drawing/ImageProcessor.cs | 54 +++++++++++++++---- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs index cb6058f3f1..79e5a0cf00 100644 --- a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs +++ b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs @@ -130,7 +130,44 @@ namespace MediaBrowser.Server.Implementations.Drawing public ImageFormat[] GetSupportedImageOutputFormats() { - return new[] { ImageFormat.Webp, ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png }; + if (_webpAvailable) + { + return new[] { ImageFormat.Webp, ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png }; + } + return new[] { ImageFormat.Gif, ImageFormat.Jpg, ImageFormat.Png }; + } + + private bool _webpAvailable = true; + private void TestWebp() + { + try + { + var tmpPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".webp"); + Directory.CreateDirectory(Path.GetDirectoryName(tmpPath)); + + using (var wand = new MagickWand(1, 1, new PixelWand("none", 1))) + { + wand.SaveImage(tmpPath); + } + } + catch (Exception ex) + { + _logger.ErrorException("Error loading webp: ", ex); + _webpAvailable = false; + } + } + + private void LogImageMagickVersionVersion() + { + try + { + _logger.Info("ImageMagick version: " + Wand.VersionString); + } + catch (Exception ex) + { + _logger.ErrorException("Error loading ImageMagick: ", ex); + } + TestWebp(); } public async Task ProcessImage(ImageProcessingOptions options) @@ -179,7 +216,8 @@ namespace MediaBrowser.Server.Implementations.Drawing var quality = options.Quality ?? 90; - var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.BackgroundColor); + var outputFormat = GetOutputFormat(options.OutputFormat); + var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, outputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.BackgroundColor); var semaphore = GetLock(cacheFilePath); @@ -250,16 +288,14 @@ namespace MediaBrowser.Server.Implementations.Drawing } } - private void LogImageMagickVersionVersion() + private ImageFormat GetOutputFormat(ImageFormat requestedFormat) { - try - { - _logger.Info("ImageMagick version: " + Wand.VersionString); - } - catch (Exception ex) + if (requestedFormat == ImageFormat.Webp && !_webpAvailable) { - _logger.ErrorException("Error loading ImageMagick: ", ex); + return ImageFormat.Png; } + + return requestedFormat; } ///