Wrong with calling Imagick::cropImage() for two times
PHP Version | 7.3.7 imagick module version | 3.4.4 ImageMagick version | ImageMagick 7.0.8 Q16 x86_64
PHP Code: $img = new Imagick('D://11.jpg'); $img->cropImage(424, 467, 72, 0); $img->cropImage(273, 149, 32, 108); echo 'width: ' . $img->getImageWidth() . '; height: ' . $img->getImageHeight();
Result: width: 233; height: 149 It should be: width: 273; height: 149
PHP Code: $img = new Imagick('D://11.jpg'); $img->cropImage(424, 467, 72, 0); $img->setFormat('png'); $img->writeImage('D://tmp.png'); $img->destroy(); $img = new Imagick('D://tmp.png'); $img->cropImage(273, 149, 32, 108); echo 'width: ' . $img->getImageWidth() . '; height: ' . $img->getImageHeight();
The result is wrong still: width: 233; height: 149
So, cropping an image, leaves an image with an 'image page' set. Which is way of offsetting the image that needs to be displayed from the full size of the image.
You can force the image page to be exactly what you'd expect.
$img->setImagePage($img->getimageWidth(), $img->getimageheight(), 0, 0);
tbh, this is quite surprising behaviour, and probably shouldn't be the default, or at least should be documented better.
$img = new Imagick(realpath('./Virgo.jpg'));
$img->cropImage(424, 467, 72, 0);
$img->writeimage("./after_crop1.jpg");
$img->setImagePage($img->getimageWidth(), $img->getimageheight(), 0, 0);
$img->cropImage(273, 149, 32, 108);
$img->writeimage("./after_crop2.jpg");
echo 'width: ' . $img->getImageWidth() . '; height: ' . $img->getImageHeight() . "\n";
Thanks for your response! That means i must setImagePage after cropImage. That's really a quite surprising behavior. But at least i can get the right result. Thank you again!