Configure WebP encoder to use Lossy by default (#16769)

This greatly reduces file sizes in many cases compared to the default
Lossless setting that seems to be used for PNGs.

(cherry picked from commit ee0fd7dd5f)
This commit is contained in:
Daniël Knippers
2024-07-16 21:48:32 +02:00
committed by Sebastiaan Janssen
parent 68db079700
commit afeed54352

View File

@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.Web.Commands;
using SixLabors.ImageSharp.Web.Middleware;
using SixLabors.ImageSharp.Web.Processors;
@@ -85,5 +86,14 @@ public sealed class ConfigureImageSharpMiddlewareOptions : IConfigureOptions<Ima
return Task.CompletedTask;
};
options.Configuration.ImageFormatsManager.SetEncoder(
WebpFormat.Instance,
new WebpEncoder
{
// Default to Lossy encoding like in ImageSharp 2.x.
// ImageSharp 3.x seems to use Lossless for PNGs which creates files 10x larger than Lossy.
FileFormat = WebpFileFormatType.Lossy,
});
}
}