Get Cropped image url in Umbraco API controller - v7.10.4
Hi all,
Every version of Umbraco is different than the last one. I've been searching and trying to get the crop image url. I have Umbraco API controller and I want to return JSON result. I am using Umbraco.TypedContent(id) and so far I have succeeded to get every type of property but this. There is no GetCropUrl method on the variable that I created with the method mentioned above. I've read that I need to use UrlHelper in Umbraco v7.3.5+ but there is no GetCropUrl on that either. Also when I use GetProperty method I can see in Value that I have a JSON that contains the original image path along with information about focal point and available crops so I am thinking can I build the url myself using that information? The weird thing is that the path leads to media but when I open Media section through the CMS the image does not exist there. When I paste the url in the browser it gives me "page not found" error.
Can your share some of your code for the API controller? Then I'd be happy to have a look at it ;)
Anyways - do you by any change know whether you're using ModelsBuilder? If you're currently running Umbraco 7.10.4 and using ModelsBuilder, that might be the reason why things seems a bit different from previous versions of Umbraco. If you aren't sure , that's fine, I can try to give examples for both using ModelsBuilder and not using it.
That you're not able to see the image in the backoffice might be an entirely different issue. Is is just for one particular image, or all images/media in the media section? Can you check whether the file exists on disk?
I am not sure if I am using ModelsBuilder or not as I don't have much experience with Umbraco, but I will be more than happy to share some of the code.
public JsonResult<List<Hotel>> Hotels()
{
var content = Umbraco.TypedContent(1055);
var result = new List<Hotel>();
foreach (var ch in content.Children)
{
var hotel = new Hotel();
hotel.ID = ch.Id;
hotel.Title = ch.GetProperty("title").Value.ToString();
hotel.Description = ch.GetProperty("description").Value.ToString();
hotel.Price= Convert.ToDouble(ch.GetProperty("price").Value);
var temp = ch.GetProperty("thumbnail");
result.Add(hotel);
}
return Json(result);
}
This temp variable is actually the one I need to change to somehow get the thumbnail property which is cropped image. The name of the crop is also thumbnail.
As for the image not showing in the Media section, this is only happening with cropped image property. When I choose an image from the Content section (I choose image from my local computer ) I assume it is supposed to automatically show up in the Media as well. I mean that's what's happening when I use for example Media Picker property. Even If I don't have the images in the Media section when I upload them from my local computer they also show up in Media.
I'm assuming the data type of your thumbnail property is Media Picker (see screenshot), in which case the value of the property will be an instance of IPublishedContent representing the selected media.
I would then get the thumbnail URL like as shown below:
Get Cropped image url in Umbraco API controller - v7.10.4
Hi all,
Every version of Umbraco is different than the last one. I've been searching and trying to get the crop image url. I have Umbraco API controller and I want to return JSON result. I am using Umbraco.TypedContent(id) and so far I have succeeded to get every type of property but this. There is no GetCropUrl method on the variable that I created with the method mentioned above. I've read that I need to use UrlHelper in Umbraco v7.3.5+ but there is no GetCropUrl on that either. Also when I use GetProperty method I can see in Value that I have a JSON that contains the original image path along with information about focal point and available crops so I am thinking can I build the url myself using that information? The weird thing is that the path leads to media but when I open Media section through the CMS the image does not exist there. When I paste the url in the browser it gives me "page not found" error.
Please help
Hi Kevin,
Can your share some of your code for the API controller? Then I'd be happy to have a look at it ;)
Anyways - do you by any change know whether you're using ModelsBuilder? If you're currently running Umbraco 7.10.4 and using ModelsBuilder, that might be the reason why things seems a bit different from previous versions of Umbraco. If you aren't sure , that's fine, I can try to give examples for both using ModelsBuilder and not using it.
That you're not able to see the image in the backoffice might be an entirely different issue. Is is just for one particular image, or all images/media in the media section? Can you check whether the file exists on disk?
Hi Andres
I am not sure if I am using ModelsBuilder or not as I don't have much experience with Umbraco, but I will be more than happy to share some of the code.
This temp variable is actually the one I need to change to somehow get the thumbnail property which is cropped image. The name of the crop is also thumbnail.
As for the image not showing in the Media section, this is only happening with cropped image property. When I choose an image from the Content section (I choose image from my local computer ) I assume it is supposed to automatically show up in the Media as well. I mean that's what's happening when I use for example Media Picker property. Even If I don't have the images in the Media section when I upload them from my local computer they also show up in Media.
Hi again,
I'm assuming the data type of your
thumbnail
property is Media Picker (see screenshot), in which case the value of the property will be an instance ofIPublishedContent
representing the selected media.I would then get the thumbnail URL like as shown below:
Notice that
GetCropUrl
is used on theIPublishedContent
, not on the helper.Hope that helps ;)
Yeah this worked. And my picture is now showing. Thanks man :))))
is working on a reply...