Mediawithcrops from Media
# help-with-umbraco
c
V13 If we've managed to return an "Image" from an examine query, how do we get the "mediawithcrops" version?
l
In the past few projects, I’ve used Examine as a tool to retrieve the correct results, then using the NodeId, gone and retrieved the IPublishedContent for those NodeIds, giving you the best of both worlds. In the world of pagination, had no performance issues with this method. I do however see the benefit of using examine to get everything you need, you could perhaps add an additional field to your chosen index of the cropped image, seems clunky however.
j
That's interesting.. What examine returns for an image is some sort of id, whether its an int, guid or udi depends on version and how it's selected etc. So you probably have some search logic that fetches an IPublishedContent based on the id. But the MediaWithCrops model is not contextually bound to the media itself, it's a mix of the media item and the node it's on (as it has the local crops that are specific to the exact picker it's selected within). So if you select an image on a page, and you set local crops on that image, and it then gets added to examine. Then when you search for it, you will have to take the searchresult and find the specific editor on that content page, then pull the image as a mediawithcrops from the content cache. I wonder if there is a better way to index the local crops somehow so you could grab those from the index and match with the media item to generate the full mediawithcrops model 🤔
c
Ah, the reason is that the page isn't a normal Umbraco page, it shows data from an external database we have no control over. The images are named such that they can be called by knowing the DB table they relate to (one image per table). We got the editors to drop the images into the Umbraco Media section for ease of management. So, that's why we want to be able to get a proper image, with srcsets, etc, out of the Media section by knowing the file name. We've got the image reference from Examine, but not the crops, as I wanted to use Slimsy. I suppose we "could" do the srcsets manually but I was hoping there might be a smarter way.
j
So where do these crops exist then? Is it global crops on the image cropper datatype? Because that may still be possible What is it you are trying to do to render the srcsets that is not working? This approach should still work: https://docs.umbraco.com/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/image-cropper#get-all-the-crop-urls-for-a-specific-image
c
The crops are just global crops. I've never used the local variety yet. So the issue is that I can't get hold of the MediaWithCrops properties.
j
IPublishedContent contains crops as well, but only the global ones. Check out the link in my message above to see an example of pulling all possible crops from an IPublishedContent model of media
c
That link requires an IPublishedContent "GetCropUrls(IPublishedContent image)" which I don't have. All I have is an orphaned image. I think I might have to generated the srcsets without slimsy, which means I lose the focus points.
So I tried to generate a new MediaWithCrops(Image, null, null); It looked promising but generated an exception becuase, although the MediaWithCrops item had no local crops, it also didn't have any local crop properties, which I think is what caused Slimsy to bomb. Comparing the same image when called with a picker and just as an orphan, they both have no local crops, but at least the one from the picker had local crop properties, which were null, rather than no properties at all. Not sure if I should raise an issue as it's a pretty niche use case.
In the end, I just copied the HTML from another Slimsy'd image, changed the source and ratios and put that up. it works fine.
33 Views