Page 1 of 1

Rotate and Crop image when not centered

Posted: 2017-09-03T18:05:21-07:00
by tubeman
Hi,

i am using imagemagick in a AWS Lambda to crop an image uploaded from my website. on the website i use react-avatar-editor to rotate and crop the image. i can get the crop rectangle information (x, y, width, height) and the rotation. i am also using the Node package gm in the Lambda.

i can successfully crop the image if i do not move the image when rotated. that is i can resize, rotate and crop the image but if i do the same and move the image the crop i get is not correct. i cannot figure out how to move the crop rectangle to obtain the correct crop.

the code that i am using is below. i have tried added the cropX and cropY to the extent command but that did not produce the desired results either.

thanks in advance for any help

Code: Select all

if(body.rotate && body.rotate > 0){ // if the image is rotated do logic for rotation
        gmImage
            .rotate("#ffffff", body.rotate) 
            .crop(crop.cropWidth, crop.cropHeight, crop.cropX, crop.cropY)
            .resize(maxImageWidth, maxImageHeight)
            .compose("Copy")
            .gravity("Center")
            .extent(maxImageWidth, maxImageHeight)
            .toBuffer(function (err, buffer) {
                if (err) returnMessage(400, { "error": "Error cropping image" });
                      saveImage(buffer);
            })
}
else {
            gmImage
                .crop(crop.cropWidth, crop.cropHeight, crop.cropX, crop.cropY)
                .resize(maxImageWidth, maxImageHeight)
                .toBuffer(function (err, buffer) {
                     if (err) returnMessage(400, { "error": "Error cropping image" });
                        saveImage(buffer);
            })
}

Re: Rotate and Crop image when not centered

Posted: 2017-09-03T18:08:52-07:00
by magick
GM is the GraphicsMagick package, we can't help you with that. Install the ImageMagick node package and if you have problems, let us know. We can help you with that.

Re: Rotate and Crop image when not centered

Posted: 2017-09-03T21:07:54-07:00
by tubeman
let me ask this question a different way:

if i have the crop rectangle and rotation how can i do the correct crop if i have the image rotated around a point that is not the center point. i would like to do this on the command line.

the crop rectangle information that i receive back for my editor is
x: left edge
y: top edge
width: width of the crop
height: height of the crop

also can you please let me know which node package you recommend.

https://github.com/rsms/node-imagemagick
this package is abandoned and they suggest not to use

https://github.com/yourdeveloper/node-imagemagick
and this package states that it is not maintained and not to use it and suggest to use

https://github.com/aheckmann/gm
which you say you cannot help with.

gm does support imagemagick and has a really nice fluent api.

I really don't care which package i use i am trying to solve how to obtain the correct crop of an image that is rotated around a point that is not the center of the image.

thanks

Re: Rotate and Crop image when not centered

Posted: 2017-09-03T22:43:19-07:00
by snibgo
When transforming images, the order is critical. Also, "rotated around a point that is not the center point" can mean many things. "-distort SRT" with a viewport may do what you want.

If you post an image and say what you want done to it, we can show you how to do that with an ImageMagick command line.

Re: Rotate and Crop image when not centered

Posted: 2017-09-04T00:06:05-07:00
by Bonzo
For your information gm, Imagick etc. are all written/maintained by other people not the Imagemagick developers. I think they are basically classes. They decide what options etc. to include so unless you have an interest in it you do not know what options are supported.

If you want to use a certain API I would have a look around and see if they have a forum.