Imagick not reflects the gravity once it is set

2019-08-26 04:14发布

问题:

I am trying to translate the following piece of Imagemagick command line code

 convert tmpI.mpc -gravity center -crop ${ww}x${hc}+0+0 +repage

as follows

$tmpIC->setGravity(imagick::GRAVITY_CENTER);
$tmpIC->cropImage($ww, $hc, 0, 0);
$tmpIC->setImagePage($tmpIC->getImageWidth(), $tmpIC->getImageHeight(), 0, 0);

but setting or not setting the gravity makes no difference. Please tell me what is wrong with the piece of code I have

Width and Height of the image before cropping are

tmpIC->getImageWidth() = 479
tmpIC->getImageHeight() = 599

And the values of ww and hc are

ww=479
hc=479

and the width and height of the image after cropping are as follows

tmpIC->getImageWidth() = 479
tmpIC->getImageHeight() = 479

回答1:

Not all of the ImageMagick commands can be converted directly to the same named commands in Imagick.

In your case, the gravity command doesn't map very well - but the same effect of cropping the image to the centre can be done by yourself in a single cropImage function call:

$tmpIC->cropImage(
    $ww,
    $hc,
    $startX = ($tmpIC->getImageWidth() - $ww) / 2,
    $startY = ($tmpIC->getImageHeight() - $hc) / 2
);