From 790a2b180ebc1c5f4481c9bcc5195a3df910c5b4 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 6 Apr 2026 22:23:46 +1000 Subject: [PATCH] Auto-orient WEBP and preserve encoder options --- .../ImageSharp.Web.Sample/Pages/Index.cshtml | 26 +++++++++++++++---- .../wwwroot/Exif5-1x.webp | 3 +++ .../Middleware/ImageSharpMiddlewareOptions.cs | 16 +++++++++++- .../Processors/QualityWebProcessor.cs | 5 +++- 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 samples/ImageSharp.Web.Sample/wwwroot/Exif5-1x.webp diff --git a/samples/ImageSharp.Web.Sample/Pages/Index.cshtml b/samples/ImageSharp.Web.Sample/Pages/Index.cshtml index d2012729..ec9405c7 100644 --- a/samples/ImageSharp.Web.Sample/Pages/Index.cshtml +++ b/samples/ImageSharp.Web.Sample/Pages/Index.cshtml @@ -125,20 +125,23 @@

ICC Profiles

+

+ Demonstrates that ICC Profiles are converted to sRGB by default ensuring correct display of images with embedded profiles. +

- issue_2723.jpg?width=2000 + issue_2723.jpg?width=1000

- +

- issue_2723.jpg?width=2000&format=webp + issue_2723.jpg?width=1000&format=webp

- +

@@ -308,7 +311,20 @@

- +
+
+

EXIF AutoOrientation

+

Demonstrates that by default EXIF orientation is normalized enabling correct display of WEBP images.

+
+

+ Exif5-1x.webp?width=128 +

+

+ +

+
+
+

EXIF Handling Portrait

Demonstrates that the middleware handles EXIF orientation correctly for portrait images.

diff --git a/samples/ImageSharp.Web.Sample/wwwroot/Exif5-1x.webp b/samples/ImageSharp.Web.Sample/wwwroot/Exif5-1x.webp new file mode 100644 index 00000000..76355e87 --- /dev/null +++ b/samples/ImageSharp.Web.Sample/wwwroot/Exif5-1x.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:299e46b87c867b8ba2f654831c96e9ced0a9801a5162f810e353d05cd6eb0568 +size 1086 diff --git a/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs b/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs index 2cc85bcb..8c5382e3 100644 --- a/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs +++ b/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs @@ -9,6 +9,7 @@ using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Formats.Webp; using SixLabors.ImageSharp.Web.Commands; +using SixLabors.ImageSharp.Web.Processors; using SixLabors.ImageSharp.Web.Providers; namespace SixLabors.ImageSharp.Web.Middleware; @@ -31,7 +32,20 @@ public class ImageSharpMiddlewareOptions return HMACUtilities.ComputeHMACSHA256(uri, secret); }; - private Func onParseCommandsAsync = _ => Task.CompletedTask; + private Func onParseCommandsAsync = context => + { + // WEBP EXIF orientation is ignored by browsers. + // https://zpl.fi/exif-orientation-in-different-formats/ + // To ensure that orientation is handled correctly for web use we transparently add the auto-orient command if it is not already present. + // See issues #304, #375, and #381 for more details. + if (!context.Commands.Contains(AutoOrientWebProcessor.AutoOrient)) + { + context.Commands.Insert(0, new KeyValuePair(AutoOrientWebProcessor.AutoOrient, bool.TrueString)); + } + + return Task.CompletedTask; + }; + private Func> onBeforeLoadAsync = (_, _) => Task.FromResult(null); private Func onBeforeSaveAsync = _ => Task.CompletedTask; private Func onProcessedAsync = _ => Task.CompletedTask; diff --git a/src/ImageSharp.Web/Processors/QualityWebProcessor.cs b/src/ImageSharp.Web/Processors/QualityWebProcessor.cs index 2400c763..271c0fae 100644 --- a/src/ImageSharp.Web/Processors/QualityWebProcessor.cs +++ b/src/ImageSharp.Web/Processors/QualityWebProcessor.cs @@ -56,7 +56,10 @@ public FormattedImage Process( Quality = quality, Interleaved = reference.Interleaved, ColorType = reference.ColorType, - SkipMetadata = reference.SkipMetadata + Progressive = reference.Progressive, + ProgressiveScans = reference.ProgressiveScans, + SkipMetadata = reference.SkipMetadata, + RestartInterval = reference.RestartInterval }; } }