2016-02-04 16:03:43 +01:00
using System ;
using Newtonsoft.Json.Linq ;
using System.Globalization ;
using System.Text ;
using Umbraco.Core ;
using Umbraco.Core.Models ;
2016-06-10 16:37:28 +02:00
using Umbraco.Core.Models.PublishedContent ;
2016-02-04 16:03:43 +01:00
using Umbraco.Web.Models ;
namespace Umbraco.Web
2014-02-17 16:15:39 +01:00
{
2014-03-24 17:51:52 +00:00
/// <summary>
/// Provides extension methods for getting ImageProcessor Url from the core Image Cropper property editor
/// </summary>
2014-02-17 16:15:39 +01:00
public static class ImageCropperTemplateExtensions
{
2014-03-24 17:51:52 +00:00
/// <summary>
/// Gets the ImageProcessor Url by the crop alias (from the "umbracoFile" property alias) on the IPublishedContent item
/// </summary>
/// <param name="mediaItem">
/// The IPublishedContent item.
/// </param>
/// <param name="cropAlias">
/// The crop alias e.g. thumbnail
/// </param>
/// <returns>
/// The ImageProcessor.Web Url.
/// </returns>
2014-03-20 11:23:17 +00:00
public static string GetCropUrl ( this IPublishedContent mediaItem , string cropAlias )
{
2014-03-24 17:51:52 +00:00
return mediaItem . GetCropUrl ( cropAlias : cropAlias , useCropDimensions : true ) ;
2014-03-20 11:23:17 +00:00
}
2014-03-24 17:51:52 +00:00
/// <summary>
/// Gets the ImageProcessor Url by the crop alias using the specified property containing the image cropper Json data on the IPublishedContent item.
/// </summary>
/// <param name="mediaItem">
/// The IPublishedContent item.
/// </param>
/// <param name="propertyAlias">
/// The property alias of the property containing the Json data e.g. umbracoFile
/// </param>
/// <param name="cropAlias">
/// The crop alias e.g. thumbnail
/// </param>
/// <returns>
/// The ImageProcessor.Web Url.
/// </returns>
2014-02-24 15:33:47 +01:00
public static string GetCropUrl ( this IPublishedContent mediaItem , string propertyAlias , string cropAlias )
2014-02-17 16:15:39 +01:00
{
2014-03-24 16:32:50 +00:00
return mediaItem . GetCropUrl ( propertyAlias : propertyAlias , cropAlias : cropAlias , useCropDimensions : true ) ;
2014-02-17 16:15:39 +01:00
}
2014-03-24 17:51:52 +00:00
/// <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>
2014-03-24 18:37:15 +00:00
/// <param name="propertyAlias">
/// Property alias of the property containing the Json data.
/// </param>
/// <param name="cropAlias">
/// The crop alias.
/// </param>
2014-03-24 17:51:52 +00:00
/// <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>
2014-03-24 18:37:15 +00:00
/// <param name="preferFocalPoint">
2014-03-24 17:51:52 +00:00
/// 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">
2014-03-24 19:27:00 +00:00
/// Add a serialised date of the last edit of the item to ensure client cache refresh when updated
2014-03-24 17:51:52 +00:00
/// </param>
/// <param name="furtherOptions">
/// The further options.
/// </param>
2014-04-18 22:04:23 +01:00
/// <param name="ratioMode">
/// Use a dimension as a ratio
2014-04-21 09:45:05 +01:00
/// </param>
/// <param name="upScale">
/// If the image should be upscaled to requested dimensions
2014-04-18 22:04:23 +01:00
/// </param>
2014-03-24 17:51:52 +00:00
/// <returns>
/// The <see cref="string"/>.
/// </returns>
2014-03-20 14:27:58 +11:00
public static string GetCropUrl (
this IPublishedContent mediaItem ,
int? width = null ,
int? height = null ,
2014-03-24 18:37:15 +00:00
string propertyAlias = Constants . Conventions . Media . File ,
string cropAlias = null ,
2014-03-20 14:27:58 +11:00
int? quality = null ,
ImageCropMode ? imageCropMode = null ,
ImageCropAnchor ? imageCropAnchor = null ,
2014-03-24 18:37:15 +00:00
bool preferFocalPoint = false ,
2014-03-24 16:32:50 +00:00
bool useCropDimensions = false ,
2014-03-24 15:44:04 +00:00
bool cacheBuster = true ,
2014-04-18 22:04:23 +01:00
string furtherOptions = null ,
2015-08-20 09:34:48 +01:00
ImageCropRatioMode ? ratioMode = null ,
bool upScale = true )
2014-02-17 16:15:39 +01:00
{
2016-02-04 16:03:43 +01:00
if ( mediaItem = = null ) throw new ArgumentNullException ( "mediaItem" ) ;
2016-01-26 15:54:17 +01:00
2016-02-04 16:03:43 +01:00
var cacheBusterValue = cacheBuster ? mediaItem . UpdateDate . ToFileTimeUtc ( ) . ToString ( CultureInfo . InvariantCulture ) : null ;
2014-02-17 16:15:39 +01:00
2016-02-04 16:03:43 +01:00
if ( mediaItem . HasProperty ( propertyAlias ) = = false | | mediaItem . HasValue ( propertyAlias ) = = false )
return string . Empty ;
//get the default obj from the value converter
var cropperValue = mediaItem . GetPropertyValue ( propertyAlias ) ;
2014-03-20 12:42:05 +00:00
2016-02-04 16:03:43 +01:00
//is it strongly typed?
var stronglyTyped = cropperValue as ImageCropDataSet ;
string mediaItemUrl ;
if ( stronglyTyped ! = null )
2014-03-20 14:31:21 +00:00
{
2016-02-04 16:03:43 +01:00
mediaItemUrl = stronglyTyped . Src ;
return GetCropUrl (
mediaItemUrl , stronglyTyped , width , height , cropAlias , quality , imageCropMode , imageCropAnchor , preferFocalPoint , useCropDimensions ,
cacheBusterValue , furtherOptions , ratioMode , upScale ) ;
2014-03-20 14:31:21 +00:00
}
2014-03-20 12:42:05 +00:00
2016-02-04 16:03:43 +01:00
//this shouldn't be the case but we'll check
var jobj = cropperValue as JObject ;
if ( jobj ! = null )
{
stronglyTyped = jobj . ToObject < ImageCropDataSet > ( ) ;
mediaItemUrl = stronglyTyped . Src ;
return GetCropUrl (
mediaItemUrl , stronglyTyped , width , height , cropAlias , quality , imageCropMode , imageCropAnchor , preferFocalPoint , useCropDimensions ,
cacheBusterValue , furtherOptions , ratioMode , upScale ) ;
}
2014-03-24 19:27:00 +00:00
2016-02-04 16:03:43 +01:00
//it's a single string
mediaItemUrl = cropperValue . ToString ( ) ;
return GetCropUrl (
mediaItemUrl , width , height , mediaItemUrl , cropAlias , quality , imageCropMode , imageCropAnchor , preferFocalPoint , useCropDimensions ,
cacheBusterValue , furtherOptions , ratioMode , upScale ) ;
2014-02-17 16:15:39 +01:00
}
2014-03-24 17:51:52 +00:00
/// <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>
2015-08-19 16:54:03 +01:00
/// <param name="imageCropperValue">
/// The Json data from the Umbraco Core Image Cropper property editor
/// </param>
/// <param name="cropAlias">
/// The crop alias.
/// </param>
2014-03-24 17:51:52 +00:00
/// <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>
2014-03-24 18:37:15 +00:00
/// <param name="preferFocalPoint">
/// Use focal point to generate an output image using the focal point instead of the predefined crop if there is one
2014-03-24 17:51:52 +00:00
/// </param>
/// <param name="useCropDimensions">
2015-08-19 16:54:03 +01:00
/// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters
2014-03-24 17:51:52 +00:00
/// </param>
2014-03-24 19:27:00 +00:00
/// <param name="cacheBusterValue">
/// Add a serialised date of the last edit of the item to ensure client cache refresh when updated
2014-03-24 17:51:52 +00:00
/// </param>
/// <param name="furtherOptions">
/// The further options.
/// </param>
2014-04-18 22:04:23 +01:00
/// <param name="ratioMode">
/// Use a dimension as a ratio
2015-08-19 16:54:03 +01:00
/// </param>
2014-04-21 09:45:05 +01:00
/// <param name="upScale">
/// If the image should be upscaled to requested dimensions
2015-08-19 16:54:03 +01:00
/// </param>
2014-03-24 17:51:52 +00:00
/// <returns>
/// The <see cref="string"/>.
/// </returns>
2014-02-24 15:33:47 +01:00
public static string GetCropUrl (
2014-02-17 16:15:39 +01:00
this string imageUrl ,
int? width = null ,
int? height = null ,
2014-03-24 18:37:15 +00:00
string imageCropperValue = null ,
string cropAlias = null ,
2014-02-17 16:15:39 +01:00
int? quality = null ,
2014-03-20 14:27:58 +11:00
ImageCropMode ? imageCropMode = null ,
ImageCropAnchor ? imageCropAnchor = null ,
2014-03-24 18:37:15 +00:00
bool preferFocalPoint = false ,
2014-03-24 16:32:50 +00:00
bool useCropDimensions = false ,
2014-03-24 19:27:00 +00:00
string cacheBusterValue = null ,
2014-04-18 22:04:23 +01:00
string furtherOptions = null ,
2014-04-21 09:45:05 +01:00
ImageCropRatioMode ? ratioMode = null ,
2015-08-19 16:54:03 +01:00
bool upScale = true )
2016-02-04 16:03:43 +01:00
{
if ( string . IsNullOrEmpty ( imageUrl ) ) return string . Empty ;
ImageCropDataSet cropDataSet = null ;
if ( string . IsNullOrEmpty ( imageCropperValue ) = = false & & imageCropperValue . DetectIsJson ( ) & & ( imageCropMode = = ImageCropMode . Crop | | imageCropMode = = null ) )
{
cropDataSet = imageCropperValue . SerializeToCropDataSet ( ) ;
}
return GetCropUrl (
imageUrl , cropDataSet , width , height , cropAlias , quality , imageCropMode ,
imageCropAnchor , preferFocalPoint , useCropDimensions , cacheBusterValue , furtherOptions , ratioMode , upScale ) ;
}
public static string GetCropUrl (
this string imageUrl ,
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 )
2014-02-17 16:15:39 +01:00
{
2014-03-24 17:51:52 +00:00
if ( string . IsNullOrEmpty ( imageUrl ) = = false )
2014-02-17 16:15:39 +01:00
{
2015-08-20 09:33:12 +01:00
var imageProcessorUrl = new StringBuilder ( ) ;
2014-02-17 16:15:39 +01:00
2016-02-04 16:03:43 +01:00
if ( cropDataSet ! = null & & ( imageCropMode = = ImageCropMode . Crop | | imageCropMode = = null ) )
2014-02-17 16:15:39 +01:00
{
2016-02-04 16:03:43 +01:00
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 )
2014-03-24 15:44:04 +00:00
{
2016-02-04 16:03:43 +01:00
var widthRatio = ( decimal ) crop . Width / ( decimal ) crop . Height ;
imageProcessorUrl . Append ( "&widthratio=" + widthRatio . ToString ( CultureInfo . InvariantCulture ) ) ;
2014-03-24 15:44:04 +00:00
}
2014-02-17 16:15:39 +01:00
}
else
{
2015-08-20 09:33:12 +01:00
imageProcessorUrl . Append ( imageUrl ) ;
2014-04-19 11:04:32 +01:00
2014-03-20 14:27:58 +11:00
if ( imageCropMode = = null )
2014-02-17 16:15:39 +01:00
{
2014-03-20 14:27:58 +11:00
imageCropMode = ImageCropMode . Pad ;
2014-02-17 16:15:39 +01:00
}
2014-03-24 17:51:52 +00:00
2015-08-20 09:33:12 +01:00
imageProcessorUrl . Append ( "?mode=" + imageCropMode . ToString ( ) . ToLower ( ) ) ;
2014-02-17 16:15:39 +01:00
2014-03-20 14:27:58 +11:00
if ( imageCropAnchor ! = null )
2014-02-17 16:15:39 +01:00
{
2015-08-20 09:33:12 +01:00
imageProcessorUrl . Append ( "&anchor=" + imageCropAnchor . ToString ( ) . ToLower ( ) ) ;
2014-02-17 16:15:39 +01:00
}
}
if ( quality ! = null )
{
2015-08-20 09:33:12 +01:00
imageProcessorUrl . Append ( "&quality=" + quality ) ;
2014-02-17 16:15:39 +01:00
}
2014-04-19 11:04:32 +01:00
if ( width ! = null & & ratioMode ! = ImageCropRatioMode . Width )
2014-02-17 16:15:39 +01:00
{
2015-08-20 09:33:12 +01:00
imageProcessorUrl . Append ( "&width=" + width ) ;
2014-02-17 16:15:39 +01:00
}
2014-04-19 11:04:32 +01:00
if ( height ! = null & & ratioMode ! = ImageCropRatioMode . Height )
2014-02-17 16:15:39 +01:00
{
2015-08-20 09:33:12 +01:00
imageProcessorUrl . Append ( "&height=" + height ) ;
2014-02-17 16:15:39 +01:00
}
2014-04-21 09:27:53 +01:00
if ( ratioMode = = ImageCropRatioMode . Width & & height ! = null )
2014-04-18 22:04:23 +01:00
{
2015-08-19 16:54:03 +01:00
// if only height specified then assume a sqaure
2014-04-21 09:27:53 +01:00
if ( width = = null )
{
width = height ;
}
2015-08-19 16:54:03 +01:00
var widthRatio = ( decimal ) width / ( decimal ) height ;
2015-08-20 09:33:12 +01:00
imageProcessorUrl . Append ( "&widthratio=" + widthRatio . ToString ( CultureInfo . InvariantCulture ) ) ;
2014-04-18 22:04:23 +01:00
}
2014-04-21 09:27:53 +01:00
if ( ratioMode = = ImageCropRatioMode . Height & & width ! = null )
2014-04-18 22:04:23 +01:00
{
2015-08-19 16:54:03 +01:00
// if only width specified then assume a sqaure
2014-04-21 09:27:53 +01:00
if ( height = = null )
{
height = width ;
}
2015-08-19 16:54:03 +01:00
var heightRatio = ( decimal ) height / ( decimal ) width ;
2015-08-20 09:33:12 +01:00
imageProcessorUrl . Append ( "&heightratio=" + heightRatio . ToString ( CultureInfo . InvariantCulture ) ) ;
2014-04-18 22:04:23 +01:00
}
2014-04-21 09:45:05 +01:00
if ( upScale = = false )
{
2016-02-04 16:03:43 +01:00
imageProcessorUrl . Append ( "&upscale=false" ) ;
2014-04-21 09:45:05 +01:00
}
2014-02-17 16:15:39 +01:00
if ( furtherOptions ! = null )
{
2015-08-20 09:33:12 +01:00
imageProcessorUrl . Append ( furtherOptions ) ;
2014-02-17 16:15:39 +01:00
}
2014-04-19 11:04:32 +01:00
if ( cacheBusterValue ! = null )
{
2015-08-20 09:33:12 +01:00
imageProcessorUrl . Append ( "&rnd=" ) . Append ( cacheBusterValue ) ;
2014-04-19 11:04:32 +01:00
}
2015-08-20 09:33:12 +01:00
return imageProcessorUrl . ToString ( ) ;
2014-02-17 16:15:39 +01:00
}
2014-03-24 17:51:52 +00:00
2014-02-17 16:15:39 +01:00
return string . Empty ;
}
}
}