public function focus(ImageInterface $image, $x, $y, $width, $height)
{
$imageSize = $image->getSize();
$currentRatio = $imageSize->getWidth() / $imageSize->getHeight();
$targetRatio = $currentRatio;
if ($width !== null && $height !== null) {
$targetRatio = $width / $height;
}
if ($targetRatio < $currentRatio) {
$height = $imageSize->getHeight();
$width = $targetRatio * $height;
$cropY = 0;
switch ($x) {
case static::FOCUS_LEFT:
$cropX = 0;
break;
case static::FOCUS_RIGHT:
$cropX = $imageSize->getWidth() - $width;
break;
default:
$cropX = ($imageSize->getWidth() - $width) / 2;
}
} else {
$width = $imageSize->getWidth();
$height = $width / $targetRatio;
$cropX = 0;
switch ($y) {
case static::FOCUS_TOP:
$cropY = 0;
break;
case static::FOCUS_BOTTOM:
$cropY = $imageSize->getHeight() - $height;
break;
default:
$cropY = ($imageSize->getHeight() - $height) / 2;
}
}
return $image->crop(new Point($cropX, $cropY), new Box($width, $height));
}