Copied to clipboard

Flag this post as spam?

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


  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Aug 08, 2018 @ 03:14
    Nicholas Westby
    0

    ImageProcessor: Crop an Image From Center Without Resizing

    I have an original image that is, say, 600x600. I want to crop just the center of the image with a size of 400x400. So, there will be 100 pixels of image on each of the four sides that will be discarded during the crop operation. The source image is hosted remotely, so it's not easy to reliably figure out what the dimensions are.

    I can't seem to figure out how to do that with ImageProcessor. I've looked at these pages:

    The "Crop" method sounds like what I want, except it requires me to know the dimensions of the image if I want to crop from the center. I could crop from the top left of the image, but that's not ideal.

    The "Resize" method seems like it might work if I use the "Resize Crop" mode, except that stretches the image to fit the specified dimensions rather than simply cropping it.

    Has anybody figured out a simple way to use ImageProcessor to crop the center of an image using specific dimensions and without resizing the image?

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Aug 08, 2018 @ 03:33
    Nicholas Westby
    0

    I just found a way to get the width/height of the original image (using a third-party API), so I don't really need an answer to this anymore (I can just use crop).

    However, if anybody has an answer, I'd still be curious, and I'm sure it would help others, so please do share.

  • Louis Ferreira 69 posts 265 karma points
    Aug 08, 2018 @ 07:38
    Louis Ferreira
    0

    Hi Nicholas,

    It is quite easy really, all you need to do is call GetCropUrl with params...

    @if (Model.Content.HasValue("image"))
    {
        <img src="@Url.GetCropUrl(Model.Content, propertyAlias: "image", height: 400, width: 400)" />
    }
    

    For more details, see this page https://our.umbraco.com/documentation/getting-started/backoffice/property-editors/built-in-property-editors/image-cropper

    Hope that helps

    Louis

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Aug 08, 2018 @ 14:55
    Nicholas Westby
    0

    I suspect that would actually resize the image, but I haven't tested it so I'm not sure.

    Also, I mentioned that this image is hosted externally, and the example you showed seems to be on a locally hosted image.

Please Sign in or register to post replies

Write your reply to:

Draft