Copied to clipboard

Flag this post as spam?

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


  • Zakhar 171 posts 397 karma points
    Aug 15, 2012 @ 12:08
    Zakhar
    0

    How to get image crop Url from media using Razor?

    Hi guys,

    I've seen similar questions here but all of them contain outdated answers which don't work.

    So here is my question: I have a media folder which contains images for a gallery. I'm looping through images in this folder to display them. All images have crops but I can't get their url from xml.

    Here is how I do it:

    @using umbraco.cms.businesslogic.media;
    @using uComponents.Core;
    @using uComponents.Core.uQueryExtensions;

    @{
      var folderId = Convert.ToInt32(@Model.GetProperty("galleryFolder").Value);     
    }
    @if (@folderId > 0) {
        Media startMedia = new Media(@folderId);   
        foreach(Media m in startMedia.GetChildMedia()) {
            string imgPath = m.GetImageCropperUrl("crops", "GalleryThumb");
            //imgPath is always empty string here
            if (string.IsNullOrEmpty(imgPath)) {
                imgPath = @m.getProperty("umbracoFile").Value.ToString();
            }
            <a rel="example_group" href="@m.getProperty("umbracoFile").Value.ToString()" title="@m.getProperty("caption").Value.ToString()">
                <img alt="" src="/umbraco/imagegen.ashx?image=@imgPath&width=180&height=120&crop=resize">
            </a>
        }
    }

    example media node contains this xml:

    <crops date=\"2012-08-03T17:59:28\"><crop name=\"GalleryThumb\" x=\"121\" y=\"137\" x2=\"587\" y2=\"448\" url=\"/media/6176/img1_b_GalleryThumb.jpg\" /></crops>

    How can I get the url? It should be easy, but I can't find it.

    Thanks.

  • Hendy Racher 859 posts 3822 karma points MVP admin c-trib
    Aug 15, 2012 @ 12:21
    Hendy Racher
    0

    Hi Zakhar,

    Your script looks like it should work - what does it render if you add the following snippet where your "//imgPath is always empty string here" comment is:

    @(m.GetProperty<string>("crops"))

     

  • Zakhar 171 posts 397 karma points
    Aug 15, 2012 @ 12:43
    Zakhar
    0


    Hi Hendy, thank you for your hint,

    m.GetProperty<string>("crops")

    was empty in my case, so i tried

    string imgPath = m.GetImageCropperUrl("imageCropper", "GalleryThumb");

    And it works, my bad.

    Thanks one more time.

  • Robin Winslow Morris 20 posts 82 karma points
    Jan 22, 2013 @ 16:05
    Robin Winslow Morris
    0

    I couldn't get GetImageCropperUrl to work.

    I suggest you use the solution in this post - it's by far the neatest: http://our.umbraco.org/projects/backoffice-extensions/image-cropper/image-cropper/32728-Get-Crops-with-Razor-syntax

    Or if you only have one crop in that property, use:

    @if(@Model.Media("tipsImage") != null) {
        var image = @Model.Media("tipsImage");
    <img src="@image.imageCrop.crops.crop.url" alt="@image.Name" class="responsive" />
    }
Please Sign in or register to post replies

Write your reply to:

Draft