Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mads Sørensen 188 posts 433 karma points
    Mar 19, 2018 @ 14:18
    Mads Sørensen
    0

    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:

    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;
    
     }
    

    But no luck with it :o)

    Any help would be appreciated

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Mar 19, 2018 @ 14:54
    Kevin Jump
    100

    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.

     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;
         }
     }
    
  • Mads Sørensen 188 posts 433 karma points
    Mar 20, 2018 @ 10:30
    Mads Sørensen
    0

    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

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Mar 20, 2018 @ 12:28
    Kevin Jump
    0

    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.

    var crops = property.GetPropertyValue<ImageCropDataSet>("umbracoFile"); 
    

    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:

    var crop = MediaItem.GetCropInfo("cropName"); 
    crop.Width
    crop.Height 
    
  • Mads Sørensen 188 posts 433 karma points
    Mar 20, 2018 @ 12:35
    Mads Sørensen
    0

    Cool, so the walk a round must be to return the object as a "dataSet" i guess :D

    Thanks a lot Kevin :D

Please Sign in or register to post replies

Write your reply to:

Draft