Copied to clipboard

Flag this post as spam?

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


  • Jonas Widenberg 17 posts 98 karma points
    Feb 27, 2019 @ 10:31
    Jonas Widenberg
    1

    GetCropUrl returns empty

    Hi, I'm trying out v. 8 for a bit this morning. I'm having trouble rendering cropped image urls though. I did this the other day in v.7 without issues. Has anything changed or am I missing something?

    testimonial.Image is a MediaPicker and I've defined a crop called "Testimonial".

    @testimonial.Image.Url renders an URL fine.

    @testimonial.Image.GetCropUrl("Testimonial") renders nothing.

    @Url.GetCropUrl(testimonial.Image, "Testimonial") renders nothing.

    Thanks!

  • Jonas Widenberg 17 posts 98 karma points
    Feb 27, 2019 @ 10:47
    Jonas Widenberg
    0

    If I go and edit the crop. The image renders afterwards. It seems you dont get a default crop?

    I don't see this behaviour in v. 7.

  • Murat 3 posts 73 karma points
    May 16, 2019 @ 14:51
    Murat
    0

    I'm using umbraco 8. How is the idea of how to solve this problem?

  • Josip 190 posts 656 karma points c-trib
    May 16, 2019 @ 15:35
    Josip
    2

    Hi,

    Probably there are better ways to do this but this is how i did it:

     @if (Image != null)
                        {
                            <img class="img-fluid d-block" src="@Image.GetCropUrl(250,150)" alt="@Image.Name" />
                        }
    
  • Murat 3 posts 73 karma points
    May 16, 2019 @ 15:47
    Murat
    0

    thanks for the answer, it solved

  • Ginu Jose 5 posts 86 karma points
    May 26, 2019 @ 10:06
    Ginu Jose
    1

    I don't think this solves the question since the extension with crop alias is not working in Umbraco 8.

  • Proxicode 123 posts 309 karma points
    Dec 15, 2019 @ 21:20
    Proxicode
    1

    Thank you @Josip! That was exactly the right solution, and after seeing it, it made my error make sense.

    var featuredImage = model.Value<IPublishedContent>("featuredImage").GetCropUrl(276,276)
    

    fails because I was trying to get the image and the URL at the same time. Your example makes total sense. Get the image, then ask for it's cropUrl. :-)

    var featuredImage = model.Value<IPublishedContent>("featuredImage");
    <img src="@featuredImage.GetCropUrl(276,276)" class="initial-img" />
    

    I appreciate you! Thanks

  • Arun 126 posts 327 karma points
    Dec 26, 2019 @ 06:42
    Arun
    0

    It works but i don't think this is an optimal way. Suppose we have used a particular resolution in various pages and whenever we need to change the resolution we have to check and change everywhere that is used manually.! Where in Umbraco 7 we only need to change the resolution in the cropper simply

  • Evan Moore 4 posts 75 karma points c-trib
    Jun 27, 2019 @ 14:10
    Evan Moore
    0

    It looks like a bug in V8. I've added a github issue for this. https://github.com/umbraco/Umbraco-CMS/issues/5737

  • Vijay Bhatter 5 posts 86 karma points
    Aug 03, 2019 @ 06:52
    Vijay Bhatter
    1

    I was also facing the same problem. However when I reset the focus point in each of the images it started showing.

    This code works:

    @testimonial.Image.GetCropUrl("Testimonial")
    
  • Christian Reinholdt 2 posts 73 karma points
    Feb 05, 2020 @ 15:27
    Christian Reinholdt
    1

    I can confirm that GetCropUrl with alias only works after changing the focal point for the images.

    Very strange behavior :)

  • emaadali 56 posts 96 karma points
    May 22, 2020 @ 20:11
    emaadali
    0

    I agree with Christian. It works after focal point change.

  • David Armitage 490 posts 2049 karma points
    Jun 14, 2020 @ 09:39
    David Armitage
    0

    Hi Guys,

    Yeah I agree. I have tested this loads and like a few mentioned above the crop doesn't work unless you go into the media section click on the image and edit the crop on the image.

    It didn't work like this with Umbraco 7. It seems to be a bug that has been introduced into Umbraco 8.

    I think I logged a bug for this a while ago.

    It's a really frustrating but. I have become reliant on the cropper with all my projects. It worked amazingly in Umbraco 7. It's sort of become unusable at the member but I might trying and follow the recommendation above and manually set this.

    I was also thinking I could write an extension method to heck of there is no crop set then use a fallback setting if not. That way we might be able to get best of both worlds making this hack more useful.

    I will see how it goes.

    Regards

    David

  • Heather Floyd 582 posts 892 karma points MVP 4x c-trib
    Aug 05, 2021 @ 22:17
    Heather Floyd
    1

    I came across this today in a site I'm migrating from 7 to 8.14.

    Here is the extension method I wrote to handle it:

    using Umbraco.Core.Models;
    using Umbraco.Core.PropertyEditors.ValueConverters;
    
    public static class ExtensionMethods
    {
        /// <summary>
        /// Returns the url for a named crop, if present, otherwise just returns the image src url
        /// </summary>
        /// <param name="CropperValue">ImageCropperValue</param>
        /// <param name="CropAlias">Alias of the desired crop</param>
        /// <param name="ImageUrlGenerator">If possible, use 'Current.ImageUrlGenerator' (null works, but is obsoleted in Umbraco 8)</param>
        /// <returns>Ready-to-Use Image Url</returns>
        public static string GetCropOrSrcUrl(this ImageCropperValue CropperValue, string CropAlias,
            IImageUrlGenerator ImageUrlGenerator = null)
        {
            var src = CropperValue.Src;
            string cropInfo;
    
            if (ImageUrlGenerator != null)
            {
                cropInfo = CropperValue.GetCropUrl(CropAlias, ImageUrlGenerator);
            }
            else
            {
                cropInfo = CropperValue.GetCropUrl(CropAlias);
            }
    
            if (string.IsNullOrEmpty(cropInfo))
            {
                //No crop info found, return original src
                return src;
            }
            else if (cropInfo.StartsWith("?"))
            {
                //src url is missing (Umbraco 8 bug)
                return src + cropInfo;
            }
            else
            {
                return cropInfo;
            }
        }
    }
    

    Use it in a view like this:

    var imgUrl = Model.MyImageProperty.GetCropOrSrcUrl("MyCrop", Current.ImageUrlGenerator);
    
  • wheel7 1 post 21 karma points
    Oct 31, 2021 @ 10:09
    wheel7
    0

    I ended up here, because I had the same issue as the OP. Since none of the answers seemed to solve my issue, I will post my solution to the problem.

    This doesn't work:

    var imageUrl = item.Value<IPublishedContent>("ImageName").GetCropUrl("ImageCropName");
    

    Nor does this (for obvious reasons):

    var image = item.Value<IPublishedContent>("ImageName");
    var imageUrl = image.GetCropUrl("ImageCropName");
    

    With explicit cast it does work:

    var image = opleiding.Value<IPublishedContent>("ImageName") as MediaWithCrops;
    var imageUrl = image.GetCropUrl("ImageCropName");
    

    Notice the "as MediaWithCrops" at the end of the first line. So I wrote my own extension method:

    public static string CroppedImageUrl(this IPublishedContent model, string imagePropertyName, string imageCropName)
    {
        var image = model.Value<IPublishedContent>(imagePropertyName) as MediaWithCrops;
        return image?.GetCropUrl(imageCropName);
    }
    

    Or like this, if you will:

    public static string CroppedImageUrl(this IPublishedContent model, string imagePropertyName, string imageCropName) =>
            (model.Value<IPublishedContent>(imagePropertyName) as MediaWithCrops)?.GetCropUrl(imageCropName);
    
Please Sign in or register to post replies

Write your reply to:

Draft