Copied to clipboard

Flag this post as spam?

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


  • Simon Dingley 1388 posts 3239 karma points c-trib
    Nov 27, 2017 @ 13:40
    Simon Dingley
    0

    GetCropUrl with only dimensions is returning a named crop with the given dimensions

    I have a strange issue on a site whereby the Image media type Upload property editor has been replaced with the Image Cropper ( Umbraco.ImageCropper) property editor. The datatype is configured with a single named Crop to allow editors to manually select a crop area for images in a certain position on the site.

    The problem however is that for images that have the crop set, where they are used elsewhere on the site where they shouldn't be cropped they are still returning the named crop instead of the original image (if that makes sense?).

    So, when I am calling @img.GetCropUrl(294, 224) I am actually getting a url like this:

    /media/287242/north-india-ganges-varanasi.jpg?crop=0,0.51895043731778423,0,0.083947549442418&cropmode=percentage&width=294&height=224&rnd=131562628480000000
    

    Whereas I should actually be getting:

    /media/287242/north-india-ganges-varanasi.jpg?width=294&height=224&rnd=131562628480000000
    

    Any ideas why this might be and how to work around it without having to create more unwanted crops for what is a huge media library?

    This is for a site running v7.6.12.

    Thanks, Simon

  • Alex Skrypnyk 5273 posts 19277 karma points MVP 3x admin c-trib
    Nov 27, 2017 @ 13:42
    Alex Skrypnyk
    0

    Hi Simon

    Try to use this code:

    @Url.GetCropUrl(img, htmlEncode: false, width: 294, height: 224)
    
  • Simon Dingley 1388 posts 3239 karma points c-trib
    Nov 27, 2017 @ 13:56
    Simon Dingley
    0

    Hi Alex. I get the same result, the named crop is returned but at the dimensions supplied.

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Nov 27, 2017 @ 14:03
    Simon Dingley
    0

    In the directory where the image is there is the following crop.json file content:

    {
        "Default": {
            "TopLeft": {
                "IsEmpty": true,
                "X": 0,
                "Y": 0
            },
            "BottomRight": {
                "IsEmpty": false,
                "X": 1,
                "Y": 1
            },
            "Gravity": {
                "IsEmpty": false,
                "X": 0.4296077,
                "Y": 0.671009958
            },
            "Bias": {
                "IsEmpty": false,
                "X": 0.4296077,
                "Y": 0.671009958
            },
            "Calculated": false,
            "Relative": true
        },
        "ManualCroppings": {}
    }
    

    ...This is not what I would expect to find as none of the values seem to tally up with the crop information in the url.

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Nov 27, 2017 @ 18:39
    Simon Dingley
    0

    OK, so I can answer my own question. having dug through the source code I found this;

    internal static ImageCropData GetCrop(this IEnumerable<ImageCropData> dataset, string cropAlias)
    {
        var imageCropDatas = dataset.ToArray();
        if (dataset == null || imageCropDatas.Any() == false)
            return null;
    
        if (string.IsNullOrEmpty(cropAlias))
            return imageCropDatas.FirstOrDefault();
    
        return imageCropDatas.FirstOrDefault(x => x.Alias.ToLowerInvariant() == cropAlias.ToLowerInvariant());
    }
    

    And so as I suspected if there are any crops on the media item but you don't tell it which one you want - it returns the first. I'm not convinced this should be the correct behaviour and would welcome other opinions.

    In my mind if I don't give a crop name I want to generate a dynamic crop e.g. one based on the dimensions I provided, not resize and give me the first named crop in the collection.

    Thoughts?

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Nov 29, 2017 @ 07:52
    Simon Dingley
    100

    The solution was to write my on UrlHelper Extension method to return my custom crops.

    public static IHtmlString GetImageCropUrl(
        this UrlHelper urlHelper,
        IPublishedContent mediaItem,
        int? width = null,
        int? height = null,
        int? quality = null,
        ImageCropMode? imageCropMode = null,
        bool htmlEncode = true)
    {
        string imageUrl = mediaItem.Url;
        var imageProcessorUrl = new StringBuilder();
    
        imageProcessorUrl.Append(imageUrl);
    
        if (imageCropMode == null)
        {
            imageCropMode = ImageCropMode.Crop;
        }
    
        imageProcessorUrl.Append("?mode=" + imageCropMode.ToString().ToLower());
    
        if (quality != null)
        {
            imageProcessorUrl.Append("&quality=" + quality);
        }
    
        if (width != null)
        {
            imageProcessorUrl.Append("&width=" + width);
        }
    
        if (height != null)
        {
            imageProcessorUrl.Append("&height=" + height);
        }
    
        var url = imageProcessorUrl.ToString();
    
        return htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(url)) : new HtmlString(url);
    }
    

    I will probably look to extend this further to make use of focal points but this should suffice for now.

  • Chriztian Steinmeier 2697 posts 8105 karma points MVP 3x admin c-trib
    Jan 21, 2019 @ 14:02
    Chriztian Steinmeier
    0

    Hi Simon,

    Was there ever filed an issue for this?

    I'm getting the same behavior in 7.12.x

    /Chriztian

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Jan 21, 2019 @ 15:30
    Simon Dingley
    0

    Hi Chriztian,

    I'm afraid I honestly can't remember but I suspect not, sorry :(

    Cheers, Simon

Please Sign in or register to post replies

Write your reply to:

Draft