Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I have a strange issue on a site whereby the Image media type Upload property editor has been replaced with the Image Cropper ( Umbraco.ImageCropper) property editor. The datatype is configured with a single named Crop to allow editors to manually select a crop area for images in a certain position on the site.
The problem however is that for images that have the crop set, where they are used elsewhere on the site where they shouldn't be cropped they are still returning the named crop instead of the original image (if that makes sense?).
So, when I am calling @img.GetCropUrl(294, 224) I am actually getting a url like this:
Whereas I should actually be getting:
Any ideas why this might be and how to work around it without having to create more unwanted crops for what is a huge media library?
This is for a site running v7.6.12.
Try to use this code:
@Url.GetCropUrl(img, htmlEncode: false, width: 294, height: 224)
Hi Alex. I get the same result, the named crop is returned but at the dimensions supplied.
In the directory where the image is there is the following crop.json file content:
...This is not what I would expect to find as none of the values seem to tally up with the crop information in the url.
OK, so I can answer my own question. having dug through the source code I found this;
internal static ImageCropData GetCrop(this IEnumerable<ImageCropData> dataset, string cropAlias)
var imageCropDatas = dataset.ToArray();
if (dataset == null || imageCropDatas.Any() == false)
return imageCropDatas.FirstOrDefault(x => x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant());
And so as I suspected if there are any crops on the media item but you don't tell it which one you want - it returns the first. I'm not convinced this should be the correct behaviour and would welcome other opinions.
In my mind if I don't give a crop name I want to generate a dynamic crop e.g. one based on the dimensions I provided, not resize and give me the first named crop in the collection.
The solution was to write my on UrlHelper Extension method to return my custom crops.
public static IHtmlString GetImageCropUrl(
this UrlHelper urlHelper,
int? width = null,
int? height = null,
int? quality = null,
ImageCropMode? imageCropMode = null,
bool htmlEncode = true)
string imageUrl = mediaItem.Url;
var imageProcessorUrl = new StringBuilder();
if (imageCropMode == null)
imageCropMode = ImageCropMode.Crop;
imageProcessorUrl.Append("?mode=" + imageCropMode.ToString().ToLower());
if (quality != null)
imageProcessorUrl.Append("&quality=" + quality);
if (width != null)
imageProcessorUrl.Append("&width=" + width);
if (height != null)
imageProcessorUrl.Append("&height=" + height);
var url = imageProcessorUrl.ToString();
return htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(url)) : new HtmlString(url);
I will probably look to extend this further to make use of focal points but this should suffice for now.
Was there ever filed an issue for this?
I'm getting the same behavior in 7.12.x
I'm afraid I honestly can't remember but I suspect not, sorry :(
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted