Copied to clipboard

Flag this post as spam?

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


  • Meni 273 posts 530 karma points
    Oct 05, 2016 @ 22:07
    Meni
    0

    Error while try to get image size

    Hi, I try to do something simple - but just keep get errors... Here's what I try to do: I need to display image based on its dimensions (I get the media from the media picker), here's the code:

              @{
                if(CurrentPage.HasValue("PageImg")){    
                    var w = @Umbraco.Media(CurrentPage.PageImg).umbracoWidth;   
                    var h = @Umbraco.Media(CurrentPage.PageImg).umbracoHeight;
                    if (w >> 700)
                    {
                        <div class="col-xl-12">
                            <img src="@Umbraco.Media(CurrentPage.PageImg).Url" />
                        </div>  
                    }
        else if (w <= 700) {
        do something else
        }
      }     
      }
    

    Try to figure out what I do wrong? I tried if (@w >>) / if (w >>) - both give error. Maybe the Umbraco helper returns string and I need to convert to int? or anything else?

    Thanks for your help :)

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Oct 06, 2016 @ 07:55
    Steve Morgan
    0

    Hi Meni,

    You were right - for some reason the umbracoWidth is returned as a string. You could tryParse that to an int but it's getting messy - I prefer to use the Strongly typed method of doing this sort of thing, code below!

    Couple of points on your razor code which will hopefully help in the future. The @ is only for "outputting" - for assigning variable values don't use it (e.g. your var w = @Umbraco....)

    Also, when writing complex bits of razor straight into the markup (like your img src) ensure you wrap it in a set of brackets so that the Razor engine knows what is what.. e.g.

    <img src="@Umbraco.Media(CurrentPage.PageImg).Url" />
    //Should be:
    <img src="@(Umbraco.Media(CurrentPage.PageImg).Url)" />
    

    I'd do it using the Strongly Typed model like this:

            @{
                if (Model.Content.HasValue("PageImg"))
                {
                    var typedImage = Umbraco.TypedMedia(Model.Content.GetPropertyValue<int>("PageImg"));
                    if (typedImage != null)
                    {
                        var w = typedImage.GetPropertyValue<int>("umbracoWidth");
                        var h = typedImage.GetPropertyValue<int>("umbracoHeight");
                        if (w > 700)
                        {
                        <div class="col-xl-12">
                            <img src="@(typedImage.Url)" />
                        </div>
                        }
                        else
                        {
                            <p>No image</p>  
                        }
                    }
                }
            }
    

    HTH

    Steve

  • Meni 273 posts 530 karma points
    Oct 10, 2016 @ 23:37
    Meni
    0

    Thanks Steve :) Works now!

    Just quick question: if my whole site written in dynamic, is it fine to write one script in Strongly Typed?

    1. What's more recommend in general in terms of speed? dynamic or Strongly Typed? I was reading somewhere that Strongly Typed is only good if using VWD, so you can use the IntelliSense?

    Thanks again for helping!

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Oct 11, 2016 @ 07:04
    Steve Morgan
    0

    Hi Meni,

    That's a very interesting question. I have to say I don't actually know the answer though. I only use the strongly typed methods as I prefer it.

    I would imagine under the hood it does the same / similar amount of work but I don't know! I certainly can't see a problem in mixing it except it makes your code harder to follow and support for others. For me, the strongly typed code is far more predictable as the example above shows. You get to typeset what you're getting back which for me is a big bonus.

    HTH

    Steve

Please Sign in or register to post replies

Write your reply to:

Draft