Adding checks for predefined crops that are still set to default and where a specific resize parameter has been passed into GetCropUrl so that output crop is in same ratio as the predefined crop

This commit is contained in:
Jeavon Leopold
2015-08-19 16:54:03 +01:00
parent 367ba5cddf
commit 3595dd3339
3 changed files with 61 additions and 17 deletions

View File

@@ -124,5 +124,32 @@ namespace Umbraco.Tests.PropertyEditors
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 300, height: 150, preferFocalPoint:true);
Assert.AreEqual(mediaPath + "?anchor=center&mode=crop&width=300&height=150", urlString);
}
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidth()
{
var cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "home", width: 200);
Assert.AreEqual(mediaPath + "?anchor=center&mode=crop&heightratio=0.5962962962962962962962962963&width=200", urlString);
}
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithWidthAndFocalPoint()
{
var cropperJson = "{\"focalPoint\": {\"left\": 0.4275,\"top\": 0.41},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "home", width: 200);
Assert.AreEqual(mediaPath + "?center=0.41,0.4275&mode=crop&heightratio=0.5962962962962962962962962963&width=200", urlString);
}
[Test]
public void GetCropUrl_PreDefinedCropNoCoordinatesWithHeight()
{
var cropperJson = "{\"focalPoint\": {\"left\": 0.5,\"top\": 0.5},\"src\": \"/media/1005/img_0671.jpg\",\"crops\": [{\"alias\": \"home\",\"width\": 270,\"height\": 161}]}";
var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "home", height: 200);
Assert.AreEqual(mediaPath + "?anchor=center&mode=crop&widthratio=1.6770186335403726708074534161&height=200", urlString);
}
}
}

View File

@@ -81,6 +81,7 @@
{
return null;
}
if ((preferFocalPoint && cropDataSet.HasFocalPoint()) || (crop != null && crop.Coordinates == null && cropDataSet.HasFocalPoint()) || (string.IsNullOrEmpty(cropAlias) && cropDataSet.HasFocalPoint()))
{
cropUrl.Append("?center=" + cropDataSet.FocalPoint.Top.ToString(CultureInfo.InvariantCulture) + "," + cropDataSet.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
@@ -100,6 +101,7 @@
cropUrl.Append("?anchor=center");
cropUrl.Append("&mode=crop");
}
return cropUrl.ToString();
}
}

View File

@@ -153,6 +153,12 @@
/// <param name="height">
/// The height of the output image.
/// </param>
/// <param name="imageCropperValue">
/// The Json data from the Umbraco Core Image Cropper property editor
/// </param>
/// <param name="cropAlias">
/// The crop alias.
/// </param>
/// <param name="quality">
/// Quality percentage of the output image.
/// </param>
@@ -162,17 +168,11 @@
/// <param name="imageCropAnchor">
/// The image crop anchor.
/// </param>
/// <param name="imageCropperValue">
/// The Json data from the Umbraco Core Image Cropper property editor
/// </param>
/// <param name="cropAlias">
/// The crop alias.
/// </param>
/// <param name="preferFocalPoint">
/// Use focal point to generate an output image using the focal point instead of the predefined crop if there is one
/// </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>.
/// 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="cacheBusterValue">
/// Add a serialised date of the last edit of the item to ensure client cache refresh when updated
@@ -182,10 +182,10 @@
/// </param>
/// <param name="ratioMode">
/// Use a dimension as a ratio
/// </param>
/// </param>
/// <param name="upScale">
/// If the image should be upscaled to requested dimensions
/// </param>
/// </param>
/// <returns>
/// The <see cref="string"/>.
/// </returns>
@@ -203,8 +203,7 @@
string cacheBusterValue = null,
string furtherOptions = null,
ImageCropRatioMode? ratioMode = null,
bool upScale = true
)
bool upScale = true)
{
if (string.IsNullOrEmpty(imageUrl) == false)
{
@@ -229,11 +228,25 @@
return null;
}
if (crop!= null & useCropDimensions)
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 && crop.Coordinates == null && ratioMode == null && width != null && height == null)
{
var heightRatio = (decimal)crop.Height / (decimal)crop.Width;
imageResizerUrl.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 && crop.Coordinates == null && ratioMode == null && width == null && height != null)
{
var widthRatio = (decimal)crop.Width / (decimal)crop.Height;
imageResizerUrl.Append("&widthratio=" + widthRatio.ToString(CultureInfo.InvariantCulture));
}
}
}
else
@@ -270,23 +283,25 @@
if (ratioMode == ImageCropRatioMode.Width && height != null)
{
//if only height specified then assume a sqaure
// if only height specified then assume a sqaure
if (width == null)
{
width = height;
}
var widthRatio = (decimal)width/(decimal)height;
imageResizerUrl.Append("&widthratio=" + widthRatio.ToString(CultureInfo.InvariantCulture));
var widthRatio = (decimal)width / (decimal)height;
imageResizerUrl.Append("&widthratio=" + widthRatio.ToString(CultureInfo.InvariantCulture));
}
if (ratioMode == ImageCropRatioMode.Height && width != null)
{
//if only width specified then assume a sqaure
// if only width specified then assume a sqaure
if (height == null)
{
height = width;
}
var heightRatio = (decimal)height/(decimal)width;
var heightRatio = (decimal)height / (decimal)width;
imageResizerUrl.Append("&heightratio=" + heightRatio.ToString(CultureInfo.InvariantCulture));
}