Copied to clipboard

Flag this post as spam?

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


  • Petr Hruzek 28 posts 100 karma points
    Apr 25, 2014 @ 13:33
    Petr Hruzek
    1

    Getting image dimensions

    Hello!

    I have a custom media type with a cropper property "image". Is there a way to get width and height of the original image? I know I can use native c# methods, but maybe there is something more simple...

  • Matt Redmond 1 post 71 karma points
    Aug 19, 2016 @ 02:54
    Matt Redmond
    0

    Waaaaay late on the reply, but putting this here in case anyone else needs a solution (like I did) :-)

    If you're looking to get the original image dimensions (particularly in views), there's a couple of options.


    (1) Dynamically typed:

    @{ var imageUrl = Umbraco.Media(id).umbracoFile; }
    <img src="@imageUrl" alt=""/>
    

    will return the original image URL (e.g. /media/1234/my-image.jpg).


    (2) Strongly typed:

    Using GetCropUrl without parameters will return a crop without width and height properties set (e.g. /media/1234/my-image.jpg?anchor=center&mode=crop&rnd=...), which should by default use the images native width and height.

    @{ var image = Umbraco.TypedMedia(id); }
    <img src="@image.GetCropUrl()" alt=""/>
    

    Alternatively, if you want to force the height and width to match the original image dimensions, you can pass the umbracoWidth and umbracoHeight property values as parameters to GetCropUrl instead of an alias:

    @{ 
        var image = Umbraco.TypedMedia(id);
        var height = image.GetPropertyValue<int>("height");
        var width = image.GetPropertyValue<int>("width");
    }
    <img src="@(image.GetCropUrl(width, height))" alt="" />
    

    (which I believe is essentially what the alias lookup does behind the scenes)

    If you're doing that a lot, you could move it to a simple helper.

    I personally prefer using strongly typed versions, but YMMV.

  • Robert Foster 459 posts 1820 karma points MVP 2x admin c-trib
    Aug 19, 2016 @ 03:17
    Robert Foster
    0

    Just a note on getting the raw image url from Umbraco.TypedMedia(id):

    you should still be able to do @image.Url instead of having to use the GetCropUrl() extension...

Please Sign in or register to post replies

Write your reply to:

Draft