I'm trying to get the Width and Height of a crop from the built in "Image cropper".
I was hoping for an easy solution like:
var galleryImagesCollection = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("galleryImages");
@foreach(var item in galleryImagesCollection)
{
var imgWidth = item.GetCropUrl("Thumbnail")).width;
var imgHeight = item.GetCropUrl("Thumbnail")).height;
}
you can attempt to cast the "umbracoFile" property to a ImageCropDataSet model (in Umbraco.Web.Models) to get the crop info out of the media item
e.g.
var cropperValue = property.GetPropertyValue("umbracoFile");
var imageCrops = cropperValue as ImageCropDataSet;
if (imageCrops != null)
{
var crop = imageCrops.Crops.FirstOrDefault(x => x.Alias.InvariantEquals("mycrop"));
if (crop != null)
{
var width = crop.Width;
var height = crop.Height;
}
}
That worked - but I was wondering if this is rellay the easiest way to get acces to the cropped image height and width. Isn't there a more "native" code to get the data? :D
I would have the above code wrapped in an extension method returning the crop. Then you could call it on a media item and it could return the crop and then you could do the following in your razor code:
var crop = MediaItem.GetCropInfo("cropName");
crop.Width
crop.Height
Image Cropper - Get Width and height of a crop
Hi
I'm trying to get the Width and Height of a crop from the built in "Image cropper".
I was hoping for an easy solution like:
But no luck with it :o)
Any help would be appreciated
Hi
you can attempt to cast the "umbracoFile" property to a ImageCropDataSet model (in Umbraco.Web.Models) to get the crop info out of the media item
e.g.
Hi Kevin - Thanks a lot :D
That worked - but I was wondering if this is rellay the easiest way to get acces to the cropped image height and width. Isn't there a more "native" code to get the data? :D
Hi,
I don't think there is a 'simple' get me height / width call.
I am not sure (because i haven't tried it) buy you might be able to combine the top to lines into one.
There is a
GetCrop()
function on a ImageCropDataSet, but it's internal to Umbraco - so that is what the rest of the code above is doing really.I would have the above code wrapped in an extension method returning the crop. Then you could call it on a media item and it could return the crop and then you could do the following in your razor code:
Cool, so the walk a round must be to return the object as a "dataSet" i guess :D
Thanks a lot Kevin :D
is working on a reply...