public function AutomatedCrop::generateAutomatedCrop in Automated Crop 8
Run the generation of crop.
Parameters
\Drupal\crop\Events\AutomaticCrop $event: The Event to process.
Throws
\Drupal\Core\Entity\EntityStorageException
File
- modules/
automated_crop_crop_provider/ src/ EventSubscriber/ AutomatedCrop.php, line 43
Class
- AutomatedCrop
- A subscriber running automated crop after a crop is needed.
Namespace
Drupal\automated_crop_crop_provider\EventSubscriberCode
public function generateAutomatedCrop(AutomaticCrop $event) {
if (!$this
->applies($event
->getConfiguration()['automatic_crop_provider'])) {
return;
}
/** @var \Drupal\crop\Entity\CropType $crop_type */
$crop_type = $event
->getCropType();
/** @var \Drupal\Core\Image\Image $image */
$image = $event
->getImage();
$hard_limit = $crop_type
->getHardLimit();
$soft_limit = $crop_type
->getSoftLimit();
$configuration = [
'image' => $image,
'min_width' => isset($hard_limit['width']) ? $hard_limit['width'] : $soft_limit['width'],
'min_height' => isset($hard_limit['height']) ? $hard_limit['height'] : $soft_limit['height'],
'aspect_ratio' => !empty($crop_type
->getAspectRatio()) ? $crop_type
->getAspectRatio() : 'NaN',
];
/** @var \Drupal\automated_crop\AutomatedCropInterface $automated_crop */
$automated_crop = \Drupal::service('plugin.manager.automated_crop')
->createInstance($event
->getConfiguration()['automatic_crop_provider'], $configuration);
$values = [
'type' => $crop_type
->id(),
'uri' => $image
->getSource(),
'x' => $automated_crop
->anchor()['x'] + $automated_crop
->size()['width'] / 2,
'y' => $automated_crop
->anchor()['y'] + $automated_crop
->size()['height'] / 2,
'width' => $automated_crop
->size()['width'],
'height' => $automated_crop
->size()['height'],
];
/** @var \Drupal\crop\CropInterface $crop */
$crop = $this->cropStorage
->create($values);
$crop
->save();
$event
->setCrop($crop);
}