Rotate and Crop image when not centered

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
tubeman
Posts: 2
Joined: 2017-09-03T17:30:27-07:00
Authentication code: 1151

Rotate and Crop image when not centered

Post 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);
            })
}
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Rotate and Crop image when not centered

Post 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.
tubeman
Posts: 2
Joined: 2017-09-03T17:30:27-07:00
Authentication code: 1151

Re: Rotate and Crop image when not centered

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotate and Crop image when not centered

Post 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.
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Rotate and Crop image when not centered

Post 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.
Post Reply