using System; using Newtonsoft.Json.Linq; using System.Globalization; using System.Text; using Newtonsoft.Json; using Umbraco.Core; using Umbraco.Core.Composing; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.PropertyEditors.ValueConverters; using Umbraco.Web.Models; namespace Umbraco.Web { /// /// Provides extension methods for getting ImageProcessor URL from the core Image Cropper property editor /// public static class ImageCropperTemplateExtensions { /// /// Gets the ImageProcessor URL by the crop alias (from the "umbracoFile" property alias) on the IPublishedContent item /// /// /// The IPublishedContent item. /// /// /// The crop alias e.g. thumbnail /// /// /// The ImageProcessor.Web URL. /// public static string GetCropUrl(this IPublishedContent mediaItem, string cropAlias) => ImageCropperTemplateCoreExtensions.GetCropUrl(mediaItem, cropAlias, Current.ImageUrlGenerator); /// /// Gets the ImageProcessor 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 ImageProcessor.Web URL. /// public static string GetCropUrl(this IPublishedContent mediaItem, string propertyAlias, string cropAlias) => ImageCropperTemplateCoreExtensions.GetCropUrl(mediaItem, propertyAlias, cropAlias, Current.ImageUrlGenerator); /// /// Gets the ImageProcessor 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 ImageProcessor supports. For example: /// /// /// /// /// /// Use a dimension as a ratio /// /// /// If the image should be upscaled to requested dimensions /// /// /// The . /// public static string GetCropUrl( this IPublishedContent mediaItem, 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, bool upScale = true) => ImageCropperTemplateCoreExtensions.GetCropUrl(mediaItem, Current.ImageUrlGenerator, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale); /// /// Gets the ImageProcessor 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 ImageProcessor supports. For example: /// /// /// /// /// /// Use a dimension as a ratio /// /// /// If the image should be upscaled to requested dimensions /// /// /// The . /// 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) => ImageCropperTemplateCoreExtensions.GetCropUrl(imageUrl, Current.ImageUrlGenerator, width, height, imageCropperValue, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale); /// /// Gets the ImageProcessor 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 ImageProcessor supports. For example: /// /// /// /// /// /// Use a dimension as a ratio /// /// /// If the image should be upscaled to requested dimensions /// /// /// The . /// 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) => ImageCropperTemplateCoreExtensions.GetCropUrl(imageUrl, Current.ImageUrlGenerator, cropDataSet, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale); private static readonly JsonSerializerSettings ImageCropperValueJsonSerializerSettings = new JsonSerializerSettings { Culture = CultureInfo.InvariantCulture, FloatParseHandling = FloatParseHandling.Decimal }; internal static ImageCropperValue DeserializeImageCropperValue(this string json) { ImageCropperValue imageCrops = null; if (json.DetectIsJson()) { try { imageCrops = JsonConvert.DeserializeObject(json, ImageCropperValueJsonSerializerSettings); } catch (Exception ex) { Current.Logger.Error(typeof(ImageCropperTemplateExtensions), ex, "Could not parse the json string: {Json}", json); } } imageCrops = imageCrops ?? new ImageCropperValue(); return imageCrops; } } }