using System;
using Newtonsoft.Json.Linq;
using System.Globalization;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors.ValueConverters;
using Umbraco.Web.Models;
namespace Umbraco.Web
{
public static class ImageCropperTemplateCoreExtensions
{
///
/// 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, IImageUrlGenerator imageUrlGenerator)
{
return mediaItem.GetCropUrl(imageUrlGenerator, cropAlias: cropAlias, useCropDimensions: true);
}
///
/// 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, IImageUrlGenerator imageUrlGenerator)
{
return mediaItem.GetCropUrl(imageUrlGenerator, propertyAlias: propertyAlias, cropAlias: cropAlias, useCropDimensions: true);
}
///
/// 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,
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();
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);
}
///
/// 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,
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);
}
///
/// Gets the ImageProcessor 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 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,
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);
}
}
}