From 062efa35054bd8634e4d9528bd1c46f6ddca1941 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Mon, 9 Mar 2026 15:47:24 +0100 Subject: [PATCH] auto-orientation for images in strip command as exif orientation does not work in browsers with webp --- zero/Media/ImageSharp/PresetRequestParser.cs | 13 ++++++------- .../Processors/StripMetadataWebProcessor.cs | 3 +++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/zero/Media/ImageSharp/PresetRequestParser.cs b/zero/Media/ImageSharp/PresetRequestParser.cs index 606c810d..8d3eef68 100644 --- a/zero/Media/ImageSharp/PresetRequestParser.cs +++ b/zero/Media/ImageSharp/PresetRequestParser.cs @@ -55,7 +55,7 @@ public sealed class PresetRequestParser : IRequestParser } // get default processing commands (which are added to each request) from options - Dictionary defaults = _options.ImageSharp.DefaultCommands.Select(x => x.Split("=", 2)).ToDictionary(x => x[0], x => x[1]); + Dictionary defaults = _options.ImageSharp.DefaultCommands.Select(x => x.Split("=", 2)).ToDictionary(x => x[0], x => x.Length > 1 ? x[1] : null); // fall back to webp, as avif is not supported yet by ImageSharp @@ -65,12 +65,11 @@ public sealed class PresetRequestParser : IRequestParser CommandCollection filters = []; - foreach (KeyValuePair kv in defaults) + foreach ((string key, string value) in defaults) { - string key = kv.Key; if (key != "format" && key != "quality") { - filters[key] = kv.Value; + filters[key] = value; } } @@ -123,12 +122,12 @@ public sealed class PresetRequestParser : IRequestParser { string acceptKey = Microsoft.Net.Http.Headers.HeaderNames.Accept; - if (context == null || context.Request == null || !context.Request.Headers.ContainsKey(acceptKey)) + if (context?.Request == null || !context.Request.Headers.TryGetValue(acceptKey, out StringValues value)) { return FallbackFormat.None; } - string acceptHeader = context.Request.Headers[acceptKey].ToString(); + string acceptHeader = value.ToString(); if (acceptHeader.IsNullOrEmpty()) { @@ -155,7 +154,7 @@ public sealed class PresetRequestParser : IRequestParser if (startSlash < 0) { - return new(); + return []; } string preset = path.Substring(startSlash + PREFIX.Length, lastSlash - startSlash - PREFIX.Length); diff --git a/zero/Media/ImageSharp/Processors/StripMetadataWebProcessor.cs b/zero/Media/ImageSharp/Processors/StripMetadataWebProcessor.cs index be2413fc..8b51bb06 100644 --- a/zero/Media/ImageSharp/Processors/StripMetadataWebProcessor.cs +++ b/zero/Media/ImageSharp/Processors/StripMetadataWebProcessor.cs @@ -3,6 +3,7 @@ using SixLabors.ImageSharp.Web; using SixLabors.ImageSharp.Web.Commands; using SixLabors.ImageSharp.Web.Processors; using System.Globalization; +using SixLabors.ImageSharp.Processing; namespace zero.Media.ImageSharp.Processors; @@ -35,6 +36,8 @@ public class StripMetadataWebProcessor : IImageWebProcessor if (strip) { + image.Image.Mutate(x => x.AutoOrient()); + image.Image.Metadata.ExifProfile = null; image.Image.Metadata.XmpProfile = null; image.Image.Metadata.IptcProfile = null;