From 69c725b3ec71bd6cd67f93be4de7a666be547d24 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. --- .../Media/ImageSharpImageUrlGenerator.cs | 8 +++++- .../Media/ImageSharpImageUrlGeneratorTests.cs | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Cms.Imaging.ImageSharp/Media/ImageSharpImageUrlGenerator.cs b/src/Umbraco.Cms.Imaging.ImageSharp/Media/ImageSharpImageUrlGenerator.cs index b7560853d7..ad76603187 100644 --- a/src/Umbraco.Cms.Imaging.ImageSharp/Media/ImageSharpImageUrlGenerator.cs +++ b/src/Umbraco.Cms.Imaging.ImageSharp/Media/ImageSharpImageUrlGenerator.cs @@ -46,6 +46,7 @@ public sealed class ImageSharpImageUrlGenerator : IImageUrlGenerator } var queryString = new Dictionary(); + Dictionary furtherOptions = QueryHelpers.ParseQuery(options.FurtherOptions); if (options.Crop is not null) { @@ -80,12 +81,17 @@ public sealed 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 3a882d8b91..ca3b388e44 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. ///