[Solved] How do we get image height and width from a MediaWithCrops?
c
How do we get image height and width (i.e. UmbracoWidth & UmbracoHeight) from a MediaWithCrops object?
h
You need to cast to (Image), but you may need to retrieve it as an IPublishedContent , I don't know if you can cast MediaWithCrops to Image, never tried
a
I'm fairly certain you can't cast an
MediaWithCrops
to the generated model (such as
Image
), but
MediaWithCrops
has a
Content
property that exposes the underlying
IPublishedContent
. A
MediaWithCrops
instance is actually also an
IPublishedContent
, so you should be able to use
mediaWithCrops.Value<int>("umbracoWidth")
.
c
Didn't work I'm afraid. Debugging shows me there's a myImage.Content.Properties["umbracoWidth"] but I just can't seem to get hold of it.
a
What about
mediaWithCrops.Value<int>("umbracoWidth")
or
mediaWithCrops.Content.Value<int>("umbracoWidth")
?
h
mediaWithCrops.Value<int>("umbracoWidth").
worked for me when I tested it
c
It's an Umbraco.Label and I don't actually see a value or it's 0, which it shouldn't be
Is "mediaWithCrops" taking the place of your image object there?
a
Does it have a value when you view the image in the backoffice?
c
Good point!
a
It's the
MediaWithCrops
item
h
c
Er..... No, width, height, size and type are all empty!
The site has just been migrated using uSync.Migrations. Wonder if I have to resave all the media to get it all populated.
That's exactly what it was. Saved the media item and it worked for me too!
a
Same thing for the site I'm working. My coworker also used uSync Migrations, but I'm not entirely sure of the process. Does the media files exist on disk? I suppose if they didn't exist on disk (or blob storage or similar) at the time of the migration, Umbraco couldn't find the files and therefore couldn't set width, height etc.
c
First time I've used uSync.Migrations to be fair. 🤦‍♂️
Thanks for your help 🙂
Just in case anyone else comes across this. A quick way to save all your media if you only have a few levels (sub-folders) like I did, just pop this in a view and adjust the level for each run:-
Copy code
@inject IMediaService _mediaService

@{
    Layout = null;

    foreach (var item in _mediaService.GetByLevel(3)) {
        _mediaService.Save(item);
    }
    
}