From 261730f53000e98ec021f697add8b7eeef6a2d73 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 6 May 2021 10:06:22 +0200 Subject: [PATCH] V9: Reintroduce missing friendly get crop url extensions (#10233) * Adds missing extension methods of `GetCropUrl` that uses the service locator og inject the required services. * Xml docs updated --- .../FriendlyImageCropperTemplateExtensions.cs | 317 ++++++++++++++++++ .../ImageCropperTemplateCoreExtensions.cs | 23 +- 2 files changed, 328 insertions(+), 12 deletions(-) create mode 100644 src/Umbraco.Web.Common/Extensions/FriendlyImageCropperTemplateExtensions.cs diff --git a/src/Umbraco.Web.Common/Extensions/FriendlyImageCropperTemplateExtensions.cs b/src/Umbraco.Web.Common/Extensions/FriendlyImageCropperTemplateExtensions.cs new file mode 100644 index 0000000000..ad274e36e3 --- /dev/null +++ b/src/Umbraco.Web.Common/Extensions/FriendlyImageCropperTemplateExtensions.cs @@ -0,0 +1,317 @@ +using Microsoft.Extensions.DependencyInjection; +using Umbraco.Cms.Core.Media; +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Models.PublishedContent; +using Umbraco.Cms.Core.PropertyEditors.ValueConverters; +using Umbraco.Cms.Core.Routing; +using Umbraco.Cms.Web.Common.DependencyInjection; + +namespace Umbraco.Extensions +{ + public static class FriendlyImageCropperTemplateExtensions + { + private static IImageUrlGenerator ImageUrlGenerator { get; } = + StaticServiceProvider.Instance.GetRequiredService(); + + private static IPublishedValueFallback PublishedValueFallback { get; } = + StaticServiceProvider.Instance.GetRequiredService(); + + private static IPublishedUrlProvider PublishedUrlProvider { get; } = + StaticServiceProvider.Instance.GetRequiredService(); + + + /// + /// Gets the underlying image processing service URL by the crop alias (from the "umbracoFile" property alias) on the IPublishedContent item + /// + /// + /// The IPublishedContent item. + /// + /// + /// The crop alias e.g. thumbnail + /// + /// + /// The URL of the cropped image. + /// + public static string GetCropUrl( + this IPublishedContent mediaItem, + string cropAlias) => + mediaItem.GetCropUrl(cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider); + + /// + /// Gets the underlying image processing service URL by the crop alias using the specified property containing the image cropper Json data on the IPublishedContent item. + /// + /// + /// The IPublishedContent item. + /// + /// + /// The property alias of the property containing the Json data e.g. umbracoFile + /// + /// + /// The crop alias e.g. thumbnail + /// + /// + /// The URL of the cropped image. + /// + public static string GetCropUrl( + this IPublishedContent mediaItem, + string propertyAlias, + string cropAlias) => + mediaItem.GetCropUrl(propertyAlias, cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider); + + /// + /// Gets the underlying image processing service URL from the IPublishedContent item. + /// + /// + /// The IPublishedContent item. + /// + /// + /// The width of the output image. + /// + /// + /// The height of the output image. + /// + /// + /// Property alias of the property containing the Json data. + /// + /// + /// The crop alias. + /// + /// + /// Quality percentage of the output image. + /// + /// + /// The image crop mode. + /// + /// + /// The image crop anchor. + /// + /// + /// Use focal point, to generate an output image using the focal point instead of the predefined crop + /// + /// + /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters. + /// + /// + /// 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 + /// + /// + /// If the image should be upscaled to requested dimensions + /// + /// + /// The URL of the cropped image. + /// + public static string GetCropUrl( + this IPublishedContent mediaItem, + int? width = null, + int? height = null, + string propertyAlias = Cms.Core.Constants.Conventions.Media.File, + string cropAlias = null, + int? quality = null, + ImageCropMode? imageCropMode = null, + ImageCropAnchor? imageCropAnchor = null, + bool preferFocalPoint = false, + bool useCropDimensions = false, + bool cacheBuster = true, + string furtherOptions = null, + ImageCropRatioMode? ratioMode = null, + bool upScale = true) + => mediaItem.GetCropUrl( + ImageUrlGenerator, + PublishedValueFallback, + PublishedUrlProvider, + width, + height, + propertyAlias, + cropAlias, + quality, + imageCropMode, + imageCropAnchor, + preferFocalPoint, + useCropDimensions, + cacheBuster, + furtherOptions, + ratioMode, + upScale + ); + + /// + /// Gets the underlying image processing service URL from the image path. + /// + /// + /// The image URL. + /// + /// + /// The width of the output image. + /// + /// + /// The height of the output image. + /// + /// + /// The Json data from the Umbraco Core Image Cropper property editor + /// + /// + /// The crop alias. + /// + /// + /// Quality percentage of the output image. + /// + /// + /// The image crop mode. + /// + /// + /// The image crop anchor. + /// + /// + /// Use focal point to generate an output image using the focal point instead of the predefined crop if there is one + /// + /// + /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters + /// + /// + /// 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 + /// + /// + /// If the image should be upscaled to requested dimensions + /// + /// + /// The the URL of the cropped image. + /// + public static string GetCropUrl( + this string imageUrl, + int? width = null, + int? height = null, + string imageCropperValue = null, + string cropAlias = null, + int? quality = null, + ImageCropMode? imageCropMode = null, + ImageCropAnchor? imageCropAnchor = null, + bool preferFocalPoint = false, + bool useCropDimensions = false, + string cacheBusterValue = null, + string furtherOptions = null, + ImageCropRatioMode? ratioMode = null, + bool upScale = true) + => imageUrl.GetCropUrl( + ImageUrlGenerator, + width, + height, + imageCropperValue, + cropAlias, + quality, + imageCropMode, + imageCropAnchor, + preferFocalPoint, + useCropDimensions, + cacheBusterValue, + furtherOptions, + ratioMode, + upScale + ); + + /// + /// Gets the underlying image processing service URL from the image path. + /// + /// + /// The image URL. + /// + /// + /// + /// The width of the output image. + /// + /// + /// The height of the output image. + /// + /// + /// The crop alias. + /// + /// + /// Quality percentage of the output image. + /// + /// + /// The image crop mode. + /// + /// + /// The image crop anchor. + /// + /// + /// Use focal point to generate an output image using the focal point instead of the predefined crop if there is one + /// + /// + /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters + /// + /// + /// 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 + /// + /// + /// If the image should be upscaled to requested dimensions + /// + /// + /// The the URL of the cropped image. + /// + public static string GetCropUrl( + this string imageUrl, + ImageCropperValue cropDataSet, + int? width = null, + int? height = null, + string cropAlias = null, + int? quality = null, + ImageCropMode? imageCropMode = null, + ImageCropAnchor? imageCropAnchor = null, + bool preferFocalPoint = false, + bool useCropDimensions = false, + string cacheBusterValue = null, + string furtherOptions = null, + ImageCropRatioMode? ratioMode = null, + bool upScale = true, + string animationProcessMode = null) + => imageUrl.GetCropUrl( + ImageUrlGenerator, + cropDataSet, + width, + height, + cropAlias, + quality, imageCropMode, + imageCropAnchor, + preferFocalPoint, + useCropDimensions, + cacheBusterValue, + furtherOptions, + ratioMode, + upScale, + animationProcessMode + ); + } +} diff --git a/src/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensions.cs b/src/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensions.cs index 16d071f1ac..18a460002a 100644 --- a/src/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/ImageCropperTemplateCoreExtensions.cs @@ -12,7 +12,7 @@ namespace Umbraco.Extensions public static class ImageCropperTemplateCoreExtensions { /// - /// Gets the ImageProcessor URL by the crop alias (from the "umbracoFile" property alias) on the IPublishedContent item + /// Gets the underlying image processing service URL by the crop alias (from the "umbracoFile" property alias) on the IPublishedContent item /// /// /// The IPublishedContent item. @@ -24,7 +24,7 @@ namespace Umbraco.Extensions /// The published value fallback. /// The published url provider. /// - /// The ImageProcessor.Web URL. + /// The URL of the cropped image. /// public static string GetCropUrl( this IPublishedContent mediaItem, @@ -37,7 +37,7 @@ namespace Umbraco.Extensions } /// - /// Gets the ImageProcessor URL by the crop alias using the specified property containing the image cropper Json data on the IPublishedContent item. + /// Gets the underlying image processing service URL by the crop alias using the specified property containing the image cropper Json data on the IPublishedContent item. /// /// /// The IPublishedContent item. @@ -52,7 +52,7 @@ namespace Umbraco.Extensions /// The published value fallback. /// The published url provider. /// - /// The ImageProcessor.Web URL. + /// The URL of the cropped image. /// public static string GetCropUrl( this IPublishedContent mediaItem, @@ -66,7 +66,7 @@ namespace Umbraco.Extensions } /// - /// Gets the ImageProcessor URL from the IPublishedContent item. + /// Gets the underlying image processing service URL from the IPublishedContent item. /// /// /// The IPublishedContent item. @@ -118,9 +118,8 @@ namespace Umbraco.Extensions /// /// If the image should be upscaled to requested dimensions /// - /// - /// The . + /// The URL of the cropped image. /// public static string GetCropUrl( this IPublishedContent mediaItem, @@ -179,7 +178,7 @@ namespace Umbraco.Extensions } /// - /// Gets the ImageProcessor URL from the image path. + /// Gets the underlying image processing service URL from the image path. /// /// /// The image URL. @@ -215,7 +214,7 @@ 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: + /// These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example: /// /// /// - /// The . + /// The the URL of the cropped image. /// public static string GetCropUrl( this string imageUrl, @@ -261,7 +260,7 @@ namespace Umbraco.Extensions } /// - /// Gets the ImageProcessor URL from the image path. + /// Gets the underlying image processing service URL from the image path. /// /// /// The image URL. @@ -298,7 +297,7 @@ 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: + /// These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example: /// ///