Merge branch '7.1.0-ImageCropper-JeavonWIP' of https://github.com/Jeavon/Umbraco-CMS into Jeavon-7.1.0-ImageCropper-JeavonWIP

Conflicts:
	src/Umbraco.Web/ImageCropperBaseExtensions.cs
This commit is contained in:
Shannon
2014-03-20 13:18:57 +11:00
2 changed files with 54 additions and 118 deletions

View File

@@ -49,43 +49,6 @@ namespace Umbraco.Web
return imageCrops;
}
internal static bool HasPropertyAndValue(this IPublishedContent publishedContent, string propertyAlias)
{
try
{
if (propertyAlias != null && publishedContent.HasProperty(propertyAlias)
&& publishedContent.HasValue(propertyAlias))
{
var propertyAliasValue = publishedContent.GetPropertyValue<string>(propertyAlias);
if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2)
{
return false;
}
return true;
}
}
catch (Exception ex)
{
LogHelper.Warn<IPublishedContent>("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 false;
}
internal static ImageCropData GetCrop(this ImageCropDataSet dataset, string cropAlias)
{
if (dataset == null || dataset.Crops == null || !dataset.Crops.Any())
@@ -108,65 +71,30 @@ namespace Umbraco.Web
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<string>(propertyAlias);
if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2)
{
var propertyAliasValue = publishedContent.GetPropertyValue<string>(propertyAlias);
if (propertyAliasValue.DetectIsJson() && propertyAliasValue.Length <= 2)
{
return false;
}
var allTheCrops = propertyAliasValue.SerializeToCropDataSet();
var selectedCrop = allTheCrops.GetCrop(cropAlias);
if (selectedCrop != null)
return true;
return false;
}
}
catch (Exception ex)
{
LogHelper.Warn<IPublishedContent>("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;
}
var allTheCrops = propertyAliasValue.SerializeToCropDataSet();
return allTheCrops.GetCrop(cropAlias) != null;
}
}
var allTheCrops = propertyAliasValue.SerializeToCropDataSet();
if (allTheCrops != null && allTheCrops.Crops.Any())
{
var crop = cropAlias != null
? allTheCrops.Crops.First(x => x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant())
: allTheCrops.Crops.First();
if (crop != null)
{
return true;
}
}
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<string>(propertyAlias);
}
}
catch (Exception ex)
{
var cropsProperty = publishedContent.Properties.FirstOrDefault(x => x.PropertyTypeAlias == propertyAlias);
if (cropsProperty != null)
{
propertyValue = cropsProperty.Value.ToString();
}
}
return propertyValue;
}
}
}

View File

@@ -32,7 +32,6 @@ namespace Umbraco.Web
}
}
public static string GetCropUrl(
this IPublishedContent mediaItem,
int? width = null,
@@ -40,19 +39,23 @@ namespace Umbraco.Web
int? quality = null,
Mode? mode = null,
Anchor? anchor = null,
string imageCropperAlias = null,
string imageCropperCropId = null,
string furtherOptions = null,
bool slimmage = false)
string propertyAlias = null,
string cropAlias = null,
string furtherOptions = null)
{
string imageCropperValue = null;
if (mediaItem.HasPropertyAndValueAndCrop(imageCropperAlias, imageCropperCropId))
string mediaItemUrl = null;
if (mediaItem.HasPropertyAndValueAndCrop(propertyAlias, cropAlias))
{
imageCropperValue = mediaItem.GetPropertyValueHack(imageCropperAlias);
imageCropperValue = mediaItem.GetPropertyValue<string>(propertyAlias);
}
return mediaItem != null ? GetCropUrl(mediaItem.Url, width, height, quality, mode, anchor, imageCropperValue, imageCropperCropId, 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;
}
@@ -65,26 +68,44 @@ 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))
{
var imageResizerUrl = new StringBuilder();
imageResizerUrl.Append(imageUrl);
if (!string.IsNullOrEmpty(imageCropperValue) && imageCropperValue.DetectIsJson())
{
var allTheCrops = imageCropperValue.SerializeToCropDataSet();
if (allTheCrops != null && allTheCrops.Crops.Any())
var cropDataSet = imageCropperValue.SerializeToCropDataSet();
imageResizerUrl.Append(cropDataSet.Src);
var crop = cropDataSet.Crops.FirstOrDefault(x => cropAlias != null && x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant());
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");
}
if(allTheCrops.HasCrop(cropAlias))
imageResizerUrl.Append(allTheCrops.GetCropUrl(cropAlias));
}
}
else
{
imageResizerUrl.Append(imageUrl);
if (mode == null)
{
mode = Mode.Pad;
@@ -112,19 +133,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);