Copied to clipboard

Flag this post as spam?

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


  • Kevin C. Halpin 27 posts 108 karma points
    May 25, 2018 @ 19:11
    Kevin C. Halpin
    0

    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

  • Anders Bjerner 467 posts 2781 karma points MVP 6x admin c-trib
    May 25, 2018 @ 21:18
    Anders Bjerner
    1

    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?

  • Kevin C. Halpin 27 posts 108 karma points
    May 26, 2018 @ 13:27
    Kevin C. Halpin
    1

    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.

    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.

  • Anders Bjerner 467 posts 2781 karma points MVP 6x admin c-trib
    May 27, 2018 @ 12:27
    Anders Bjerner
    2

    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 of IPublishedContent representing the selected media.

    image

    I would then get the thumbnail URL like as shown below:

    IPublishedContent thumbnail = ch.GetPropertyValue<IPublishedContent>("thumbnail");
    
    if (thumbnail != null)
    {
        hotel.Thumbnail = thumbnail.GetCropUrl(250, 250);
    }
    

    Notice that GetCropUrl is used on the IPublishedContent, not on the helper.

    Hope that helps ;)

  • Kevin C. Halpin 27 posts 108 karma points
    May 28, 2018 @ 20:07
    Kevin C. Halpin
    0

    Yeah this worked. And my picture is now showing. Thanks man :))))

Please Sign in or register to post replies

Write your reply to:

Draft