Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Merge branch 'CheeseSucker-patch-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Mar 27, 2020
2 parents a20dcad + c299a10 commit 1a08b2e
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/stojg/crop/CropBalanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ protected function getRandomEdgeOffset(\Imagick $original, $targetWidth, $target
// Turn image into a grayscale
$measureImage->modulateImage(100, 0, 100);
// Turn everything darker than this to pitch black
$measureImage->blackThresholdImage("#101010");
$measureImage->blackThresholdImage("#070707");
// Get the calculated offset for cropping
return $this->getOffsetBalanced($targetWidth, $targetHeight);
return $this->getOffsetBalancedForImage($measureImage, $targetWidth, $targetHeight);
}

/**
Expand All @@ -58,33 +58,45 @@ protected function getRandomEdgeOffset(\Imagick $original, $targetWidth, $target
*/
public function getOffsetBalanced($targetWidth, $targetHeight)
{
$size = $this->originalImage->getImageGeometry();
return $this->getOffsetBalancedForImage($this->originalImage, $targetWidth, $targetHeight);
}

/**
* @param \Imagick $image
* @param $targetWidth
* @param $targetHeight
* @return array
* @throws \Exception
*/
private function getOffsetBalancedForImage(\Imagick $image, $targetWidth, $targetHeight)
{
$size = $image->getImageGeometry();

$points = array();

$halfWidth = ceil($size['width']/2);
$halfHeight = ceil($size['height']/2);

// First quadrant
$clone = clone($this->originalImage);
$clone = clone($image);
$clone->cropimage($halfWidth, $halfHeight, 0, 0);
$point = $this->getHighestEnergyPoint($clone);
$points[] = array('x' => $point['x'], 'y' => $point['y'], 'sum' => $point['sum']);

// Second quadrant
$clone = clone($this->originalImage);
$clone = clone($image);
$clone->cropimage($halfWidth, $halfHeight, $halfWidth, 0);
$point = $this->getHighestEnergyPoint($clone);
$points[] = array('x' => $point['x']+$halfWidth, 'y' => $point['y'], 'sum' => $point['sum']);

// Third quadrant
$clone = clone($this->originalImage);
$clone = clone($image);
$clone->cropimage($halfWidth, $halfHeight, 0, $halfHeight);
$point = $this->getHighestEnergyPoint($clone);
$points[] = array('x' => $point['x'], 'y' => $point['y']+$halfHeight, 'sum' => $point['sum']);

// Fourth quadrant
$clone = clone($this->originalImage);
$clone = clone($image);
$clone->cropimage($halfWidth, $halfHeight, $halfWidth, $halfHeight);
$point = $point = $this->getHighestEnergyPoint($clone);
$points[] = array('x' => $point['x']+$halfWidth, 'y' => $point['y']+$halfHeight, 'sum' => $point['sum']);
Expand Down

0 comments on commit 1a08b2e

Please sign in to comment.