Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 886 posts 2415 karma points
    Nov 06, 2014 @ 12:52
    Claushingebjerg
    0

    Getting image in Razor Macro

    Im modifying The Full tText Search Macro to suit my needs. The package uses a Razor macro.

    Im trying to render an image from a document type attribute.

    dynamic node = Library.NodeById(@fullTextId);
    var personImage = @Library.MediaById(node.ImageID);
    

    where @fullTextId returns the ID of the node, and @personImage returns "umbraco.MacroEngines.DynamicMedia".

    So far so good!

    <img src="@personImage.UmbracoFile" />

     should then return the image, but instead it returns a blob of Json

    {src: '/media/1029/m_fv.jpg', crops: [ { "alias": "testcrop", "width": 300, "height": 300 } ]}

     

    How do i get the image?

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 06, 2014 @ 12:54
    Jeavon Leopold
    0

    Hi Claus,

    Do you have a link for that Macro code?

    Thanks,

    Jeavon

  • Claushingebjerg 886 posts 2415 karma points
    Nov 06, 2014 @ 14:00
    Claushingebjerg
    0

    Hi Jeavon

    I dont have a link to the macro script directly, but its this package:

    http://our.umbraco.org/projects/website-utilities/full-text-search

    I've tried this approach as well:

    if (!string.IsNullOrEmpty(@node.imageID.ToString()))
    {
        dynamic mediaItem = Library.MediaById(@node.imageID);
        <img src="@mediaItem.umbracoFile" alt="@mediaItem.Name" />
    }   


    Which returns:

    <img src="{src: '/media/1029/m_fv.jpg', crops: [
      {
        &quot;alias&quot;: &quot;testcrop&quot;,
        &quot;width&quot;: 300,
        &quot;height&quot;: 300
      }
    ]}" alt="m_fv.JPG">

     

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 06, 2014 @ 14:37
    Jeavon Leopold
    0

    Ah ok, think it's this one then?

    I would probably rewrite it as a Partial View Macro alternatively there is a solution here for use in a Macro Script.

    Jeavon

  • Claushingebjerg 886 posts 2415 karma points
    Nov 06, 2014 @ 15:53
    Claushingebjerg
    0

    Yeah, if i knew how ;).

    Using your eample from the link above

    @using Newtonsoft.Json
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @using System.Xml.XPath
    @using Governor.Umbraco.FullTextSearch.Extensions
    @inherits umbraco.MacroEngines.DynamicNodeContext
    var sidebarImage = node.GetPropertyValue("imageID");
    var imageCrops = JsonConvert.DeserializeObject<ImageCropDataSet>(sidebarImage);

    throws an error

    @sidebarImage return 2601, so it must be something with JsonConvert

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 06, 2014 @ 15:57
    Jeavon Leopold
    0

    Try like this:

    if (!string.IsNullOrEmpty(@node.imageID.ToString()))
    {
        var mediaItem = Library.MediaById(node.imageID);
        var imageCrops = JsonConvert.DeserializeObject<ImageCropDataSet>(mediaItem.umbracoFile.ToString());
    }  
    
  • Claushingebjerg 886 posts 2415 karma points
    Nov 06, 2014 @ 16:05
    Claushingebjerg
    0

    We're getting closer :). But now its getting weird :)

    <img src="@imageCrops"/>  <p>@imageCrops</p>

    Returns

    <img src="Umbraco.Web.Models.ImageCropDataSet"> <p>/media/1029/m_fv.jpg</p>
  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 06, 2014 @ 16:08
    Jeavon Leopold
    100
    <img src="@imageCrops.Src"/>  <p>@imageCrops</p>
    
  • Claushingebjerg 886 posts 2415 karma points
    Nov 06, 2014 @ 16:12
    Claushingebjerg
    0

    Codegarden Beer Unlocked ;)

  • Claushingebjerg 886 posts 2415 karma points
    Nov 06, 2014 @ 16:14
    Claushingebjerg
    0

    And ill try my luck at converting the macro :)

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 06, 2014 @ 16:14
    Jeavon Leopold
    0

    Lol :) Glad you've got it working

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 06, 2014 @ 16:26
    Jeavon Leopold
    0

    I had a quick look and it seems you mainly need to change the many Parameter.property to Model.MacroParameters["property"].ToString() and add the inherits @inherits Umbraco.Web.Macros.PartialViewMacroPage

  • Claushingebjerg 886 posts 2415 karma points
    Nov 07, 2014 @ 15:25
    Claushingebjerg
    0

    See... I knew it was too good to be true :)

    So:

    var mediaItem = Library.MediaById(node.imageID);
    var imageCrops = JsonConvert.DeserializeObject<ImageCropDataSet>(mediaItem.umbracoFile.ToString());
    <img src="@imageCrops.Src"/>

    Gets me the image just fine

    Buuuuuuuut how do i get a cropped image? Neither of the following dows the trick:

    <img src="@imageCrops.GetResponsiveImageUrl(100, 100)" />
    <img src="@imageCrops.Src.GetResponsiveImageUrl(100, 100)" />
    <img src="@mediaItem.GetResponsiveImageUrl(100, 100)" />
  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 07, 2014 @ 15:39
    Jeavon Leopold
    0

    Ah, now you really are in the realm of needing to convert it to a Partial View Macro. I can think of a potential workaround but really.....????

  • Claushingebjerg 886 posts 2415 karma points
    Nov 07, 2014 @ 15:46
    Claushingebjerg
    0

    I tried to convert, but it didnt work.

    Have pinged the creator for a partial view macro.

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 07, 2014 @ 16:49
    Jeavon Leopold
    0

    Ok, how about this unnatural thing:

    var umbHelper = new UmbracoHelper(UmbracoContext.Current);
    var typedMedia = umbHelper.TypedMedia((int)node.imageID);
    
    <img src="@typedMedia.GetResponsiveImageUrl(100, 100)" />
    
  • Claushingebjerg 886 posts 2415 karma points
    Nov 10, 2014 @ 12:39
    Claushingebjerg
    0
    Hmmmm, that throws an error, but digging into it i get the following:
    var umbHelper = new UmbracoHelper(UmbracoContext.Current);
    Returns Umbraco.Web.UmbracoHelper
    var typedMedia = umbHelper.TypedMedia((int)node.imageID);
    returns an error, but changed to
    var typedMedia = umbHelper.TypedMedia(node.imageID);
    It returns Umbraco.Web.PublishedCache.XmlPublishedCache.PublishedMediaCache+DictionaryPublishedContent
    Does that get us any nearer?
  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 10, 2014 @ 13:21
    Jeavon Leopold
    0

    That's good!

    Then can't you do:

    var umbHelper = new UmbracoHelper(UmbracoContext.Current);
    var typedMedia = umbHelper.TypedMedia(node.imageID);
    
    <img src="@typedMedia.GetResponsiveImageUrl(100, 100)" />
    
  • Claushingebjerg 886 posts 2415 karma points
    Nov 10, 2014 @ 13:27
    Claushingebjerg
    0

    Nope, that throws an error...

    It works up to the <img> tag

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 10, 2014 @ 13:30
    Jeavon Leopold
    0

    Ah, you will need a using

    @using Slimsy;

    Otherwise, could you post the exception?

  • Claushingebjerg 886 posts 2415 karma points
    Nov 10, 2014 @ 14:31
    Claushingebjerg
    0

    See, thats the fun part about these macro scripts, they dont post the exception, just:

    Error loading MacroEngine script (file: FullTextSearch.cshtml) 

    which is pretty useless. And being a frontend, no VS im once again a  bit lost :).

    I have these using's:

    @using Newtonsoft.Json
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @using System.Xml.XPath
    @using Slimsy
    @using Governor.Umbraco.FullTextSearch.Extensions
    @inherits umbraco.MacroEngines.DynamicNodeContext

    but still getting an error...

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies