From 8b1bd14df1bcccb4bf536a6c5a65fae765d94c4b Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Thu, 12 Aug 2021 09:03:02 +0200 Subject: [PATCH] Remove ImageCropRatioMode and clean up GetCropUrl overloads --- src/Umbraco.Core/Models/ImageCropRatioMode.cs | 8 --- ...ImageCropperTemplateCoreExtensionsTests.cs | 40 ----------- .../Umbraco.Web.Common/ImageCropperTest.cs | 10 +-- .../FriendlyImageCropperTemplateExtensions.cs | 27 +++----- .../ImageCropperTemplateCoreExtensions.cs | 69 +++++-------------- .../Extensions/UrlHelperExtensions.cs | 6 +- 6 files changed, 33 insertions(+), 127 deletions(-) delete mode 100644 src/Umbraco.Core/Models/ImageCropRatioMode.cs diff --git a/src/Umbraco.Core/Models/ImageCropRatioMode.cs b/src/Umbraco.Core/Models/ImageCropRatioMode.cs deleted file mode 100644 index 19f69cbeac..0000000000 --- a/src/Umbraco.Core/Models/ImageCropRatioMode.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Umbraco.Cms.Core.Models -{ - public enum ImageCropRatioMode - { - Width, - Height - } -} diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensionsTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensionsTests.cs index 0389c51726..2d16805a89 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensionsTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensionsTests.cs @@ -101,25 +101,6 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions y.Height == 50))); } - [Test] - public void GetCropUrl_WithCropSpecifiedAndWidthRatioModeProvidedWithHeight_CallsImageGeneratorWithCorrectParameters() - { - var imageUrl = "/test.jpg"; - Mock imageUrlGenerator = CreateMockImageUrlGenerator(); - var result = imageUrl.GetCropUrl( - imageUrlGenerator.Object, - CreateImageCropperValueWithCrops(), - imageCropMode: ImageCropMode.Crop, - cropAlias: "TestCrop", - ratioMode: ImageCropRatioMode.Width, - height: 50); - - imageUrlGenerator - .Verify(x => x.GetImageUrl( - It.Is(y => y.Width == 50 && - y.Height == 50))); - } - [Test] public void GetCropUrl_WithCropSpecifiedAndWidthRatioModeProvidedWithWidthAndHeight_CallsImageGeneratorWithCorrectParameters() { @@ -130,7 +111,6 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions CreateImageCropperValueWithCrops(), imageCropMode: ImageCropMode.Crop, cropAlias: "TestCrop", - ratioMode: ImageCropRatioMode.Width, width: 35, height: 50); @@ -140,25 +120,6 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions y.Height == 50))); } - [Test] - public void GetCropUrl_WithCropSpecifiedAndHeightRatioModeProvidedWithWidth_CallsImageGeneratorWithCorrectParameters() - { - var imageUrl = "/test.jpg"; - Mock imageUrlGenerator = CreateMockImageUrlGenerator(); - var result = imageUrl.GetCropUrl( - imageUrlGenerator.Object, - CreateImageCropperValueWithCrops(), - imageCropMode: ImageCropMode.Crop, - cropAlias: "TestCrop", - ratioMode: ImageCropRatioMode.Height, - width: 60); - - imageUrlGenerator - .Verify(x => x.GetImageUrl( - It.Is(y => y.Width == 60 && - y.Height == 60))); - } - [Test] public void GetCropUrl_WithCropSpecifiedAndHeightRatioModeProvidedWithWidthAndHeight_CallsImageGeneratorWithCorrectParameters() { @@ -169,7 +130,6 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Extensions CreateImageCropperValueWithCrops(), imageCropMode: ImageCropMode.Crop, cropAlias: "TestCrop", - ratioMode: ImageCropRatioMode.Height, width: 60, height: 40); diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/ImageCropperTest.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/ImageCropperTest.cs index 599324b040..ce5e62d799 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/ImageCropperTest.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/ImageCropperTest.cs @@ -145,7 +145,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common [Test] public void GetCropUrlFurtherOptionsTest() { - var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 200, height: 300, furtherOptions: "&filter=comic&roundedcorners=radius-26|bgcolor-fff"); + var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 200, height: 300, furtherOptions: "filter=comic&roundedcorners=radius-26|bgcolor-fff"); Assert.AreEqual(MediaPath + "?f=0.80827067669172936,0.96&w=200&h=300&filter=comic&roundedcorners=radius-26|bgcolor-fff", urlString); } @@ -176,7 +176,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common [Test] public void GetCropUrl_CropAliasHeightRatioModeTest() { - var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Thumb", useCropDimensions: true, ratioMode: ImageCropRatioMode.Height); + var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, cropAlias: "Thumb", useCropDimensions: true); Assert.AreEqual(MediaPath + "?c=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&w=100&h=100", urlString); } @@ -186,7 +186,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common [Test] public void GetCropUrl_WidthHeightRatioModeTest() { - var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, ratioMode: ImageCropRatioMode.Height); + var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150); Assert.AreEqual(MediaPath + "?f=0.80827067669172936,0.96&w=300&h=150", urlString); } @@ -196,7 +196,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common [Test] public void GetCropUrl_HeightWidthRatioModeTest() { - var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150, ratioMode: ImageCropRatioMode.Width); + var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), imageCropperValue: CropperJson1, width: 300, height: 150); Assert.AreEqual(MediaPath + "?f=0.80827067669172936,0.96&w=300&h=150", urlString); } @@ -321,7 +321,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common { var cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"" + MediaPath + "\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}"; - var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), 400, 400, cropperJson, imageCropMode: ImageCropMode.Pad, furtherOptions: "&bgcolor=fff"); + var urlString = MediaPath.GetCropUrl(new TestImageUrlGenerator(), 400, 400, cropperJson, imageCropMode: ImageCropMode.Pad, furtherOptions: "bgcolor=fff"); Assert.AreEqual(MediaPath + "?m=pad&w=400&h=400&bgcolor=fff", urlString); } diff --git a/src/Umbraco.Web.Common/Extensions/FriendlyImageCropperTemplateExtensions.cs b/src/Umbraco.Web.Common/Extensions/FriendlyImageCropperTemplateExtensions.cs index 19090ee12e..38f6f47235 100644 --- a/src/Umbraco.Web.Common/Extensions/FriendlyImageCropperTemplateExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/FriendlyImageCropperTemplateExtensions.cs @@ -82,9 +82,8 @@ namespace Umbraco.Extensions /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated. /// These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example: /// - /// Use a dimension as a ratio. /// /// The URL of the cropped image. /// @@ -100,8 +99,7 @@ namespace Umbraco.Extensions bool preferFocalPoint = false, bool useCropDimensions = false, bool cacheBuster = true, - string furtherOptions = null, - ImageCropRatioMode? ratioMode = null) + string furtherOptions = null) => mediaItem.GetCropUrl( ImageUrlGenerator, PublishedValueFallback, @@ -116,8 +114,7 @@ namespace Umbraco.Extensions preferFocalPoint, useCropDimensions, cacheBuster, - furtherOptions, - ratioMode + furtherOptions ); /// @@ -136,9 +133,8 @@ namespace Umbraco.Extensions /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated. /// These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example: /// - /// Use a dimension as a ratio. /// /// The URL of the cropped image. /// @@ -154,8 +150,7 @@ namespace Umbraco.Extensions bool preferFocalPoint = false, bool useCropDimensions = false, string cacheBusterValue = null, - string furtherOptions = null, - ImageCropRatioMode? ratioMode = null) + string furtherOptions = null) => imageUrl.GetCropUrl( ImageUrlGenerator, width, @@ -168,8 +163,7 @@ namespace Umbraco.Extensions preferFocalPoint, useCropDimensions, cacheBusterValue, - furtherOptions, - ratioMode + furtherOptions ); /// @@ -188,9 +182,8 @@ namespace Umbraco.Extensions /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated. /// These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example: /// - /// Use a dimension as a ratio /// /// The URL of the cropped image. /// @@ -206,8 +199,7 @@ namespace Umbraco.Extensions bool preferFocalPoint = false, bool useCropDimensions = false, string cacheBusterValue = null, - string furtherOptions = null, - ImageCropRatioMode? ratioMode = null) + string furtherOptions = null) => imageUrl.GetCropUrl( ImageUrlGenerator, cropDataSet, @@ -219,8 +211,7 @@ namespace Umbraco.Extensions preferFocalPoint, useCropDimensions, cacheBusterValue, - furtherOptions, - ratioMode + furtherOptions ); diff --git a/src/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensions.cs b/src/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensions.cs index a6d82e3352..ae367b1cf9 100644 --- a/src/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensions.cs @@ -103,9 +103,8 @@ namespace Umbraco.Extensions /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated. /// These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example: /// - /// Use a dimension as a ratio. /// /// The URL of the cropped image. /// @@ -124,8 +123,7 @@ namespace Umbraco.Extensions bool preferFocalPoint = false, bool useCropDimensions = false, bool cacheBuster = true, - string furtherOptions = null, - ImageCropRatioMode? ratioMode = null) => mediaItem.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, null, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode); + string furtherOptions = null) => mediaItem.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, null, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions); public static string GetCropUrl( this MediaWithCrops mediaWithCrops, @@ -142,15 +140,14 @@ namespace Umbraco.Extensions bool preferFocalPoint = false, bool useCropDimensions = false, bool cacheBuster = true, - string furtherOptions = null, - ImageCropRatioMode? ratioMode = null) + string furtherOptions = null) { if (mediaWithCrops == null) { throw new ArgumentNullException(nameof(mediaWithCrops)); } - return mediaWithCrops.Content.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, mediaWithCrops.LocalCrops, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode); + return mediaWithCrops.Content.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, mediaWithCrops.LocalCrops, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions); } private static string GetCropUrl( @@ -170,8 +167,7 @@ namespace Umbraco.Extensions bool preferFocalPoint = false, bool useCropDimensions = false, bool cacheBuster = true, - string furtherOptions = null, - ImageCropRatioMode? ratioMode = null) + string furtherOptions = null) { if (mediaItem == null) { @@ -219,7 +215,7 @@ namespace Umbraco.Extensions return GetCropUrl( mediaItemUrl, imageUrlGenerator, localCrops, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, - cacheBusterValue, furtherOptions, ratioMode); + cacheBusterValue, furtherOptions); } /// @@ -239,9 +235,8 @@ namespace Umbraco.Extensions /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated. /// These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example: /// - /// Use a dimension as a ratio. /// /// The URL of the cropped image. /// @@ -258,8 +253,7 @@ namespace Umbraco.Extensions bool preferFocalPoint = false, bool useCropDimensions = false, string cacheBusterValue = null, - string furtherOptions = null, - ImageCropRatioMode? ratioMode = null) + string furtherOptions = null) { if (string.IsNullOrWhiteSpace(imageUrl)) { @@ -274,7 +268,7 @@ namespace Umbraco.Extensions return GetCropUrl( imageUrl, imageUrlGenerator, cropDataSet, width, height, cropAlias, quality, imageCropMode, - imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode); + imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions); } /// @@ -294,9 +288,8 @@ namespace Umbraco.Extensions /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated. /// These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example: /// - /// Use a dimension as a ratio. /// /// The URL of the cropped image. /// @@ -313,8 +306,7 @@ namespace Umbraco.Extensions bool preferFocalPoint = false, bool useCropDimensions = false, string cacheBusterValue = null, - string furtherOptions = null, - ImageCropRatioMode? ratioMode = null) + string furtherOptions = null) { if (string.IsNullOrWhiteSpace(imageUrl)) { @@ -334,14 +326,14 @@ namespace Umbraco.Extensions options = cropDataSet.GetCropBaseOptions(imageUrl, crop, preferFocalPoint || string.IsNullOrWhiteSpace(cropAlias)); - if (crop != null & useCropDimensions) + if (crop != null && useCropDimensions) { width = crop.Width; height = crop.Height; } - // Calculate missing dimension if a predefined crop has been specified, there are no coordinates and no ratio mode - if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null) + // Calculate missing dimension if a predefined crop has been specified, but has no coordinates + if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null) { if (width != null && height == null) { @@ -357,41 +349,14 @@ namespace Umbraco.Extensions { options = new ImageUrlGenerationOptions(imageUrl) { - ImageCropMode = (imageCropMode ?? ImageCropMode.Pad), + ImageCropMode = (imageCropMode ?? ImageCropMode.Pad), // Not sure why we default to Pad ImageCropAnchor = imageCropAnchor }; } options.Quality = quality; - options.Width = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Width ? null : width; - options.Height = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Height ? null : height; - - if (ratioMode == ImageCropRatioMode.Width && height != null) - { - // If only height specified then assume a square - if (width == null) - { - options.Width = height; - } - else - { - options.Width = (int)MathF.Round(height.Value * ((float)width.Value / height.Value)); - } - } - - if (ratioMode == ImageCropRatioMode.Height && width != null) - { - // If only width specified then assume a square - if (height == null) - { - options.Height = width; - } - else - { - options.Height = (int)MathF.Round(width.Value * ((float)height.Value / width.Value)); - } - } - + options.Width = width; + options.Height = height; options.FurtherOptions = furtherOptions; options.CacheBusterValue = cacheBusterValue; diff --git a/src/Umbraco.Web.Common/Extensions/UrlHelperExtensions.cs b/src/Umbraco.Web.Common/Extensions/UrlHelperExtensions.cs index a6dd4ac1ca..aefd102725 100644 --- a/src/Umbraco.Web.Common/Extensions/UrlHelperExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/UrlHelperExtensions.cs @@ -246,7 +246,6 @@ namespace Umbraco.Extensions bool useCropDimensions = false, bool cacheBuster = true, string furtherOptions = null, - ImageCropRatioMode? ratioMode = null, bool htmlEncode = true) { if (mediaItem == null) @@ -255,7 +254,7 @@ namespace Umbraco.Extensions } var url = mediaItem.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode, - imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode); + imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions); return CreateHtmlString(url, htmlEncode); } @@ -272,14 +271,13 @@ namespace Umbraco.Extensions bool useCropDimensions = true, string cacheBusterValue = null, string furtherOptions = null, - ImageCropRatioMode? ratioMode = null, bool htmlEncode = true) { if (imageCropperValue == null) return HtmlString.Empty; var imageUrl = imageCropperValue.Src; var url = imageUrl.GetCropUrl(imageCropperValue, width, height, cropAlias, quality, imageCropMode, - imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode); + imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions); return CreateHtmlString(url, htmlEncode); }