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...
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.
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...
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:
will return the original image URL (e.g.
/media/1234/my-image.jpg
).(2) Strongly typed:
Using
GetCropUrl
without parameters will return a crop withoutwidth
andheight
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.Alternatively, if you want to force the height and width to match the original image dimensions, you can pass the
umbracoWidth
andumbracoHeight
property values as parameters toGetCropUrl
instead of an alias:(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.
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 theGetCropUrl()
extension...is working on a reply...