Rotate and Crop image when not centered
Posted: 2017-09-03T18:05:21-07:00
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
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);
})
}