diff --git a/src/stojg/crop/CropBalanced.php b/src/stojg/crop/CropBalanced.php index 8da8898..892e076 100644 --- a/src/stojg/crop/CropBalanced.php +++ b/src/stojg/crop/CropBalanced.php @@ -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); } /** @@ -58,7 +58,19 @@ 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(); @@ -66,25 +78,25 @@ public function getOffsetBalanced($targetWidth, $targetHeight) $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']);