From eb587fb2331f7ee319c3b42da3484f2baccf06c2 Mon Sep 17 00:00:00 2001 From: Joel Alphonso Date: Mon, 26 Feb 2018 15:34:59 -0500 Subject: [PATCH] calculate crop gravity --- .../Imagick/Effect/ImagickCropEffect.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/Charcoal/Image/Imagick/Effect/ImagickCropEffect.php b/src/Charcoal/Image/Imagick/Effect/ImagickCropEffect.php index d50ff94..4ef6207 100644 --- a/src/Charcoal/Image/Imagick/Effect/ImagickCropEffect.php +++ b/src/Charcoal/Image/Imagick/Effect/ImagickCropEffect.php @@ -22,8 +22,47 @@ protected function doCrop($width, $height, $x, $y) { $gravity = $this->image()->imagickGravity($this->gravity()); + // This sets the gravity for the rest of the chain $this->image()->imagick()->setGravity($gravity); + // Apply gravity to crop coordinates + + $imageWidth = $this->image()->width(); + $imageHeight = $this->image()->height(); + + switch ($this->image()->imagick()->getGravity()) { + case Imagick::GRAVITY_NORTHWEST: + break; + case Imagick::GRAVITY_NORTH: + $x = ($imageWidth / 2 - $width / 2); + break; + case Imagick::GRAVITY_NORTHEAST: + $x = ($imageWidth - $width); + break; + case Imagick::GRAVITY_WEST: + $y = ($imageHeight / 2 - $height / 2); + break; + case Imagick::GRAVITY_CENTER: + $x = ($imageWidth / 2 - $width / 2); + $y = ($imageHeight / 2 - $height / 2); + break; + case Imagick::GRAVITY_EAST: + $x = ($imageWidth - $width); + $y = ($imageHeight / 2 - $height / 2); + break; + case Imagick::GRAVITY_SOUTHWEST: + $y = ($imageHeight - $height); + break; + case Imagick::GRAVITY_SOUTH: + $x = ($imageWidth / 2 - $width / 2); + $y = ($imageHeight - $height); + break; + case Imagick::GRAVITY_SOUTHEAST: + $x = ($imageWidth - $width); + $y = ($imageHeight - $height); + break; + } + $this->image()->imagick()->cropImage($width, $height, $x, $y); } }