From ba53b47b72fe81b98e8e241f393e097271a705b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20Tolleshaug=20M=C3=B8rch?= Date: Thu, 10 Nov 2022 13:56:29 +0100 Subject: [PATCH] Ensure format processor is before quality processor. (cherry picked from commit 69c725b3ec71bd6cd67f93be4de7a666be547d24) --- .../Media/ImageSharpImageUrlGenerator.cs | 8 +++++- .../Media/ImageSharpImageUrlGeneratorTests.cs | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Web.Common/Media/ImageSharpImageUrlGenerator.cs b/src/Umbraco.Web.Common/Media/ImageSharpImageUrlGenerator.cs index d91b6706c9..b1b96ddc10 100644 --- a/src/Umbraco.Web.Common/Media/ImageSharpImageUrlGenerator.cs +++ b/src/Umbraco.Web.Common/Media/ImageSharpImageUrlGenerator.cs @@ -47,6 +47,7 @@ public class ImageSharpImageUrlGenerator : IImageUrlGenerator } var queryString = new Dictionary(); + Dictionary furtherOptions = QueryHelpers.ParseQuery(options.FurtherOptions); if (options.Crop is not null) { @@ -81,12 +82,17 @@ public class ImageSharpImageUrlGenerator : IImageUrlGenerator queryString.Add(ResizeWebProcessor.Height, options.Height?.ToString(CultureInfo.InvariantCulture)); } + if (furtherOptions.Remove(FormatWebProcessor.Format, out StringValues format)) + { + queryString.Add(FormatWebProcessor.Format, format[0]); + } + if (options.Quality is not null) { queryString.Add(QualityWebProcessor.Quality, options.Quality?.ToString(CultureInfo.InvariantCulture)); } - foreach (KeyValuePair kvp in QueryHelpers.ParseQuery(options.FurtherOptions)) + foreach (KeyValuePair kvp in furtherOptions) { queryString.Add(kvp.Key, kvp.Value); } diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Media/ImageSharpImageUrlGeneratorTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Media/ImageSharpImageUrlGeneratorTests.cs index bc2c26727f..093e9b075c 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Media/ImageSharpImageUrlGeneratorTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Media/ImageSharpImageUrlGeneratorTests.cs @@ -63,6 +63,33 @@ public class ImageSharpImageUrlGeneratorTests urlString); } + [Test] + public void GetImageUrlFurtherOptionsModeAndQualityTest() + { + var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) + { + Quality = 10, + FurtherOptions = "format=webp", + }); + Assert.AreEqual( + MediaPath + + "?format=webp&quality=10", + urlString); + } + + [Test] + public void GetImageUrlFurtherOptionsWithModeAndQualityTest() + { + var urlString = s_generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) + { + FurtherOptions = "quality=10&format=webp", + }); + Assert.AreEqual( + MediaPath + + "?format=webp&quality=10", + urlString); + } + /// /// Test that if options is null, the generated image URL is also null. ///