Copied to clipboard

Flag this post as spam?

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


  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    May 12, 2011 @ 16:51
    Sebastiaan Janssen
    0

    Getting crops on media items using Model.MediaById

    I have a media item with an imagecropper on it, trying to get to the crops in multiple different ways, but none are giving me results:

    @Model.MediaById(Model.ImagePicker).crop

    Gives: <crops date="12/05/2011 14:04:23"><crop name="NewsItem" x="0" y="0" x2="958" y2="717" url="/media/2154/brouwerwebsite_NewsItem.jpg" /></crops>

    That seems good, as it can access the property! But...

    @Model.MediaById(Model.ImagePicker).crop.crops

    Gives: 'string' does not contain a definition for 'crops'  (same for: .crop.crop without the "s")

    @Model.MediaById(Model.ImagePicker).XPath("//crops")

    Gives: 'umbraco.MacroEngines.DynamicMedia' does not contain a definition for 'XPath'

    And last I tried:

    @using umbraco.MacroEngines
    @{ var mediaItem = (DynamicXml) Model.MediaById(Media.ImagePicker).crop; }

    But that gives me: Cannot convert type 'string' to 'umbraco.MacroEngines.DynamicXml'

    I'm out of ideas, help?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 12, 2011 @ 16:57
    Jeroen Breuer
    0

    If your using DAMP there is a GetImageCropperUrl helper method.

    Here is the code. This should also work for MediaById.

            /// <summary>
    /// Returns the url for a given crop name using the built in Image Cropper datatype.
    /// It accepts a razor dynamic xml property.
    /// </summary>
    /// <param name="mediaItem">The mediaItem XML property used in razor. Example: @node.slider.mediaItem. It also accepts the child element of mediaItem like "Image".</param>
    /// <param name="cropName">Name of crop to get url for.</param>
    /// <returns>Emtpy string or url.</returns>
    public static string GetImageCropperUrl(umbraco.MacroEngines.DynamicXml mediaItem, string cropName)
    {
    string cropUrl = string.Empty;

    /*
    * Example xml :
    *
    * <mediaItem>
    * <Image id="1307" version="9eac3ee8-e18f-410d-988a-f257d53ab7e6" parentID="1274" level="4" writerID="0" nodeType="1276" template="0" sortOrder="4" createDate="2011-04-28T14:50:31" updateDate="2011-04-28T14:50:38" nodeName="slide4Image" urlName="slide4image" writerName="admin" nodeTypeAlias="ImageHomepageSlider" path="-1,1123,1124,1274,1307">
    * <umbracoFile>/media/5031/slide4image.jpg</umbracoFile>
    * <umbracoWidth>960</umbracoWidth>
    * <umbracoHeight>446</umbracoHeight>
    * <umbracoBytes>128397</umbracoBytes>
    * <umbracoExtension>jpg</umbracoExtension>
    * <resized><![CDATA[/media/5031/slide4image_resized.jpg]]></resized>
    * <slider>
    * <crops date="28/04/2011 14:50:32">
    * <crop name="slider" x="0" y="0" x2="960" y2="446" url="/media/5031/slide4image_resized_slider.jpg" />
    * <crop name="sliderSmall" x="0" y="0" x2="200" y2="100" url="/media/5032/slide4image_resized_sliderSmall.jpg" />
    * </crops>
    * </slider>
    * </Image>
    * </mediaItem>
    *
    */


    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(mediaItem.BaseElement.ToString());
    XmlNode cropNode = xmlDocument.SelectSingleNode("descendant::crops/crop[@name='" + cropName + "']");

    if (cropNode != null)
    {
    cropUrl = cropNode.Attributes.GetNamedItem("url").InnerText;
    }

    return cropUrl;
    }

    Jeroen

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    May 12, 2011 @ 17:09
    Sebastiaan Janssen
    0

    Not using damp though. But interesting approach, I can probably modify it to make it work!

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    May 12, 2011 @ 17:13
    Sebastiaan Janssen
    0

    Well that was quicker than I thought, I use this now:

    @UmbracoHelper.GetImageCropperUrl(Model.MediaById(Model.Image).crop, "NewsItem")

    And your function turned has been put in my App_Code folder (filename: UmbracoHelper.cshtml, hence the namespace above)

    @using System.Xml
    @functions {    
        public static string GetImageCropperUrl(string cropText, string cropName)
        {
            string cropUrl = string.Empty;
    
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(cropText);
            XmlNode cropNode = xmlDocument.SelectSingleNode("descendant::crops/crop[@name='" + cropName + "']");
    
            if (cropNode != null)
            {
                cropUrl = cropNode.Attributes.GetNamedItem("url").InnerText;
            }
    
            return cropUrl;
        }
    }
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 12, 2011 @ 17:18
    Jeroen Breuer
    1

    Credits should go to uCompontens since that's what my GetImageCropperUrl method is based on: http://ucomponents.codeplex.com/SourceControl/changeset/view/77624#1458752

    Only difference is my method is optimized for DAMP and Razor. You can probably use that method now that I think about it...

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft