Copied to clipboard

Flag this post as spam?

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


  • Peter Laurie 47 posts 121 karma points
    Jul 22, 2021 @ 11:51
    Peter Laurie
    0

    Getting crops from New Media Picker 3

    HI, I am upgrading to Umbraco 8.15 and trying to use the new Media Oicker 3 with crops, like a crop with an alias of blogPost.

    I can select the image and I cannot get it to display the specific crop.

    I have even been through the documentation page here: https://our.umbraco.com/Documentation/Fundamentals/Backoffice/property-editors/built-in-property-editors/Media-Picker-3/

    Here is my code, please can someone shed some light on getting to the crop for me please.

    foreach (var post in Model.Posts) {
    @if (post.HasValue("postMediaImage")) {
    dynamic mediaItem = post.GetProperty("postMediaImage").Value() as Umbraco.Core.Models.PublishedContent.IPublishedContent;
    var t = mediaItem.LocalCrops;
    <div class="postthumb" style="background: url(@t)"></div>
    }
    }
    

    The above gets my image displayed, but no crop version.

    The images use new media Picker 3, and have a crop blogPost. So I am iterating down a list of entries, trying to display a thumbnail of the image for the main post.

    the examples in the documentation do not seem to work:

    @{ foreach (var entry in Model.Medias) { } }

    I cannot seem to replace the Model.Media bit with my post to get to the image and be able to use the GetLocalCropUrl helper.

    Thank you in advance for your help.

    Kind regards,

    Pete

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jul 22, 2021 @ 13:59
    Søren Gregersen
    1

    Hi,

    It seems you are reading the image as a IPublishedContent, when you should be reading it as a MediaWithCrops. This is what is documented here https://our.umbraco.com/Documentation/Fundamentals/Backoffice/property-editors/built-in-property-editors/Media-Picker-3/#multiple-enabled-without-modelsbuilder

    To translate it into your models, the code could look like:

    @foreach (var post in Model.Posts) 
    {
        if (post.HasValue("postMediaImage")) 
        {
            var mediaItem = post.Value<IEnumerable<MediaWithCrops>>("postMediaImage").FirstOrDefault();
            var crops = mediaItem.LocalCrops;
            <div class="postthumb" style="background: url(@Url.GetCropUrl(crops, "cropAlias"))"></div>
        }
    } 
    

    HTH :)

  • Peter Laurie 47 posts 121 karma points
    Jul 22, 2021 @ 17:42
    Peter Laurie
    1

    Hi Søren, Excellent, thank you for the feedback. done the trick. Kind regards,

    Pete

Please Sign in or register to post replies

Write your reply to:

Draft