From 5c8440a7d67b8228a447bd8075424cf1becbe26e Mon Sep 17 00:00:00 2001 From: Jeavon Leopold Date: Tue, 18 Mar 2014 15:20:48 +0000 Subject: [PATCH 1/4] ImageCropper helpers WIP --- src/Umbraco.Web/ImageCropperBaseExtensions.cs | 112 ++++-------------- .../ImageCropperTemplateExtensions.cs | 22 ++-- 2 files changed, 29 insertions(+), 105 deletions(-) diff --git a/src/Umbraco.Web/ImageCropperBaseExtensions.cs b/src/Umbraco.Web/ImageCropperBaseExtensions.cs index f589deabee..6041802fe6 100644 --- a/src/Umbraco.Web/ImageCropperBaseExtensions.cs +++ b/src/Umbraco.Web/ImageCropperBaseExtensions.cs @@ -52,115 +52,45 @@ namespace Umbraco.Web internal static bool HasPropertyAndValue(this IPublishedContent publishedContent, string propertyAlias) - { - try + { + if (propertyAlias != null && publishedContent.HasProperty(propertyAlias) + && publishedContent.HasValue(propertyAlias)) { - - if (propertyAlias != null && publishedContent.HasProperty(propertyAlias) - && publishedContent.HasValue(propertyAlias)) + var propertyAliasValue = publishedContent.GetPropertyValue(propertyAlias); + if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) { - var propertyAliasValue = publishedContent.GetPropertyValue(propertyAlias); - if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) - { - return false; - } - return true; + return false; } - } - catch (Exception ex) - { - LogHelper.Warn("The cache unicorn is not happy with node id: " + publishedContent.Id + " - http://issues.umbraco.org/issue/U4-4146"); - - var cropsProperty = publishedContent.Properties.FirstOrDefault(x => x.PropertyTypeAlias == propertyAlias); - - if (cropsProperty != null && !string.IsNullOrEmpty(cropsProperty.Value.ToString())) - { - var propertyAliasValue = cropsProperty.Value.ToString(); - if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) - { - return false; - } - return true; - } - } + return true; + } return false; } internal static bool HasPropertyAndValueAndCrop(this IPublishedContent publishedContent, string propertyAlias, string cropAlias) { - try + if (propertyAlias != null && publishedContent.HasProperty(propertyAlias) + && publishedContent.HasValue(propertyAlias)) { - if (propertyAlias != null && publishedContent.HasProperty(propertyAlias) - && publishedContent.HasValue(propertyAlias)) + var propertyAliasValue = publishedContent.GetPropertyValue(propertyAlias); + if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) { - var propertyAliasValue = publishedContent.GetPropertyValue(propertyAlias); - if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) - { - return false; - } - var allTheCrops = propertyAliasValue.SerializeToCropDataSet(); - if (allTheCrops != null && allTheCrops.Crops.Any()) - { - var crop = cropAlias != null - ? allTheCrops.Crops.First(x => x.Alias ==cropAlias) - : allTheCrops.Crops.First(); - if (crop != null) - { - return true; - } - } return false; } - } - catch (Exception ex) - { - LogHelper.Warn("The cache unicorn is not happy with node id: " + publishedContent.Id + " - http://issues.umbraco.org/issue/U4-4146"); - var cropsProperty = publishedContent.Properties.FirstOrDefault(x => x.PropertyTypeAlias == propertyAlias); - - if (cropsProperty != null && !string.IsNullOrEmpty(cropsProperty.Value.ToString())) + var allTheCrops = propertyAliasValue.SerializeToCropDataSet(); + if (allTheCrops != null && allTheCrops.Crops.Any()) { - var propertyAliasValue = cropsProperty.Value.ToString(); - if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) + var crop = cropAlias != null + ? allTheCrops.Crops.First(x => x.Alias ==cropAlias) + : allTheCrops.Crops.First(); + if (crop != null) { - return false; + return true; } - var allTheCrops = propertyAliasValue.SerializeToCropDataSet(); - if (allTheCrops != null && allTheCrops.Crops.Any()) - { - var crop = cropAlias != null - ? allTheCrops.Crops.First(x => x.Alias ==cropAlias) - : allTheCrops.Crops.First(); - if (crop != null) - { - return true; - } - } - return false; } - } + return false; + } return false; } - internal static string GetPropertyValueHack(this IPublishedContent publishedContent, string propertyAlias) - { - string propertyValue = null; - try - { - if (propertyAlias != null && publishedContent.HasProperty(propertyAlias) - && publishedContent.HasValue(propertyAlias)) - { - propertyValue = publishedContent.GetPropertyValue(propertyAlias); - } - } - catch (Exception ex) - { - var cropsProperty = publishedContent.Properties.FirstOrDefault(x => x.PropertyTypeAlias == propertyAlias); - if (cropsProperty != null) - { - propertyValue = cropsProperty.Value.ToString(); - } - } - return propertyValue; - } } } diff --git a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs index 783e25f4ae..fbc1c3dba2 100644 --- a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs +++ b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs @@ -32,7 +32,6 @@ namespace Umbraco.Web } } - public static string GetCropUrl( this IPublishedContent mediaItem, int? width = null, @@ -40,19 +39,19 @@ namespace Umbraco.Web int? quality = null, Mode? mode = null, Anchor? anchor = null, - string imageCropperAlias = null, - string imageCropperCropId = null, + string propertyAlias = null, + string cropAlias = null, string furtherOptions = null, bool slimmage = false) { string imageCropperValue = null; - if (mediaItem.HasPropertyAndValueAndCrop(imageCropperAlias, imageCropperCropId)) + if (mediaItem.HasPropertyAndValueAndCrop(propertyAlias, cropAlias)) { - imageCropperValue = mediaItem.GetPropertyValueHack(imageCropperAlias); + imageCropperValue = mediaItem.GetPropertyValue(propertyAlias); } - return mediaItem != null ? GetCropUrl(mediaItem.Url, width, height, quality, mode, anchor, imageCropperValue, imageCropperCropId, furtherOptions, slimmage) : string.Empty; + return mediaItem != null ? GetCropUrl(mediaItem.Url, width, height, quality, mode, anchor, imageCropperValue, cropAlias, furtherOptions, slimmage) : string.Empty; } @@ -71,17 +70,12 @@ namespace Umbraco.Web if (!string.IsNullOrEmpty(imageUrl)) { var imageResizerUrl = new StringBuilder(); - imageResizerUrl.Append(imageUrl); + //imageResizerUrl.Append(imageUrl); if (!string.IsNullOrEmpty(imageCropperValue) && imageCropperValue.DetectIsJson()) { - var allTheCrops = imageCropperValue.SerializeToCropDataSet(); - if (allTheCrops != null && allTheCrops.Crops.Any()) - { - - if(allTheCrops.HasCrop(cropAlias)) - imageResizerUrl.Append(allTheCrops.GetCropUrl(cropAlias)); - } + var cropDataSet = imageCropperValue.SerializeToCropDataSet(); + imageResizerUrl.Append(cropDataSet.Src + cropDataSet.GetCropUrl(cropAlias)); } else { From c6e1235ac4e233db8c4300fec0d51af005999fde Mon Sep 17 00:00:00 2001 From: Jeavon Leopold Date: Wed, 19 Mar 2014 09:52:53 +0000 Subject: [PATCH 2/4] Fixing GetCropUrl overload --- .../ImageCropperTemplateExtensions.cs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs index fbc1c3dba2..063c170200 100644 --- a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs +++ b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs @@ -70,15 +70,42 @@ namespace Umbraco.Web if (!string.IsNullOrEmpty(imageUrl)) { var imageResizerUrl = new StringBuilder(); - //imageResizerUrl.Append(imageUrl); + // imageResizerUrl.Append(imageUrl); if (!string.IsNullOrEmpty(imageCropperValue) && imageCropperValue.DetectIsJson()) { var cropDataSet = imageCropperValue.SerializeToCropDataSet(); - imageResizerUrl.Append(cropDataSet.Src + cropDataSet.GetCropUrl(cropAlias)); + imageResizerUrl.Append(cropDataSet.Src); + var crop = cropDataSet.Crops.FirstOrDefault(x => x.Alias == cropAlias); + if (crop != null && crop.Coordinates != null) + { + imageResizerUrl.Append("?crop="); + imageResizerUrl.Append(crop.Coordinates.X1).Append(","); + imageResizerUrl.Append(crop.Coordinates.Y1).Append(","); + imageResizerUrl.Append(crop.Coordinates.X2).Append(","); + imageResizerUrl.Append(crop.Coordinates.Y2); + imageResizerUrl.Append("&cropmode=percentage"); + } + else + { + if (cropDataSet.HasFocalPoint()) + { + imageResizerUrl.Append("?center=" + cropDataSet.FocalPoint.Top + "," + cropDataSet.FocalPoint.Left); + imageResizerUrl.Append("&mode=crop"); + } + else + { + imageResizerUrl.Append("?anchor=center"); + imageResizerUrl.Append("&mode=crop"); + } + + } + + //imageResizerUrl.Append(cropDataSet.Src + cropDataSet.GetCropUrl(cropAlias)); } else { + imageResizerUrl.Append(imageUrl); if (mode == null) { mode = Mode.Pad; From e8e8b536f1e87d44e7fe0447ed15274d844ac798 Mon Sep 17 00:00:00 2001 From: Jeavon Leopold Date: Wed, 19 Mar 2014 16:12:30 +0000 Subject: [PATCH 3/4] Fix for GetCropUrl method when mediaItem.Url has JSON data --- .../ImageCropperTemplateExtensions.cs | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs index 063c170200..3f9602a479 100644 --- a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs +++ b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs @@ -41,17 +41,21 @@ namespace Umbraco.Web Anchor? anchor = null, string propertyAlias = null, string cropAlias = null, - string furtherOptions = null, - bool slimmage = false) + string furtherOptions = null) { string imageCropperValue = null; + string mediaItemUrl = null; + if (mediaItem.HasPropertyAndValueAndCrop(propertyAlias, cropAlias)) { imageCropperValue = mediaItem.GetPropertyValue(propertyAlias); } - return mediaItem != null ? GetCropUrl(mediaItem.Url, width, height, quality, mode, anchor, imageCropperValue, cropAlias, furtherOptions, slimmage) : string.Empty; + //this probably shouldn't be needed but it is currently as mediaItem.Url is populated with full crop JSON + mediaItemUrl = mediaItem.Url.DetectIsJson() ? mediaItem.Url.SerializeToCropDataSet().Src : mediaItem.Url; + + return mediaItem != null ? GetCropUrl(mediaItemUrl, width, height, quality, mode, anchor, imageCropperValue, cropAlias, furtherOptions) : string.Empty; } @@ -64,8 +68,7 @@ namespace Umbraco.Web Anchor? anchor = null, string imageCropperValue = null, string cropAlias = null, - string furtherOptions = null, - bool slimmage = false) + string furtherOptions = null) { if (!string.IsNullOrEmpty(imageUrl)) { @@ -100,8 +103,6 @@ namespace Umbraco.Web } } - - //imageResizerUrl.Append(cropDataSet.Src + cropDataSet.GetCropUrl(cropAlias)); } else { @@ -133,19 +134,6 @@ namespace Umbraco.Web imageResizerUrl.Append("&height=" + height); } - if (slimmage) - { - if (width == null) - { - imageResizerUrl.Append("&width=300"); - } - if (quality == null) - { - imageResizerUrl.Append("&quality=90"); - } - imageResizerUrl.Append("&slimmage=true"); - } - if (furtherOptions != null) { imageResizerUrl.Append(furtherOptions); From d88745bf49beb4882559359a85626452f274c684 Mon Sep 17 00:00:00 2001 From: Jeavon Leopold Date: Wed, 19 Mar 2014 16:53:13 +0000 Subject: [PATCH 4/4] Adding ToLowerInvariant to compares and also removing a redundant method copied in from my cropper package but not needed --- src/Umbraco.Web/ImageCropperBaseExtensions.cs | 19 +------------------ .../ImageCropperTemplateExtensions.cs | 3 +-- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/src/Umbraco.Web/ImageCropperBaseExtensions.cs b/src/Umbraco.Web/ImageCropperBaseExtensions.cs index 6041802fe6..90761a04f1 100644 --- a/src/Umbraco.Web/ImageCropperBaseExtensions.cs +++ b/src/Umbraco.Web/ImageCropperBaseExtensions.cs @@ -49,23 +49,6 @@ namespace Umbraco.Web return imageCrops; } - - - internal static bool HasPropertyAndValue(this IPublishedContent publishedContent, string propertyAlias) - { - if (propertyAlias != null && publishedContent.HasProperty(propertyAlias) - && publishedContent.HasValue(propertyAlias)) - { - var propertyAliasValue = publishedContent.GetPropertyValue(propertyAlias); - if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2) - { - return false; - } - return true; - } - return false; - } - internal static bool HasPropertyAndValueAndCrop(this IPublishedContent publishedContent, string propertyAlias, string cropAlias) { if (propertyAlias != null && publishedContent.HasProperty(propertyAlias) @@ -80,7 +63,7 @@ namespace Umbraco.Web if (allTheCrops != null && allTheCrops.Crops.Any()) { var crop = cropAlias != null - ? allTheCrops.Crops.First(x => x.Alias ==cropAlias) + ? allTheCrops.Crops.First(x => x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant()) : allTheCrops.Crops.First(); if (crop != null) { diff --git a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs index 3f9602a479..78e3e64ec4 100644 --- a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs +++ b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs @@ -73,13 +73,12 @@ namespace Umbraco.Web if (!string.IsNullOrEmpty(imageUrl)) { var imageResizerUrl = new StringBuilder(); - // imageResizerUrl.Append(imageUrl); if (!string.IsNullOrEmpty(imageCropperValue) && imageCropperValue.DetectIsJson()) { var cropDataSet = imageCropperValue.SerializeToCropDataSet(); imageResizerUrl.Append(cropDataSet.Src); - var crop = cropDataSet.Crops.FirstOrDefault(x => x.Alias == cropAlias); + var crop = cropDataSet.Crops.FirstOrDefault(x => cropAlias != null && x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant()); if (crop != null && crop.Coordinates != null) { imageResizerUrl.Append("?crop=");