mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-20 00:55:13 +01:00
Support webp image output
This commit is contained in:
@@ -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;
|
||||
@@ -260,31 +273,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>
|
||||
|
||||
@@ -45,6 +45,10 @@
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Imazen.WebP, Version=0.2.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\ThirdParty\libwebp\Imazen.WebP.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Mono.Nat, Version=1.2.21.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\Mono.Nat.1.2.21.0\lib\net40\Mono.Nat.dll</HintPath>
|
||||
@@ -52,9 +56,6 @@
|
||||
<Reference Include="MoreLinq">
|
||||
<HintPath>..\packages\morelinq.1.0.16006\lib\net35\MoreLinq.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Nowin">
|
||||
<HintPath>..\ThirdParty\Nowin\Nowin.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ServiceStack.Api.Swagger">
|
||||
<HintPath>..\ThirdParty\ServiceStack\ServiceStack.Api.Swagger.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -392,6 +393,10 @@
|
||||
<EmbeddedResource Include="Localization\Ratings\ca.txt" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="..\ThirdParty\libwebp\windows\x86\libwebp.dll">
|
||||
<Link>libwebp.dll</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="..\ThirdParty\ServiceStack\swagger-ui\css\highlight.default.css">
|
||||
<Link>swagger-ui\css\highlight.default.css</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
|
||||
Reference in New Issue
Block a user