PHP ImageMagick 通过提供 top/right/bottom/left 百分比进行裁剪

PHP ImageMagick Crop By Providing top/right/bottom/left Percentages

我知道如何使用 ImageMagick 的 cropImage() 函数裁剪图像:

$imagick->cropImage($width, $height, $startX, $startY);

如何通过使用 ImageMagick 提供顶部、右侧、左侧和底部的百分比来使用 PHP 裁剪图像?

我用下面的代码解决了这个问题:

$imagick = new Imagick;
$imagick->readImageBlob( $image );

$image_width    = $imagick->getImageWidth();
$image_height   = $imagick->getImageHeight();

$x = $image_width * ($left/100);
$y = $image_height * ($top/100);

$new_width  = $image_width - ($image_width * ($right/100)) - $x;
$new_height = $image_height - ($image_height * ($bottom/100)) - $y;

$imagick->cropImage($new_width, $new_height, $x, $y);