using System; 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 mode. /// /// The URL of the cropped image. /// public static string GetCropUrl( this IPublishedContent mediaItem, string cropAlias, UrlMode urlMode = UrlMode.Default) => mediaItem.GetCropUrl(cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, urlMode); /// /// Gets the underlying image processing service URL by the crop alias (from the "umbracoFile" property alias in the MediaWithCrops content item) on the MediaWithCrops item. /// /// The MediaWithCrops item. /// The crop alias e.g. thumbnail. /// The url mode. /// /// The URL of the cropped image. /// public static string GetCropUrl(this MediaWithCrops mediaWithCrops, string cropAlias, UrlMode urlMode = UrlMode.Default) => ImageCropperTemplateCoreExtensions.GetCropUrl(mediaWithCrops, cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, urlMode); /// /// Gets the crop URL by using only the specified . /// /// The media item. /// The image cropper value. /// The crop alias. /// The url mode. /// /// The image crop URL. /// public static string GetCropUrl( this IPublishedContent mediaItem, ImageCropperValue imageCropperValue, string cropAlias, UrlMode urlMode = UrlMode.Default) => ImageCropperTemplateCoreExtensions.GetCropUrl(mediaItem, imageCropperValue, cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, urlMode); /// /// 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 mode. /// /// The URL of the cropped image. /// public static string GetCropUrl( this IPublishedContent mediaItem, string propertyAlias, string cropAlias, UrlMode urlMode = UrlMode.Default) => mediaItem.GetCropUrl(propertyAlias, cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, urlMode); /// /// Gets the underlying image processing service URL by the crop alias using the specified property containing the image cropper JSON data on the MediaWithCrops content item. /// /// The MediaWithCrops item. /// The property alias of the property containing the JSON data e.g. umbracoFile. /// The crop alias e.g. thumbnail. /// The url mode. /// /// The URL of the cropped image. /// public static string GetCropUrl(this MediaWithCrops mediaWithCrops, string propertyAlias, string cropAlias, UrlMode urlMode = UrlMode.Default) => ImageCropperTemplateCoreExtensions.GetCropUrl(mediaWithCrops, propertyAlias, cropAlias, ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, urlMode); /// /// 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: /// /// The url mode. /// /// 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, UrlMode urlMode = UrlMode.Default) => mediaItem.GetCropUrl( ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, urlMode ); /// /// Gets the underlying image processing service URL from the MediaWithCrops item. /// /// The MediaWithCrops 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: /// /// The url mode. /// /// The URL of the cropped image. /// public static string GetCropUrl( this MediaWithCrops mediaWithCrops, 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, UrlMode urlMode = UrlMode.Default) => mediaWithCrops.GetCropUrl( ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, urlMode ); /// /// 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: /// /// /// 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) => imageUrl.GetCropUrl( ImageUrlGenerator, width, height, imageCropperValue, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions ); /// /// Gets the underlying image processing service URL from the image path. /// /// The image URL. /// The crop data set. /// 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: /// /// /// 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) => imageUrl.GetCropUrl( ImageUrlGenerator, cropDataSet, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions ); [Obsolete("Use GetCropUrl to merge local and media crops, get automatic cache buster value and have more parameters.")] public static string GetLocalCropUrl( this MediaWithCrops mediaWithCrops, string alias, string cacheBusterValue = null) => mediaWithCrops.LocalCrops.Src + mediaWithCrops.LocalCrops.GetCropUrl(alias, ImageUrlGenerator, cacheBusterValue: cacheBusterValue); } }