public function transform(EventInterface $event)
{
$image = $event->getArgument('image');
$params = $event->getArgument('params');
if (empty($params['width']) || empty($params['height'])) {
throw new TransformationException('Both width and height needs to be specified', 400);
}
// Get POI from transformation params
$poi = empty($params['poi']) ? null : explode(',', $params['poi']);
// Check if we have the POI in metadata
if (!$poi) {
$metadataPoi = $this->getPoiFromMetadata($event, $image);
if ($metadataPoi) {
$poi = $metadataPoi;
}
}
$event->getResponse()->headers->set('X-Imbo-POIs-Used', $poi ? 1 : 0);
// Do a simple crop if don't have a POI
if (!$poi) {
return $this->simpleCrop($event, $params['width'], $params['height']);
}
if (!empty($params['crop']) && array_search($params['crop'], ['close', 'medium', 'wide']) === false) {
throw new TransformationException('Invalid crop value. Valid values are: close,medium,wide', 400);
}
$targetWidth = $params['width'];
$targetHeight = $params['height'];
$closeness = isset($params['crop']) ? $params['crop'] : 'medium';
$crop = $this->calculateCrop(['focalX' => $poi[0], 'focalY' => $poi[1], 'sourceWidth' => $image->getWidth(), 'sourceHeight' => $image->getHeight(), 'targetWidth' => $targetWidth, 'targetHeight' => $targetHeight, 'growFactor' => $this->getGrowFactor($closeness), 'sourcePortionThreshold' => $this->getSourcePercentageThreshold($closeness)]);
try {
$this->imagick->cropImage($crop['width'], $crop['height'], $crop['left'], $crop['top']);
$this->imagick->setImagePage(0, 0, 0, 0);
$this->resize($image, $targetWidth, $targetHeight);
} catch (ImagickException $e) {
throw new TransformationException($e->getMessage(), 400, $e);
}
}