using System; using System.Globalization; using Newtonsoft.Json.Linq; using Umbraco.Cms.Core; 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; namespace Umbraco.Extensions { public static class ImageCropperTemplateCoreExtensions { /// /// 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 image URL generator. /// The published value fallback. /// The published URL provider. /// /// The URL of the cropped image. /// public static string GetCropUrl( this IPublishedContent mediaItem, string cropAlias, IImageUrlGenerator imageUrlGenerator, IPublishedValueFallback publishedValueFallback, IPublishedUrlProvider publishedUrlProvider) => mediaItem.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, cropAlias: cropAlias, useCropDimensions: true); public static string GetCropUrl( this MediaWithCrops mediaWithCrops, string cropAlias, IImageUrlGenerator imageUrlGenerator, IPublishedValueFallback publishedValueFallback, IPublishedUrlProvider publishedUrlProvider) => mediaWithCrops.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, cropAlias: cropAlias, useCropDimensions: true); /// /// Gets the crop URL by using only the specified . /// /// The media item. /// The image cropper value. /// The crop alias. /// The image URL generator. /// The published value fallback. /// The published URL provider. /// /// The image crop URL. /// public static string GetCropUrl( this IPublishedContent mediaItem, ImageCropperValue imageCropperValue, string cropAlias, IImageUrlGenerator imageUrlGenerator, IPublishedValueFallback publishedValueFallback, IPublishedUrlProvider publishedUrlProvider) => mediaItem.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, imageCropperValue, true, cropAlias: cropAlias, useCropDimensions: true); /// /// 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 image URL generator. /// The published value fallback. /// The published URL provider. /// /// The URL of the cropped image. /// public static string GetCropUrl( this IPublishedContent mediaItem, string propertyAlias, string cropAlias, IImageUrlGenerator imageUrlGenerator, IPublishedValueFallback publishedValueFallback, IPublishedUrlProvider publishedUrlProvider) => mediaItem.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, propertyAlias: propertyAlias, cropAlias: cropAlias, useCropDimensions: true); public static string GetCropUrl(this MediaWithCrops mediaWithCrops, IPublishedValueFallback publishedValueFallback, IPublishedUrlProvider publishedUrlProvider, string propertyAlias, string cropAlias, IImageUrlGenerator imageUrlGenerator) => mediaWithCrops.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, propertyAlias: propertyAlias, cropAlias: cropAlias, useCropDimensions: true); /// /// Gets the underlying image processing service URL from the IPublishedContent item. /// /// The IPublishedContent item. /// The image URL generator. /// The published value fallback. /// The published URL provider. /// 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 ImageProcessor supports. For example: /// /// Use a dimension as a ratio. /// /// The URL of the cropped image. /// public static string GetCropUrl( this IPublishedContent mediaItem, IImageUrlGenerator imageUrlGenerator, IPublishedValueFallback publishedValueFallback, IPublishedUrlProvider publishedUrlProvider, 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) => mediaItem.GetCropUrl(imageUrlGenerator, publishedValueFallback, publishedUrlProvider, null, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode); public static string GetCropUrl( this MediaWithCrops mediaWithCrops, IImageUrlGenerator imageUrlGenerator, IPublishedValueFallback publishedValueFallback, IPublishedUrlProvider publishedUrlProvider, int? width = null, int? height = null, string propertyAlias = 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) { 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); } private static string GetCropUrl( this IPublishedContent mediaItem, IImageUrlGenerator imageUrlGenerator, IPublishedValueFallback publishedValueFallback, IPublishedUrlProvider publishedUrlProvider, ImageCropperValue localCrops, bool localCropsOnly, int? width = null, int? height = null, string propertyAlias = 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) { if (mediaItem == null) { throw new ArgumentNullException(nameof(mediaItem)); } if (mediaItem.HasProperty(propertyAlias) == false || mediaItem.HasValue(propertyAlias) == false) { return null; } var mediaItemUrl = mediaItem.MediaUrl(publishedUrlProvider, propertyAlias: propertyAlias); // Only get crops from media when required and used if (localCropsOnly == false && (imageCropMode == ImageCropMode.Crop || imageCropMode == null)) { // Get the default cropper value from the value converter var cropperValue = mediaItem.Value(publishedValueFallback, propertyAlias); var mediaCrops = cropperValue as ImageCropperValue; if (mediaCrops == null && cropperValue is JObject jobj) { mediaCrops = jobj.ToObject(); } if (mediaCrops == null && cropperValue is string imageCropperValue && string.IsNullOrEmpty(imageCropperValue) == false && imageCropperValue.DetectIsJson()) { mediaCrops = imageCropperValue.DeserializeImageCropperValue(); } // Merge crops if (localCrops == null) { localCrops = mediaCrops; } else if (mediaCrops != null) { localCrops = localCrops.Merge(mediaCrops); } } var cacheBusterValue = cacheBuster ? mediaItem.UpdateDate.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture) : null; return GetCropUrl( mediaItemUrl, imageUrlGenerator, localCrops, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode); } /// /// Gets the underlying image processing service URL from the image path. /// /// The image URL. /// The image URL generator. /// 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. /// /// The URL of the cropped image. /// public static string GetCropUrl( this string imageUrl, IImageUrlGenerator imageUrlGenerator, 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) { if (string.IsNullOrWhiteSpace(imageUrl)) { return null; } ImageCropperValue cropDataSet = null; if (string.IsNullOrEmpty(imageCropperValue) == false && imageCropperValue.DetectIsJson() && (imageCropMode == ImageCropMode.Crop || imageCropMode == null)) { cropDataSet = imageCropperValue.DeserializeImageCropperValue(); } return GetCropUrl( imageUrl, imageUrlGenerator, cropDataSet, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode); } /// /// Gets the underlying image processing service URL from the image path. /// /// The image URL. /// The generator that will process all the options and the image URL to return a full image URLs with all processing options appended. /// 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: /// /// Use a dimension as a ratio. /// /// The URL of the cropped image. /// public static string GetCropUrl( this string imageUrl, IImageUrlGenerator imageUrlGenerator, 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) { if (string.IsNullOrWhiteSpace(imageUrl)) { return null; } ImageUrlGenerationOptions options; if (cropDataSet != null && (imageCropMode == ImageCropMode.Crop || imageCropMode == null)) { ImageCropperValue.ImageCropperCrop crop = cropDataSet.GetCrop(cropAlias); // If a crop was specified, but not found, return null if (crop == null && !string.IsNullOrWhiteSpace(cropAlias)) { return null; } options = cropDataSet.GetCropBaseOptions(imageUrl, crop, preferFocalPoint || string.IsNullOrWhiteSpace(cropAlias)); 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) { if (width != null && height == null) { height = (int)MathF.Round(width.Value * ((float)crop.Height / crop.Width)); } else if (width == null && height != null) { width = (int)MathF.Round(height.Value * ((float)crop.Width / crop.Height)); } } } else { options = new ImageUrlGenerationOptions(imageUrl) { ImageCropMode = (imageCropMode ?? ImageCropMode.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.FurtherOptions = furtherOptions; options.CacheBusterValue = cacheBusterValue; return imageUrlGenerator.GetImageUrl(options); } } }