/// <summary> /// Gets the underlying image processing service URL from the MediaWithCrops item. /// </summary> /// <param name="mediaWithCrops">The MediaWithCrops item.</param> /// <param name="imageUrlGenerator">The image URL generator.</param> /// <param name="publishedValueFallback">The published value fallback.</param> /// <param name="publishedUrlProvider">The published URL provider.</param> /// <param name="width">The width of the output image.</param> /// <param name="height">The height of the output image.</param> /// <param name="propertyAlias">Property alias of the property containing the JSON data.</param> /// <param name="cropAlias">The crop alias.</param> /// <param name="quality">Quality percentage of the output image.</param> /// <param name="imageCropMode">The image crop mode.</param> /// <param name="imageCropAnchor">The image crop anchor.</param> /// <param name="preferFocalPoint">Use focal point, to generate an output image using the focal point instead of the predefined crop.</param> /// <param name="useCropDimensions">Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters.</param> /// <param name="cacheBuster">Add a serialized date of the last edit of the item to ensure client cache refresh when updated.</param> /// <param name="furtherOptions">These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example: /// <example><![CDATA[ /// furtherOptions: "bgcolor=fff" /// ]]></example></param> /// <param name="urlMode">The url mode.</param> /// <returns> /// The URL of the cropped image. /// </returns> 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, UrlMode urlMode = UrlMode.Default) { 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, urlMode)); }
/// <summary> /// Gets the ImageProcessor Url from the IPublishedContent item. /// </summary> /// <param name="mediaItem"> /// The IPublishedContent item. /// </param> /// <param name="width"> /// The width of the output image. /// </param> /// <param name="height"> /// The height of the output image. /// </param> /// <param name="propertyAlias"> /// Property alias of the property containing the Json data. /// </param> /// <param name="cropAlias"> /// The crop alias. /// </param> /// <param name="quality"> /// Quality percentage of the output image. /// </param> /// <param name="imageCropMode"> /// The image crop mode. /// </param> /// <param name="imageCropAnchor"> /// The image crop anchor. /// </param> /// <param name="preferFocalPoint"> /// Use focal point, to generate an output image using the focal point instead of the predefined crop /// </param> /// <param name="useCropDimensions"> /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters. /// </param> /// <param name="cacheBuster"> /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated /// </param> /// <param name="furtherOptions"> /// These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example: /// <example> /// <![CDATA[ /// furtherOptions: "&bgcolor=fff" /// ]]> /// </example> /// </param> /// <param name="ratioMode"> /// Use a dimension as a ratio /// </param> /// <param name="upScale"> /// If the image should be upscaled to requested dimensions /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public static string GetCropUrl( this IPublishedContent mediaItem, IImageUrlGenerator imageUrlGenerator, 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) { if (mediaItem == null) { throw new ArgumentNullException("mediaItem"); } var cacheBusterValue = cacheBuster ? mediaItem.UpdateDate.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture) : null; if (mediaItem.HasProperty(propertyAlias) == false || mediaItem.HasValue(propertyAlias) == false) { return(string.Empty); } var mediaItemUrl = mediaItem.MediaUrl(propertyAlias: propertyAlias); //get the default obj from the value converter var cropperValue = mediaItem.Value(propertyAlias); //is it strongly typed? var stronglyTyped = cropperValue as ImageCropperValue; if (stronglyTyped != null) { return(GetCropUrl( mediaItemUrl, imageUrlGenerator, stronglyTyped, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale)); } //this shouldn't be the case but we'll check var jobj = cropperValue as JObject; if (jobj != null) { stronglyTyped = jobj.ToObject <ImageCropperValue>(); return(GetCropUrl( mediaItemUrl, imageUrlGenerator, stronglyTyped, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale)); } //it's a single string return(GetCropUrl( mediaItemUrl, imageUrlGenerator, width, height, mediaItemUrl, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale)); }
/// <summary> /// Gets the ImageProcessor Url from the image path. /// </summary> /// <param name="mediaItem"> /// The IPublishedContent item. /// </param> /// <param name="width"> /// The width of the output image. /// </param> /// <param name="height"> /// The height of the output image. /// </param> /// <param name="propertyAlias"> /// Property alias of the property containing the Json data. /// </param> /// <param name="cropAlias"> /// The crop alias. /// </param> /// <param name="quality"> /// Quality percentage of the output image. /// </param> /// <param name="imageCropMode"> /// The image crop mode. /// </param> /// <param name="imageCropAnchor"> /// The image crop anchor. /// </param> /// <param name="preferFocalPoint"> /// Use focal point to generate an output image using the focal point instead of the predefined crop if there is one /// </param> /// <param name="useCropDimensions"> /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters /// </param> /// <param name="cacheBuster"> /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated /// </param> /// <param name="furtherOptions"> /// These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example: /// <example> /// <![CDATA[ /// furtherOptions: "&bgcolor=fff" /// ]]> /// </example> /// </param> /// <param name="ratioMode"> /// Use a dimension as a ratio /// </param> /// <param name="upScale"> /// If the image should be upscaled to requested dimensions /// </param> /// <param name="urlHelper"></param> /// <param name="htmlEncode"> /// Whether to HTML encode this URL - default is true - w3c standards require HTML attributes to be HTML encoded but this can be /// set to false if using the result of this method for CSS. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public static IHtmlString GetCropUrl(this UrlHelper urlHelper, IPublishedContent mediaItem, int?width = null, int?height = null, string propertyAlias = Umbraco.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, bool htmlEncode = true) { if (mediaItem == null) { return(EmptyHtmlString); } var url = mediaItem.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale); return(htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(url)) : new HtmlString(url)); }
public static IHtmlString GetCropCdnUrl(this UrlHelper urlHelper, ImageCropDataSet imageCropper, int?width = null, int?height = null, string propertyAlias = global::Umbraco.Core.Constants.Conventions.Media.File, 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, bool htmlEncode = true ) { // if no cacheBusterValue provided we need to make one if (cacheBusterValue == null) { cacheBusterValue = DateTime.UtcNow.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture); } var cropUrl = GetCropUrl(imageCropper.Src, imageCropper, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale); return(UrlToCdnUrl(cropUrl, htmlEncode)); }
/// <summary> /// Gets the ImageProcessor Url from the image path. /// </summary> /// <param name="imageUrl"> /// The image url. /// </param> /// <param name="width"> /// The width of the output image. /// </param> /// <param name="height"> /// The height of the output image. /// </param> /// <param name="imageCropperValue"> /// The Json data from the Umbraco Core Image Cropper property editor /// </param> /// <param name="cropAlias"> /// The crop alias. /// </param> /// <param name="quality"> /// Quality percentage of the output image. /// </param> /// <param name="imageCropMode"> /// The image crop mode. /// </param> /// <param name="imageCropAnchor"> /// The image crop anchor. /// </param> /// <param name="preferFocalPoint"> /// Use focal point to generate an output image using the focal point instead of the predefined crop if there is one /// </param> /// <param name="useCropDimensions"> /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters /// </param> /// <param name="cacheBusterValue"> /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated /// </param> /// <param name="furtherOptions"> /// These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example: /// <example> /// <![CDATA[ /// furtherOptions: "&bgcolor=fff" /// ]]> /// </example> /// </param> /// <param name="ratioMode"> /// Use a dimension as a ratio /// </param> /// <param name="upScale"> /// If the image should be upscaled to requested dimensions /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> 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, bool upScale = true) { if (string.IsNullOrEmpty(imageUrl)) { return(string.Empty); } 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, upScale)); }
/// <summary> /// Gets the underlying image processing service URL from the image path. /// </summary> /// <param name="imageUrl">The image URL.</param> /// <param name="cropDataSet">The crop data set.</param> /// <param name="width">The width of the output image.</param> /// <param name="height">The height of the output image.</param> /// <param name="cropAlias">The crop alias.</param> /// <param name="quality">Quality percentage of the output image.</param> /// <param name="imageCropMode">The image crop mode.</param> /// <param name="imageCropAnchor">The image crop anchor.</param> /// <param name="preferFocalPoint">Use focal point to generate an output image using the focal point instead of the predefined crop if there is one.</param> /// <param name="useCropDimensions">Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters.</param> /// <param name="cacheBusterValue">Add a serialized date of the last edit of the item to ensure client cache refresh when updated.</param> /// <param name="furtherOptions">These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example: /// <example><![CDATA[ /// furtherOptions: "bgcolor=fff" /// ]]></example></param> /// <returns> /// The URL of the cropped image. /// </returns> 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 );
/// <summary> /// Gets the underlying image processing service URL from the MediaWithCrops item. /// </summary> /// <param name="mediaWithCrops">The MediaWithCrops item.</param> /// <param name="width">The width of the output image.</param> /// <param name="height">The height of the output image.</param> /// <param name="propertyAlias">Property alias of the property containing the JSON data.</param> /// <param name="cropAlias">The crop alias.</param> /// <param name="quality">Quality percentage of the output image.</param> /// <param name="imageCropMode">The image crop mode.</param> /// <param name="imageCropAnchor">The image crop anchor.</param> /// <param name="preferFocalPoint">Use focal point, to generate an output image using the focal point instead of the predefined crop.</param> /// <param name="useCropDimensions">Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters.</param> /// <param name="cacheBuster">Add a serialized date of the last edit of the item to ensure client cache refresh when updated.</param> /// <param name="furtherOptions">These are any query string parameters (formatted as query strings) that the underlying image processing service supports. For example: /// <example><![CDATA[ /// furtherOptions: "bgcolor=fff" /// ]]></example></param> /// <param name="urlMode">The url mode.</param> /// <returns> /// The URL of the cropped image. /// </returns> 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 );
public static IHtmlContent GetCropUrl(this IUrlHelper urlHelper, ImageCropperValue imageCropperValue, string cropAlias, int?width = null, int?height = null, int?quality = null, ImageCropMode?imageCropMode = null, ImageCropAnchor?imageCropAnchor = null, bool preferFocalPoint = false, bool useCropDimensions = true, string?cacheBusterValue = null, string?furtherOptions = 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); return(CreateHtmlString(url, htmlEncode)); }
public static IHtmlContent GetCropUrl(this IUrlHelper urlHelper, 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, bool htmlEncode = true, UrlMode urlMode = UrlMode.Default) { if (mediaItem == null) { return(HtmlString.Empty); } var url = mediaItem.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, urlMode); return(CreateHtmlString(url, htmlEncode)); }
public static IHtmlString GetCropCdnUrl(this UrlHelper urlHelper, IPublishedContent mediaItem, int?width = null, int?height = null, string propertyAlias = global::Umbraco.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, bool htmlEncode = true ) { var cropUrl = urlHelper.GetCropUrl(mediaItem, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale, false) .ToString(); return(UrlToCdnUrl(cropUrl, htmlEncode)); }
public static IHtmlString GetCropUrl(this UrlHelper urlHelper, ImageCropperValue imageCropperValue, 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, bool htmlEncode = true) { if (imageCropperValue == null) { return(EmptyHtmlString); } var imageUrl = imageCropperValue.Src; var url = imageUrl.GetCropUrl(imageCropperValue, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale); return(htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(url)) : new HtmlString(url)); }
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, bool resolveCdnPath = false) { var cropUrl = ImageCropperTemplateExtensions.GetCropUrl(mediaItem, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale); var cachePrefix = "AzureBlobCache_"; var cacheKey = $"{cachePrefix}{cropUrl}"; if (resolveCdnPath) { var currentDomain = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); var absoluteCropPath = $"{currentDomain}{cropUrl}"; var runtimeCache = ApplicationContext.Current.ApplicationCache.RuntimeCache; var cachedItem = runtimeCache.GetCacheItem <CachedImage>(cacheKey); if (cachedItem == null) { var newCachedImage = new CachedImage { WebUrl = cropUrl }; var request = (HttpWebRequest)WebRequest.Create(absoluteCropPath); request.Method = "HEAD"; using (var response = (HttpWebResponse)request.GetResponse()) { var responseCode = response.StatusCode; if (responseCode.Equals(HttpStatusCode.OK)) { newCachedImage.CacheUrl = response.ResponseUri.AbsoluteUri; runtimeCache.InsertCacheItem <CachedImage>(cacheKey, () => newCachedImage); return(response.ResponseUri.AbsoluteUri); } } } return(cachedItem.CacheUrl); } return(cropUrl); }
/// <summary> /// Gets the ImageProcessor URL from the IPublishedContent item. /// </summary> /// <param name="mediaItem"> /// The IPublishedContent item. /// </param> /// <param name="width"> /// The width of the output image. /// </param> /// <param name="height"> /// The height of the output image. /// </param> /// <param name="propertyAlias"> /// Property alias of the property containing the Json data. /// </param> /// <param name="cropAlias"> /// The crop alias. /// </param> /// <param name="quality"> /// Quality percentage of the output image. /// </param> /// <param name="imageCropMode"> /// The image crop mode. /// </param> /// <param name="imageCropAnchor"> /// The image crop anchor. /// </param> /// <param name="preferFocalPoint"> /// Use focal point, to generate an output image using the focal point instead of the predefined crop /// </param> /// <param name="useCropDimensions"> /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters. /// </param> /// <param name="cacheBuster"> /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated /// </param> /// <param name="furtherOptions"> /// These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example: /// <example> /// <![CDATA[ /// furtherOptions: "&bgcolor=fff" /// ]]> /// </example> /// </param> /// <param name="ratioMode"> /// Use a dimension as a ratio /// </param> /// <param name="upScale"> /// If the image should be upscaled to requested dimensions /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> 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);
/// <summary> /// Gets the ImageProcessor URL from the image path. /// </summary> /// <param name="imageUrl"> /// The image URL. /// </param> /// <param name="cropDataSet"></param> /// <param name="width"> /// The width of the output image. /// </param> /// <param name="height"> /// The height of the output image. /// </param> /// <param name="cropAlias"> /// The crop alias. /// </param> /// <param name="quality"> /// Quality percentage of the output image. /// </param> /// <param name="imageCropMode"> /// The image crop mode. /// </param> /// <param name="imageCropAnchor"> /// The image crop anchor. /// </param> /// <param name="preferFocalPoint"> /// Use focal point to generate an output image using the focal point instead of the predefined crop if there is one /// </param> /// <param name="useCropDimensions"> /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters /// </param> /// <param name="cacheBusterValue"> /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated /// </param> /// <param name="furtherOptions"> /// These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example: /// <example> /// <![CDATA[ /// furtherOptions: "&bgcolor=fff" /// ]]> /// </example> /// </param> /// <param name="ratioMode"> /// Use a dimension as a ratio /// </param> /// <param name="upScale"> /// If the image should be upscaled to requested dimensions /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> 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);
/// <summary> /// Gets the ImageProcessor Url from the IPublishedContent item. /// </summary> /// <param name="mediaItem"> /// The IPublishedContent item. /// </param> /// <param name="width"> /// The width of the output image. /// </param> /// <param name="height"> /// The height of the output image. /// </param> /// <param name="propertyAlias"> /// Property alias of the property containing the Json data. /// </param> /// <param name="cropAlias"> /// The crop alias. /// </param> /// <param name="quality"> /// Quality percentage of the output image. /// </param> /// <param name="imageCropMode"> /// The image crop mode. /// </param> /// <param name="imageCropAnchor"> /// The image crop anchor. /// </param> /// <param name="preferFocalPoint"> /// Use focal point, to generate an output image using the focal point instead of the predefined crop /// </param> /// <param name="useCropDimensions"> /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters>. /// </param> /// <param name="cacheBuster"> /// Add a serialised date of the last edit of the item to ensure client cache refresh when updated /// </param> /// <param name="furtherOptions"> /// The further options. /// </param> /// <param name="ratioMode"> /// Use a dimension as a ratio /// </param> /// <param name="upScale"> /// If the image should be upscaled to requested dimensions /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> 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) { string imageCropperValue = null; string mediaItemUrl; if (mediaItem.HasProperty(propertyAlias) && mediaItem.HasValue(propertyAlias)) { imageCropperValue = mediaItem.GetPropertyValue <string>(propertyAlias); // get the raw value (this will be json) var urlValue = mediaItem.GetPropertyValue <string>(propertyAlias); mediaItemUrl = urlValue.DetectIsJson() ? urlValue.SerializeToCropDataSet().Src : urlValue; } else { mediaItemUrl = mediaItem.Url; } var cacheBusterValue = cacheBuster ? mediaItem.UpdateDate.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture) : null; return(mediaItemUrl != null ? GetCropUrl(mediaItemUrl, width, height, imageCropperValue, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale) : string.Empty); }
/// <summary> /// Gets the ImageProcessor Url of a media item /// </summary> /// <param name="htmlHelper"></param> /// <param name="mediaItem"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="propertyAlias"></param> /// <param name="cropAlias"></param> /// <param name="quality"></param> /// <param name="imageCropMode"></param> /// <param name="imageCropAnchor"></param> /// <param name="preferFocalPoint"></param> /// <param name="useCropDimensions"></param> /// <param name="cacheBuster"></param> /// <param name="furtherOptions"></param> /// <param name="ratioMode"></param> /// <param name="upScale"></param> /// <returns></returns> public static IHtmlString GetCropUrl(this HtmlHelper htmlHelper, 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) { return (new HtmlString(mediaItem.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale))); }
/// <summary> /// Gets the ImageProcessor Url from the media path /// </summary> /// <param name="htmlHelper"></param> /// <param name="imageUrl"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="imageCropperValue"></param> /// <param name="cropAlias"></param> /// <param name="quality"></param> /// <param name="imageCropMode"></param> /// <param name="imageCropAnchor"></param> /// <param name="preferFocalPoint"></param> /// <param name="useCropDimensions"></param> /// <param name="cacheBusterValue"></param> /// <param name="furtherOptions"></param> /// <param name="ratioMode"></param> /// <param name="upScale"></param> /// <returns></returns> public static IHtmlString GetCropUrl(this HtmlHelper htmlHelper, 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) { return (new HtmlString(imageUrl.GetCropUrl(width, height, imageCropperValue, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale))); }
public static string GetCropUrl( this MediaWithCrops mediaWithCrops, IImageUrlGenerator imageUrlGenerator, 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) { if (mediaWithCrops == null) { throw new ArgumentNullException(nameof(mediaWithCrops)); } return(mediaWithCrops.Content.GetCropUrl(imageUrlGenerator, mediaWithCrops.LocalCrops, false, width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale)); }
public static IHtmlString GetCropUrl <T>(this UrlHelper urlHelper, string imageUrl, T cropAlias, int?width = null, int?height = null, string imageCropperValue = 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) where T : struct { var url = imageUrl.GetCropUrl(cropAlias, width, height, imageCropperValue, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale); return(new HtmlString(url)); }
/// <summary> /// Gets the ImageProcessor Url from the image path. /// </summary> /// <param name="imageUrl"> /// The image url. /// </param> /// <param name="cropDataSet"></param> /// <param name="width"> /// The width of the output image. /// </param> /// <param name="height"> /// The height of the output image. /// </param> /// <param name="cropAlias"> /// The crop alias. /// </param> /// <param name="quality"> /// Quality percentage of the output image. /// </param> /// <param name="imageCropMode"> /// The image crop mode. /// </param> /// <param name="imageCropAnchor"> /// The image crop anchor. /// </param> /// <param name="preferFocalPoint"> /// Use focal point to generate an output image using the focal point instead of the predefined crop if there is one /// </param> /// <param name="useCropDimensions"> /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters /// </param> /// <param name="cacheBusterValue"> /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated /// </param> /// <param name="furtherOptions"> /// These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example: /// <example> /// <![CDATA[ /// furtherOptions: "&bgcolor=fff" /// ]]> /// </example> /// </param> /// <param name="ratioMode"> /// Use a dimension as a ratio /// </param> /// <param name="upScale"> /// If the image should be upscaled to requested dimensions /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> 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) { if (string.IsNullOrEmpty(imageUrl) == false) { var imageProcessorUrl = new StringBuilder(); if (cropDataSet != null && (imageCropMode == ImageCropMode.Crop || imageCropMode == null)) { var crop = cropDataSet.GetCrop(cropAlias); // if a crop was specified, but not found, return null if (crop == null && !string.IsNullOrWhiteSpace(cropAlias)) { return(null); } imageProcessorUrl.Append(imageUrl); cropDataSet.AppendCropBaseUrl(imageProcessorUrl, crop, string.IsNullOrWhiteSpace(cropAlias), preferFocalPoint); if (crop != null & useCropDimensions) { width = crop.Width; height = crop.Height; } // If a predefined crop has been specified & there are no coordinates & no ratio mode, but a width parameter has been passed we can get the crop ratio for the height if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width != null && height == null) { var heightRatio = (decimal)crop.Height / (decimal)crop.Width; imageProcessorUrl.Append("&heightratio=" + heightRatio.ToString(CultureInfo.InvariantCulture)); } // If a predefined crop has been specified & there are no coordinates & no ratio mode, but a height parameter has been passed we can get the crop ratio for the width if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width == null && height != null) { var widthRatio = (decimal)crop.Width / (decimal)crop.Height; imageProcessorUrl.Append("&widthratio=" + widthRatio.ToString(CultureInfo.InvariantCulture)); } } else { imageProcessorUrl.Append(imageUrl); if (imageCropMode == null) { imageCropMode = ImageCropMode.Pad; } imageProcessorUrl.Append("?mode=" + imageCropMode.ToString().ToLower()); if (imageCropAnchor != null) { imageProcessorUrl.Append("&anchor=" + imageCropAnchor.ToString().ToLower()); } } var hasFormat = furtherOptions != null && furtherOptions.InvariantContains("&format="); //Only put quality here, if we don't have a format specified. //Otherwise we need to put quality at the end to avoid it being overridden by the format. if (quality != null && hasFormat == false) { imageProcessorUrl.Append("&quality=" + quality); } if (width != null && ratioMode != ImageCropRatioMode.Width) { imageProcessorUrl.Append("&width=" + width); } if (height != null && ratioMode != ImageCropRatioMode.Height) { imageProcessorUrl.Append("&height=" + height); } if (ratioMode == ImageCropRatioMode.Width && height != null) { // if only height specified then assume a square if (width == null) { width = height; } var widthRatio = (decimal)width / (decimal)height; imageProcessorUrl.Append("&widthratio=" + widthRatio.ToString(CultureInfo.InvariantCulture)); } if (ratioMode == ImageCropRatioMode.Height && width != null) { // if only width specified then assume a square if (height == null) { height = width; } var heightRatio = (decimal)height / (decimal)width; imageProcessorUrl.Append("&heightratio=" + heightRatio.ToString(CultureInfo.InvariantCulture)); } if (upScale == false) { imageProcessorUrl.Append("&upscale=false"); } if (furtherOptions != null) { imageProcessorUrl.Append(furtherOptions); } //If furtherOptions contains a format, we need to put the quality after the format. if (quality != null && hasFormat) { imageProcessorUrl.Append("&quality=" + quality); } if (cacheBusterValue != null) { imageProcessorUrl.Append("&rnd=").Append(cacheBusterValue); } return(imageProcessorUrl.ToString()); } return(string.Empty); }
public static string GetCropUrl <T>(this string imageUrl, T cropAlias, int?width = null, int?height = null, string imageCropperValue = 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 = false) where T : struct { var crop = CropHelper.GetCropAttribute(cropAlias); if (width.HasValue == false) { if (crop.Width > 0) { width = crop.Width; } } if (height.HasValue == false) { if (crop.Height > 0) { height = crop.Height; } } return(imageUrl.GetCropUrl(width, height, imageCropperValue, crop.Alias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale)); }
public static string GetCropUrl <T>(this IPublishedContent mediaItem, T cropAlias, int?width = null, int?height = null, string propertyAlias = Constants.Conventions.Media.File, 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) where T : struct { var crop = CropHelper.GetCropAttribute(cropAlias); var alias = string.IsNullOrWhiteSpace(crop.Alias) == false ? crop.Alias : null; if (width.HasValue == false) { if (crop.Width > 0) { width = crop.Width; } } if (height.HasValue == false) { if (crop.Height > 0) { height = crop.Height; } } return(mediaItem.GetCropUrl(width, height, propertyAlias, alias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale)); }
public static string GetCropUrl( this ImageCropDataSet 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 ) { var imageResizerUrl = new StringBuilder(); imageResizerUrl.Append(cropDataSet.Src); if (imageCropMode == ImageCropMode.Crop || imageCropMode == null) { var crop = cropDataSet.GetCrop(cropAlias); var cropBaseUrl = cropDataSet.GetCropBaseUrl(cropAlias, preferFocalPoint); if (cropBaseUrl != null) { imageResizerUrl.Append(cropBaseUrl); } else { return(null); } if (crop != null & useCropDimensions) { width = crop.Width; height = crop.Height; } } else { imageResizerUrl.Append("?mode=" + imageCropMode.ToString().ToLower()); if (imageCropAnchor != null) { imageResizerUrl.Append("&anchor=" + imageCropAnchor.ToString().ToLower()); } } if (quality != null) { imageResizerUrl.Append("&quality=" + quality); } if (width != null && ratioMode != ImageCropRatioMode.Width) { imageResizerUrl.Append("&width=" + width); } if (height != null && ratioMode != ImageCropRatioMode.Height) { imageResizerUrl.Append("&height=" + height); } if (ratioMode == ImageCropRatioMode.Width && height != null) { //if only height specified then assume a sqaure if (width == null) { width = height; } var widthRatio = (decimal)width / (decimal)height; imageResizerUrl.Append("&widthratio=" + widthRatio.ToString(CultureInfo.InvariantCulture)); } if (ratioMode == ImageCropRatioMode.Height && width != null) { //if only width specified then assume a sqaure if (height == null) { height = width; } var heightRatio = (decimal)height / (decimal)width; imageResizerUrl.Append("&heightratio=" + heightRatio.ToString(CultureInfo.InvariantCulture)); } if (upScale == false) { imageResizerUrl.Append("&upscale=false"); } if (furtherOptions != null) { imageResizerUrl.Append(furtherOptions); } if (cacheBusterValue != null) { imageResizerUrl.Append("&rnd=").Append(cacheBusterValue); } return(imageResizerUrl.ToString()); }
/// <summary> /// Return a crop URL of the image based on the specified parameters. /// </summary> /// <param name="width">The width of the output image.</param> /// <param name="height">The height of the output image.</param> /// <param name="propertyAlias">Property alias of the property containing the Json data.</param> /// <param name="cropAlias">The crop alias.</param> /// <param name="quality">Quality percentage of the output image.</param> /// <param name="imageCropMode">The image crop mode.</param> /// <param name="imageCropAnchor">The image crop anchor.</param> /// <param name="preferFocalPoint">Use focal point, to generate an output image using the focal point instead of the predefined crop.</param> /// <param name="useCropDimensions">Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters.</param> /// <param name="cacheBuster">Add a serialised date of the last edit of the item to ensure client cache refresh when updated.</param> /// <param name="furtherOptions">The further options.</param> /// <param name="ratioMode">Use a dimension as a ratio.</param> /// <param name="upScale">If the image should be upscaled to requested dimensions.</param> /// <returns>Returns a string with the crop URL.</returns> public string GetCropUrl(int? width = null, int? height = null, string propertyAlias = "umbracoFile", 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) { return Image.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale); }
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, UrlMode urlMode = UrlMode.Default) { 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, mode: urlMode); // 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 <ImageCropperValue>(); } 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)); }
/// <summary> /// Gets the underlying image processing service URL from the image path. /// </summary> /// <param name="imageUrl">The image URL.</param> /// <param name="imageUrlGenerator"> /// The generator that will process all the options and the image URL to return a full /// image URLs with all processing options appended. /// </param> /// <param name="cropDataSet">The crop data set.</param> /// <param name="width">The width of the output image.</param> /// <param name="height">The height of the output image.</param> /// <param name="cropAlias">The crop alias.</param> /// <param name="quality">Quality percentage of the output image.</param> /// <param name="imageCropMode">The image crop mode.</param> /// <param name="imageCropAnchor">The image crop anchor.</param> /// <param name="preferFocalPoint"> /// Use focal point to generate an output image using the focal point instead of the /// predefined crop if there is one. /// </param> /// <param name="useCropDimensions"> /// Use crop dimensions to have the output image sized according to the predefined crop /// sizes, this will override the width and height parameters. /// </param> /// <param name="cacheBusterValue"> /// Add a serialized date of the last edit of the item to ensure client cache refresh when /// updated. /// </param> /// <param name="furtherOptions"> /// These are any query string parameters (formatted as query strings) that the underlying image processing service /// supports. For example: /// <example><![CDATA[ /// furtherOptions: "bgcolor=fff" /// ]]></example> /// </param> /// <returns> /// The URL of the cropped image. /// </returns> 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) { 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, but has no coordinates if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == 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, // Not sure why we default to Pad ImageCropAnchor = imageCropAnchor, }; } options.Quality = quality; options.Width = width; options.Height = height; options.FurtherOptions = furtherOptions; options.CacheBusterValue = cacheBusterValue; return(imageUrlGenerator.GetImageUrl(options)); }
/// <summary> /// Gets the ImageProcessor Url from the image path. /// </summary> /// <param name="imageUrl"> /// The image url. /// </param> /// <param name="width"> /// The width of the output image. /// </param> /// <param name="height"> /// The height of the output image. /// </param> /// <param name="imageCropperValue"> /// The Json data from the Umbraco Core Image Cropper property editor /// </param> /// <param name="cropAlias"> /// The crop alias. /// </param> /// <param name="quality"> /// Quality percentage of the output image. /// </param> /// <param name="imageCropMode"> /// The image crop mode. /// </param> /// <param name="imageCropAnchor"> /// The image crop anchor. /// </param> /// <param name="preferFocalPoint"> /// Use focal point to generate an output image using the focal point instead of the predefined crop if there is one /// </param> /// <param name="useCropDimensions"> /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters /// </param> /// <param name="cacheBusterValue"> /// Add a serialised date of the last edit of the item to ensure client cache refresh when updated /// </param> /// <param name="furtherOptions"> /// The further options. /// </param> /// <param name="ratioMode"> /// Use a dimension as a ratio /// </param> /// <param name="upScale"> /// If the image should be upscaled to requested dimensions /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> 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) { if (string.IsNullOrEmpty(imageUrl) == false) { var imageProcessorUrl = new StringBuilder(); if (string.IsNullOrEmpty(imageCropperValue) == false && imageCropperValue.DetectIsJson() && (imageCropMode == ImageCropMode.Crop || imageCropMode == null)) { var cropDataSet = imageCropperValue.SerializeToCropDataSet(); if (cropDataSet != null) { var crop = cropDataSet.GetCrop(cropAlias); imageProcessorUrl.Append(cropDataSet.Src); var cropBaseUrl = cropDataSet.GetCropBaseUrl(cropAlias, preferFocalPoint); if (cropBaseUrl != null) { imageProcessorUrl.Append(cropBaseUrl); } else { return(null); } if (crop != null & useCropDimensions) { width = crop.Width; height = crop.Height; } // If a predefined crop has been specified & there are no coordinates & no ratio mode, but a width parameter has been passed we can get the crop ratio for the height if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width != null && height == null) { var heightRatio = (decimal)crop.Height / (decimal)crop.Width; imageProcessorUrl.Append("&heightratio=" + heightRatio.ToString(CultureInfo.InvariantCulture)); } // If a predefined crop has been specified & there are no coordinates & no ratio mode, but a height parameter has been passed we can get the crop ratio for the width if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width == null && height != null) { var widthRatio = (decimal)crop.Width / (decimal)crop.Height; imageProcessorUrl.Append("&widthratio=" + widthRatio.ToString(CultureInfo.InvariantCulture)); } } } else { imageProcessorUrl.Append(imageUrl); if (imageCropMode == null) { imageCropMode = ImageCropMode.Pad; } imageProcessorUrl.Append("?mode=" + imageCropMode.ToString().ToLower()); if (imageCropAnchor != null) { imageProcessorUrl.Append("&anchor=" + imageCropAnchor.ToString().ToLower()); } } if (quality != null) { imageProcessorUrl.Append("&quality=" + quality); } if (width != null && ratioMode != ImageCropRatioMode.Width) { imageProcessorUrl.Append("&width=" + width); } if (height != null && ratioMode != ImageCropRatioMode.Height) { imageProcessorUrl.Append("&height=" + height); } if (ratioMode == ImageCropRatioMode.Width && height != null) { // if only height specified then assume a sqaure if (width == null) { width = height; } var widthRatio = (decimal)width / (decimal)height; imageProcessorUrl.Append("&widthratio=" + widthRatio.ToString(CultureInfo.InvariantCulture)); } if (ratioMode == ImageCropRatioMode.Height && width != null) { // if only width specified then assume a sqaure if (height == null) { height = width; } var heightRatio = (decimal)height / (decimal)width; imageProcessorUrl.Append("&heightratio=" + heightRatio.ToString(CultureInfo.InvariantCulture)); } if (upScale == false) { imageProcessorUrl.Append("&upscale=false"); } if (furtherOptions != null) { imageProcessorUrl.Append(furtherOptions); } if (cacheBusterValue != null) { imageProcessorUrl.Append("&rnd=").Append(cacheBusterValue); } return(imageProcessorUrl.ToString()); } return(string.Empty); }
/// <summary> /// Gets the ImageProcessor Url from the image path. /// </summary> /// <param name="imageUrl"> /// The image url. /// </param> /// <param name="imageUrlGenerator"> /// The generator that will process all the options and the image URL to return a full image urls with all processing options appended /// </param> /// <param name="cropDataSet"></param> /// <param name="width"> /// The width of the output image. /// </param> /// <param name="height"> /// The height of the output image. /// </param> /// <param name="cropAlias"> /// The crop alias. /// </param> /// <param name="quality"> /// Quality percentage of the output image. /// </param> /// <param name="imageCropMode"> /// The image crop mode. /// </param> /// <param name="imageCropAnchor"> /// The image crop anchor. /// </param> /// <param name="preferFocalPoint"> /// Use focal point to generate an output image using the focal point instead of the predefined crop if there is one /// </param> /// <param name="useCropDimensions"> /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters /// </param> /// <param name="cacheBusterValue"> /// Add a serialized date of the last edit of the item to ensure client cache refresh when updated /// </param> /// <param name="furtherOptions"> /// These are any query string parameters (formatted as query strings) that ImageProcessor supports. For example: /// <example> /// <![CDATA[ /// furtherOptions: "&bgcolor=fff" /// ]]> /// </example> /// </param> /// <param name="ratioMode"> /// Use a dimension as a ratio /// </param> /// <param name="upScale"> /// If the image should be upscaled to requested dimensions /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> 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, bool upScale = true, string animationProcessMode = null) { if (string.IsNullOrEmpty(imageUrl)) { return(string.Empty); } ImageUrlGenerationOptions options; if (cropDataSet != null && (imageCropMode == ImageCropMode.Crop || imageCropMode == null)) { var 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, string.IsNullOrWhiteSpace(cropAlias), preferFocalPoint); if (crop != null & useCropDimensions) { width = crop.Width; height = crop.Height; } // If a predefined crop has been specified & there are no coordinates & no ratio mode, but a width parameter has been passed we can get the crop ratio for the height if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width != null && height == null) { options.HeightRatio = (decimal)crop.Height / crop.Width; } // If a predefined crop has been specified & there are no coordinates & no ratio mode, but a height parameter has been passed we can get the crop ratio for the width if (crop != null && string.IsNullOrEmpty(cropAlias) == false && crop.Coordinates == null && ratioMode == null && width == null && height != null) { options.WidthRatio = (decimal)crop.Width / crop.Height; } } else { options = new ImageUrlGenerationOptions(imageUrl) { ImageCropMode = (imageCropMode ?? ImageCropMode.Pad).ToString().ToLowerInvariant(), ImageCropAnchor = imageCropAnchor?.ToString().ToLowerInvariant() }; } options.Quality = quality; options.Width = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Width ? null : width; options.Height = ratioMode != null && ratioMode.Value == ImageCropRatioMode.Height ? null : height; options.AnimationProcessMode = animationProcessMode; if (ratioMode == ImageCropRatioMode.Width && height != null) { // if only height specified then assume a square if (width == null) { width = height; } options.WidthRatio = (decimal)width / (decimal)height; } if (ratioMode == ImageCropRatioMode.Height && width != null) { // if only width specified then assume a square if (height == null) { height = width; } options.HeightRatio = (decimal)height / (decimal)width; } options.UpScale = upScale; options.FurtherOptions = furtherOptions; options.CacheBusterValue = cacheBusterValue; return(imageUrlGenerator.GetImageUrl(options)); }
public static string GetCropUrlWithFormat( this IPublishedContent node, Enums.CropAlias cropAlias, Enums.Media format = Enums.Media.None, bool cacheBuster = false, int?quality = 75, bool useCropDimensions = true, ImageCropMode?imageCropMode = null, ImageCropAnchor?imageCropAnchor = null) { if (node == null) { return(null); } if (node.Url().EndsWith(".svg")) { return(node.Url()); } if (cropAlias == Enums.CropAlias.NoCrop) { return(node.GetCropUrl( furtherOptions: "&format=" + format, cacheBuster: cacheBuster, quality: quality)); } switch (format) { case Enums.Media.WebP: var webP = node.GetCropUrl( cropAlias: cropAlias.GetDescription(), furtherOptions: "&format=" + format, cacheBuster: cacheBuster, useCropDimensions: useCropDimensions, quality: quality, imageCropMode: imageCropMode, imageCropAnchor: imageCropAnchor); if (webP != null) { return(webP); } break; default: var crop = node.GetCropUrl( cropAlias: cropAlias.GetDescription(), cacheBuster: cacheBuster, useCropDimensions: useCropDimensions, quality: quality, imageCropMode: imageCropMode, imageCropAnchor: imageCropAnchor); if (crop != null) { return(crop); } break; } return(node.Url()); }