Copied to clipboard

Flag this post as spam?

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


  • Jeremy Wiersma 37 posts 134 karma points
    Aug 17, 2014 @ 15:27
    Jeremy Wiersma
    0

    Getting a Responsive Image Cropper to work

    Hi folks, esp. Jeavon, need some help with the image cropper, esp. the responsive version if possible, but just getting a crop to work would be nice.

    So originally I could get my images to display but they were not cropped. My code was;

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
    Layout = "tmpContentPage.cshtml";

    var homePage = CurrentPage.AncestorsOrSelf(1).First();
    var ourMachines = homePage.dtOurMachines.First();
    var machineItems = ourMachines.dtOurMachineDetails.Take(5);
    }

    @{
    foreach (var machine in machineItems)
    {
    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <div class="accent-panel">
    <div class="col-lg-12">
    <h4><a href="@machine.Url">@machine.propMachineName</a></h4>
    </div>
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 machine-photo">
    @{
    if (machine.HasValue("propMachinePhoto"))
    {
    <img src="@machine.propMachinePhoto.GetCropUrl(height:250, width:150)" alt="@machine.propMachineName" />
    }
    }
    </div>
    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 machine-content">
    @Umbraco.Truncate(machine.propMachineContent, 500)
    </div>
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 machine-more">
    <a href="@machine.Url" class="btn link-details">view details</a>
    </div>
    </div>
    </div>
    }
    }

    I also tried the variation of: "GetCropUrl(propertyAlias: "propMachinePhoto", height:150, width:250)" but no luck, the image would appear but it wasn't cropped. This led me to this topic.

    So I proceeded to update the ImageProcessor - which it looked likes it needed - but it broke all my images as noted in this topic.

    I managed to get the image back afterwards with this...

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
    Layout = "tmpContentPage.cshtml";

    var homePage = CurrentPage.AncestorsOrSelf(1).First();
    var ourMachines = homePage.dtOurMachines.First();
    var machineItems = ourMachines.dtOurMachineDetails.Take(5);
    }

    @{
    foreach (var machine in machineItems)
    {
    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <div class="accent-panel">
    <div class="col-lg-12">
    <h4><a href="@machine.Url">@machine.propMachineName</a></h4>
    </div>
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 machine-photo">
    @{
    if (machine.HasValue("propMachinePhoto"))
    {
    <img src="@machine.propMachinePhoto.src" alt="@machine.propMachineName" />
    }
    }
    </div>
    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 machine-content">
    @Umbraco.Truncate(machine.propMachineContent, 500)
    </div>
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 machine-more">
    <a href="@machine.Url" class="btn link-details">view details</a>
    </div>
    </div>
    </div>
    }
    }

    but now this version;

    <img src="@machine.propMachinePhoto.GetCropUrl(height:250, width:150)" alt="@machine.propMachineName" />

    Ends up with the error:

    'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'GetCropUrl'

    Now I get that, because

    @machine.propMachinePhoto

    now returns a JSON object. What I don't get is why this command before I updated ImageProcessor did not trigger this error, instead it displayed the JSON in the image URL. Obviously not correct but it was different.

    So my question is... how do I get this;

    <img src="@machine.propMachinePhoto.src" alt="@machine.propMachineName" />

    into something like this;

    <img src="@machine.propMachinePhoto.GetResponsiveCropUrl(height:250, width:150)" alt="@machine.propMachineName" />

    Where I don't get an error, I do get an image, and it's cropped?

     

    Lastly... I was unable to use your example Jeavon;

    @foreach (var feature in homePage.umbTextPages.Where("featuredPage"))
    {
        <div class="3u">
            <!-- Feature -->
            <section class="is-feature">
                @if (feature.HasValue("Image"))
                {
                    var featureImage = Umbraco.Media(feature.Image);
                    <a href="@feature.Url" class="image image-full">
                    <img src="@featureImage.GetResponsiveImageUrl(270, 161)" alt="" />
                    </a>
                }
                <h3><a href="@feature.Url">@feature.Name</a></h3>
                @Umbraco.Truncate(feature.BodyText, 100)
            </section>
            <!-- /Feature -->
        </div>
    }

    Without changing it to get...

    var homePage = CurrentPage.AncestorsOrSelf(1).First();
    var ourMachines = homePage.dtOurMachines.First();
    var machineItems = ourMachines.dtOurMachineDetails.Take(5);

    @foreach (var machine in machineItems)
    {
    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <div class="accent-panel">
    <div class="col-lg-12">
    <h4><a href="@machine.Url">@machine.propMachineName</a></h4>
    </div>
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 machine-photo">
    @if (machine.HasValue("propMachinePhoto"))
    {
    var machinePhoto = Umbraco.Media(machine.propMachinePhoto);
    <a href="@machine.Url" class="image image-full">
    <img src="@machinePhoto.GetResponsiveImageUrl(250, 150)" alt="" />
    </a>
    }
    </div>
    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 machine-content">
    @Umbraco.Truncate(machine.propMachineContent, 500)
    </div>
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 machine-more">
    <a href="@machine.Url" class="btn link-details">view details</a>
    </div>
    </div>
    </div>
    }

    ... using an example from the same Starter Kit... where I used the var declarations at the top to get this part of your example to kind of work;

    var feature in homePage.umbTextPages.Where("featuredPage")

    My questions;

    - How did you get the objects "homePage" and "umbTextPages" in your example?

    - How can you write a version of your example in such a way that one can get intellisense in Visual Studio?

     

    Hope the above made sense. I'm really tired at 1:30am. Time to sleep.

    Thanks.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 18, 2014 @ 10:35
    Jeavon Leopold
    0

    Ok, I do talk about using width and height in uHangOut EP30 (around 18 minutes) and the need to strongly type the media item.

    Anyhow from what I have gathered, below is a strongly typed version (for intellisense):

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        Layout = "tmpContentPage.cshtml";
    
        var homePage = Model.Content.AncestorOrSelf(1);
        var ourMachines = homePage.Children.OfTypes("dtOurMachine").First();
        var machineItems = ourMachines.Children.OfTypes("dtOurMachineDetail").Take(5);
    }
    
    @{
        foreach (var machine in machineItems)
        {
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                <div class="accent-panel">
                    <div class="col-lg-12">
                        <h4><a href="@machine.Url">@machine.GetPropertyValue("propMachineName")</a></h4>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 machine-photo">
                        @{
            if (machine.HasValue("propMachinePhoto"))
            {
                var machinePhoto = Umbraco.TypedMedia(machine.GetPropertyValue("propMachinePhoto"));
                <img src="@machinePhoto.GetCropUrl(propertyAlias: "propMachinePhoto", height: 250, width: 150)" alt="@machine.GetPropertyValue("propMachineName")" />
            }
                        }
                    </div>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 machine-content">
                        @Umbraco.Truncate(machine.GetPropertyValue<string>("propMachineContent"), 500)
                    </div>
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 machine-more">
                        <a href="@machine.Url" class="btn link-details">view details</a>
                    </div>
                </div>
            </div>
        }
    }
    

    So for full intellisense, always use Model.Content, TypedContent and TypedMedia

    Jeavon

  • Jeremy Wiersma 37 posts 134 karma points
    Aug 18, 2014 @ 11:56
    Jeremy Wiersma
    0

    Hi Jeavon,

    Thank you for changing the code to quit, this makes a lot of sense, thank you :)

    Unfortunately when I updated my Partial View appropriately to suit, I got this error;

    Exception Details: System.InvalidOperationException: Sequence contains no elements
    Line 15:   // Then take the first one, as we know there will only be on news overview page
    Line 16:    //var ourMachines = homePage.dtOurMachines.First();
    Line 17: var ourMachines = homePage.Children.OfTypes("dtOurMachine").First(); Line 18: 
    Line 19: 

    I am assuming that there is something either;

    a) wrong with my Data Type structure (although the runtime version of this code works fine with the current structure)

    b) this is related to the ImageProcessor issue I'm experiencing.


    BTW: I've checked out the ImageProcessor site and all of the entries in the various configuration files seem to be setup correctly. The references appear to be setup properly in my project. This is weird! :P

     

    Cheers,
    Jeremy

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 18, 2014 @ 11:59
    Jeavon Leopold
    0

    I took a guess that you were using the dynamic pluraliser when I converted this but perhaps not, should the document type alias be "dtOurMachines" or "dtOurMachine"?

  • Jeremy Wiersma 37 posts 134 karma points
    Aug 18, 2014 @ 13:05
    Jeremy Wiersma
    0

    Nah for some reason in the context I am cosing in the Pluraliser is not needed. This is the kind fo rstuff that is confusing me haha

    So the document types are "dtOurMachines" and "dtOurMachineDetails".

    Unfortunately I get a "The value of parameter 'id' must be either a string or an integer" error on "var machinePhoto = Umbraco.TypedMedia(machine.GetPropertyValue("propMachinePhoto"));"

    I'm thinking I can figure this one out though.

     

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

    Totally agree, dynamics is complicated especially stuff like pluralisation.

    A bit strange, TypedMedia should accept a object, perhaps try Umbraco.TypedMedia(machine.GetPropertyValue<int> ("propMachinePhoto"));" instead and see if you get anywhere

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

    Can I just double check is propMachinePhoto a media picker or is it a cropper?

  • Jeremy Wiersma 37 posts 134 karma points
    Aug 18, 2014 @ 13:22
    Jeremy Wiersma
    0

    Hmmm it's a Cropper

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 18, 2014 @ 13:23
    Jeavon Leopold
    100

    Ok, change it to this then:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        Layout = "tmpContentPage.cshtml";
    
        var homePage = Model.Content.AncestorOrSelf(1);
        var ourMachines = homePage.Children.OfTypes("dtOurMachine").First();
        var machineItems = ourMachines.Children.OfTypes("dtOurMachineDetail").Take(5);
    }
    
    @{
        foreach (var machine in machineItems)
        {
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                <div class="accent-panel">
                    <div class="col-lg-12">
                        <h4><a href="@machine.Url">@machine.GetPropertyValue("propMachineName")</a></h4>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 machine-photo">
                        @{
            if (machine.HasValue("propMachinePhoto"))
            {
                <img src="@machine.GetCropUrl(propertyAlias: "propMachinePhoto", height: 250, width: 150)" alt="@machine.GetPropertyValue("propMachineName")" />
            }
                        }
                    </div>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 machine-content">
                        @Umbraco.Truncate(machine.GetPropertyValue<string>("propMachineContent"), 500)
                    </div>
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 machine-more">
                        <a href="@machine.Url" class="btn link-details">view details</a>
                    </div>
                </div>
            </div>
        }
    }
    
  • Jeremy Wiersma 37 posts 134 karma points
    Aug 18, 2014 @ 13:31
    Jeremy Wiersma
    0

    Yeah baby!! That was it... ZOMG :)

    Does that mean that "machine" was a Media item already?, thus creating a machinePhoto was unnecessary.

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

    Yay! It's not a media item, it's a image Cropper directly on your content node to you don't need a media item at all. You can use a cropper on both content and media, just depends on your structure (generally media is used for reuse).

    I know your up against it, but if you can find a hour, watch this video https://www.youtube.com/watch?v=bQsvGmnYaUU I walk through using the cropper on content and then also using it on media.

  • Jeremy Wiersma 37 posts 134 karma points
    Aug 18, 2014 @ 14:35
    Jeremy Wiersma
    0

    Thanks Jeavon, your help has been invaluable.

    Unfortunately I'm in a position where I have a project that demanded a CMS, I have not used MVC before, and know only a little C# (VB guy here). So I am learning 3 things at once, with what I find to be poor or confusing documentation, and a short time frame.

    Not a good situation to be in but in this instance unavoidable.

    I will definitely watch the video ASAP. If I can resolve my other problem I need to get the rest of this site implemented to present to client then I'll come back to it. Unless I break it again of course, then I will HAVE to watch it sooner :P

    Actually talking documentation, what do you find to be the most useful sources of actual reference for Umbraco, like APIs, Classes, Methods, and how things work together etc.?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 18, 2014 @ 16:27
    Jeavon Leopold
    0

    You're very welcome!

    For documentation always check here first

    Probably my most used sections are:

Please Sign in or register to post replies

Write your reply to:

Draft