Copied to clipboard

Flag this post as spam?

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


  • Fredrik Öberg 7 posts 47 karma points
    May 13, 2014 @ 10:51
    Fredrik Öberg
    0

    Image cropper getting original image/url

    Hello

    I'm pulling my hair for this one. I want to get the source path to the image (whithout any crop) so i can show the fullsize image in a fancybox. The property src on productImage does hold the path to the original image but I can't seem to access it.

    In this code i'm trying to iterate through a list of products but am unable to get the "src" to the image.

    CODE

     @foreach (var product in CurrentPage.ProductCategories.First().Children().Take(3))
     { <p>@product.productImage</p>  }
    

    OUTPUT

    { "focalPoint": { "left": 0.5, "top": 0.5 }, "src": "/media/1069/38ba0a0575_med.jpg", "crops": [ { "width": 570, "height": 715, "alias": "ProductCrop", "coordinates": { "x1": 0.15135395189003445, "y1": 0.23813227866822981, "x2": 0.14349140893470752, "y2": 0.056870843695563555 } } ] }

    As i understand this is beacuse it is in dynamic content and i need it to be typed.

    However when I'm not iterating through the CurrentPage I get the desired result.

    CODE

    @{
          var image = CurrentPage.sidebarImage;
          <p>image.src</p>  }
    

    OUTPUT

    /media/1069/38ba0a0575_med.jpg

    What am i missing?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 13, 2014 @ 12:20
    Jeavon Leopold
    0

    Hi Fredrik,

    If you are using Umbraco v7.1.2 you can do this:

    Assuming "product" is a typed object

    @product.GetCropUrl(propertyAlias: "sidebarImage", imageCropMode: ImageCropMode.Max)
    

    Before v7.1.2, you can do this

        var imageCrops = JsonConvert.DeserializeObject<ImageCropDataSet>(product.GetPropertyValue<string>("sidebarImage"));
        myImageUrl = imageCrops.Src;
    

    Jeavon

  • Fredrik Öberg 7 posts 47 karma points
    May 13, 2014 @ 13:29
    Fredrik Öberg
    0

    Hello Jeavon

    I'm using 7.1.0 and using

    var imageCrops = JsonConvert.DeserializeObject<ImageCropDataSet>(product.GetPropertyValue<string>("productImage"));
    var myImageUrl = imageCrops.Src;
    

    gives me exception Error parsing comment. Expected: *, got m. Path '', line 1, position 1.

    It seems to read the json as comments allthough the pure output of the GetPropertyValue looks like this.

        product.GetPropertyValue<string>("productImage");
    
    { "focalPoint": { "left": 0.5, "top": 0.5 }, "src": "/media/1069/38ba0a0575_med.jpg", "crops": [ { "width": 570, "height": 715, "alias": "ProductCrop", "coordinates": { "x1": 0.15135395189003445, "y1": 0.23813227866822981, "x2": 0.14349140893470752, "y2": 0.056870843695563555 } } ] }
    

    So i tried to save the string and use it and it works.

    var jsonString = "{ 'focalPoint': { 'left': 0.5, 'top': 0.5 }, 'src': '/media/1069/38ba0a0575_med.jpg', 'crops': [ { 'width': 570, 'height': 715, 'alias': 'ProductCrop', 'coordinates': { 'x1': 0.15135395189003445, 'y1': 0.23813227866822981, 'x2': 0.14349140893470752, 'y2': 0.056870843695563555 } } ] }";
    var imageCrops = JsonConvert.DeserializeObject<ImageCropDataSet>(jsonString);
    

    But it throws the same exception with

    var imageValue = product.GetPropertyValue<string>("productImage");
    var imageCrops = JsonConvert.DeserializeObject<ImageCropDataSet>(imageValue);
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 13, 2014 @ 13:38
    Jeavon Leopold
    100

    I think this might be slightly different issue as detailed here http://issues.umbraco.org/issue/U4-4756

    Can you check that every image in your loop has the full JSON or does one have only partial data?

  • Fredrik Öberg 7 posts 47 karma points
    May 13, 2014 @ 13:44
    Fredrik Öberg
    0

    You are correct it's beacuse I changed from an Upload to an Image Cropper and you have to "touch" the focalPoint or update the image for it to update to a Image cropper type.

    Thank's for the help :)

    /Fredrik

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 13, 2014 @ 13:46
    Jeavon Leopold
    0

    Great!

Please Sign in or register to post replies

Write your reply to:

Draft