Copied to clipboard

Flag this post as spam?

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


  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    May 23, 2016 @ 10:11
    Bjarne Fyrstenborg
    1

    Get image inside grid editor as media

    Hi..

    I am looking for the first image inside grid editor and would like to return it as IPublishedContent (media).

    I can find the path, but is there a way to get is as IPublishedContent, like when using Umbraco.Media(id)?

    This is how I have done it for now:

    var pageContent = Html.GetGridHtml(node, "pageContent");
                    imageIds = string.Join(",", FetchLinksFromSource(pageContent.ToString()));
    
    public List<string> FetchLinksFromSource(string htmlSource)
        {
            List<string> mediaIds = new List<string>();
            string regexImgSrc = @"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";
            MatchCollection matchesImgSrc = Regex.Matches(htmlSource, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            foreach (Match m in matchesImgSrc)
            {
                string src = m.Groups[1].Value;
                if(!string.IsNullOrEmpty(src))
                {
                    var mediaIndex = src.IndexOf("/media/");
                    var offset = src.IndexOf("/", mediaIndex + 1);
                    src = src.Substring(mediaIndex + 7, offset - 1);
                }
    
                if(!string.IsNullOrEmpty(src))
                {
                    mediaIds.Add(src);
                }
            }
            return mediaIds;
        }
    

    In this line string src = m.Groups[1].Value; it is returning the path to the image incl. querystring (width, height, crop etc). But the returned mediaIds are actually the media folder ids (in path).

    So I have to take another approach. MediaService seems to have a method for that .GetMediaByPath().

    Is that the only way I can get the Media or is there another way?

    /Bjarne

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    May 23, 2016 @ 12:27
    Bjarne Fyrstenborg
    0

    I have changed this part:

    string src = m.Groups[1].Value;
    if (!string.IsNullOrEmpty(src))
    {
        var mediaIndex = src.IndexOf("/media/");
        var offset = src.IndexOf("?", mediaIndex + 1);
        src = src.Substring(mediaIndex, offset);
    }
    
    if (!string.IsNullOrEmpty(src))
    {
        var ms = ApplicationContext.Current.Services.MediaService;
        var media = ms.GetMediaByPath(src);
        mediaIds.Add(media.Id.ToString());
    }
    

    However even the value og src is /media/43852/img_1282.jpg and by looking in Media section it has ID: 44375, but var media is returning null.

    It has been reported and fixed here http://issues.umbraco.org/issue/U4-6004 but it seems to be an issue in Umbraco 7.4.1 as well.

    /Bjarne

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    May 23, 2016 @ 14:05
    Bjarne Fyrstenborg
    100

    We use an "image" grid editor in grid content. So I got it working by taking a different approach in this case.

    public IPublishedContent GetSearchResultImage(IPublishedContent node)
    {
        var imageIds = node.GetPropertyValue<string>("shareImage");
        if (imageIds == null)
        {
            if (node.DocumentTypeAlias == "Destination")
            {
                imageIds = node.GetPropertyValue<string>("destinationImages");
            }
            if (node.DocumentTypeAlias == "Hotel")
            {
                imageIds = node.GetPropertyValue<string>("hotelImages");
            }
            if (node.DocumentTypeAlias == "Tekstside")
            {
                var pageContent = node.GetPropertyValue<string>("pageContent");
                if(!string.IsNullOrWhiteSpace(pageContent))
                {
                    JObject nodeJObject = JObject.Parse(pageContent);
                    var imgTokens = nodeJObject.SelectTokens("$..controls[?(@editor.alias == 'image')].value");
                    List<string> ids = new List<string>();
                    foreach (var e in imgTokens) //.Take(1)
                    {
                        var v = e.First["image"].Value<string>("value");
                        ids.Add(v);
                    }
                    imageIds = string.Join(",", ids);
                }
            }
        }
    
        if (string.IsNullOrEmpty(imageIds))
        {
            return null;
        }
    
        var imageId = imageIds.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).FirstOrDefault();
        if (imageId > 0)
        {
            return Umbraco.Media(imageId);
        }
        return null;
    }
    

    /Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft