From 40be3957f6fb0e30db6f70100b0b7079906baccc Mon Sep 17 00:00:00 2001 From: Jeavon Leopold Date: Mon, 24 Mar 2014 15:44:04 +0000 Subject: [PATCH] GetCropUrl - ensuring that GetCropUrl(width,height) works and also adding the useFocalPoint parameter --- .../ImageCropperTemplateExtensions.cs | 33 +++++++++++++---- src/Umbraco.Web/Models/ImageCropDataSet.cs | 35 +++++++++---------- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs index 2d3566e9f9..9d02ea299b 100644 --- a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs +++ b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs @@ -30,7 +30,14 @@ namespace Umbraco.Web if (property.DetectIsJson()) { var cropDataSet = property.SerializeToCropDataSet(); - return cropDataSet.Src + cropDataSet.GetCropUrl(cropAlias); + + var cropUrl = cropDataSet.GetCropUrl(cropAlias); + + if (cropUrl != null) + { + return cropDataSet.Src + cropUrl; + } + return null; } else { @@ -45,15 +52,17 @@ namespace Umbraco.Web int? quality = null, ImageCropMode? imageCropMode = null, ImageCropAnchor? imageCropAnchor = null, - string propertyAlias = null, + string propertyAlias = Constants.Conventions.Media.File, string cropAlias = null, + bool useFocalPoint = false, + bool cacheBuster = true, string furtherOptions = null) { string imageCropperValue = null; string mediaItemUrl; - if (mediaItem.HasPropertyAndValueAndCrop(propertyAlias, cropAlias)) + if (mediaItem.HasProperty(propertyAlias) && mediaItem.HasValue(propertyAlias)) { imageCropperValue = mediaItem.GetPropertyValue(propertyAlias); @@ -70,7 +79,7 @@ namespace Umbraco.Web } return mediaItemUrl != null - ? GetCropUrl(mediaItemUrl, width, height, quality, imageCropMode, imageCropAnchor, imageCropperValue, cropAlias, furtherOptions) + ? GetCropUrl(mediaItemUrl, width, height, quality, imageCropMode, imageCropAnchor, imageCropperValue, cropAlias, useFocalPoint, cacheBuster, furtherOptions) : string.Empty; } @@ -83,6 +92,8 @@ namespace Umbraco.Web ImageCropAnchor? imageCropAnchor = null, string imageCropperValue = null, string cropAlias = null, + bool useFocalPoint = false, + bool cacheBuster = true, string furtherOptions = null) { if (!string.IsNullOrEmpty(imageUrl)) @@ -92,8 +103,18 @@ namespace Umbraco.Web if (!string.IsNullOrEmpty(imageCropperValue) && imageCropperValue.DetectIsJson()) { var cropDataSet = imageCropperValue.SerializeToCropDataSet(); - imageResizerUrl.Append(cropDataSet.Src); - imageResizerUrl.Append(cropDataSet.GetCropUrl(cropAlias, false, false)); + if (cropDataSet != null) + { + var cropUrl = cropDataSet.GetCropUrl(cropAlias, false, useFocalPoint, cacheBuster); + + // if crop alias has been specified but not found we should return null + if (string.IsNullOrEmpty(cropAlias) == false && cropUrl == null) + { + return null; + } + imageResizerUrl.Append(cropDataSet.Src); + imageResizerUrl.Append(cropDataSet.GetCropUrl(cropAlias, false, useFocalPoint, cacheBuster)); + } } else { diff --git a/src/Umbraco.Web/Models/ImageCropDataSet.cs b/src/Umbraco.Web/Models/ImageCropDataSet.cs index 08d727b24a..97fca032a2 100644 --- a/src/Umbraco.Web/Models/ImageCropDataSet.cs +++ b/src/Umbraco.Web/Models/ImageCropDataSet.cs @@ -20,16 +20,22 @@ namespace Umbraco.Web.Models public IEnumerable Crops { get; set; } - public string GetCropUrl(string alias, bool addCropDimensions = true, bool addRandom = true) + public string GetCropUrl(string alias, bool addCropDimensions = true, bool useFocalPoint = false, bool cacheBuster = true) { var crop = Crops.GetCrop(alias); - if(crop == null) + + if(crop == null && !string.IsNullOrEmpty(alias)) return null; - var sb = new StringBuilder(); - if (crop.Coordinates != null) + + if (useFocalPoint && HasFocalPoint() || (crop != null && crop.Coordinates == null && HasFocalPoint()) || (string.IsNullOrEmpty(alias) && HasFocalPoint())) + { + sb.Append("?center=" + FocalPoint.Top.ToString(System.Globalization.CultureInfo.InvariantCulture) + "," + FocalPoint.Left.ToString(System.Globalization.CultureInfo.InvariantCulture)); + sb.Append("&mode=crop"); + } + else if (crop != null && crop.Coordinates != null) { sb.Append("?crop="); sb.Append(crop.Coordinates.X1.ToString(System.Globalization.CultureInfo.InvariantCulture)).Append(","); @@ -39,26 +45,17 @@ namespace Umbraco.Web.Models sb.Append("&cropmode=percentage"); } else - { - if (HasFocalPoint()) - { - sb.Append("?center=" + FocalPoint.Top.ToString(System.Globalization.CultureInfo.InvariantCulture) + "," + FocalPoint.Left.ToString(System.Globalization.CultureInfo.InvariantCulture)); - sb.Append("&mode=crop"); - } - else - { - sb.Append("?anchor=center"); - sb.Append("&mode=crop"); - } + { + sb.Append("?anchor=center"); + sb.Append("&mode=crop"); + } - } - - if (addCropDimensions) + if (crop!= null && addCropDimensions) { sb.Append("&width=").Append(crop.Width); sb.Append("&height=").Append(crop.Height); } - if (addRandom) + if (cacheBuster) { sb.Append("&rnd=").Append(DateTime.Now.Ticks); }