Skip to content

Commit

Permalink
calculate crop gravity
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelAlphonso committed Feb 26, 2018
1 parent dce39d8 commit eb587fb
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Charcoal/Image/Imagick/Effect/ImagickCropEffect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit eb587fb

Please sign in to comment.