3 namespace Drupal\crop\Plugin\ImageEffect;
5 use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Image\ImageFactory;
8 use Drupal\Core\Image\ImageInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\crop\CropInterface;
11 use Drupal\crop\CropStorageInterface;
12 use Drupal\crop\Entity\Crop;
13 use Drupal\image\ConfigurableImageEffectBase;
14 use Psr\Log\LoggerInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * Crops an image resource.
22 * label = @Translation("Manual crop"),
23 * description = @Translation("Applies manually provided crop to the image.")
26 class CropEffect extends ConfigurableImageEffectBase implements ContainerFactoryPluginInterface {
29 * Crop entity storage.
31 * @var \Drupal\crop\CropStorageInterface
36 * Crop type entity storage.
38 * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
40 protected $typeStorage;
45 * @var \Drupal\crop\CropInterface
50 * The image factory service.
52 * @var \Drupal\Core\Image\ImageFactory
54 protected $imageFactory;
59 public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, CropStorageInterface $crop_storage, ConfigEntityStorageInterface $crop_type_storage, ImageFactory $image_factory) {
60 parent::__construct($configuration, $plugin_id, $plugin_definition, $logger);
61 $this->storage = $crop_storage;
62 $this->typeStorage = $crop_type_storage;
63 $this->imageFactory = $image_factory;
69 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
74 $container->get('logger.factory')->get('image'),
75 $container->get('entity_type.manager')->getStorage('crop'),
76 $container->get('entity_type.manager')->getStorage('crop_type'),
77 $container->get('image.factory')
84 public function applyEffect(ImageInterface $image) {
85 if (empty($this->configuration['crop_type']) || !$this->typeStorage->load($this->configuration['crop_type'])) {
86 $this->logger->error('Manual image crop failed due to misconfigured crop type on %path.', ['%path' => $image->getSource()]);
90 if ($crop = $this->getCrop($image)) {
91 $anchor = $crop->anchor();
92 $size = $crop->size();
94 if (!$image->crop($anchor['x'], $anchor['y'], $size['width'], $size['height'])) {
95 $this->logger->error('Manual image crop failed using the %toolkit toolkit on %path (%mimetype, %width x %height)', [
96 '%toolkit' => $image->getToolkitId(),
97 '%path' => $image->getSource(),
98 '%mimetype' => $image->getMimeType(),
99 '%width' => $image->getWidth(),
100 '%height' => $image->getHeight(),
113 public function getSummary() {
115 '#theme' => 'crop_crop_summary',
116 '#data' => $this->configuration,
118 $summary += parent::getSummary();
126 public function defaultConfiguration() {
127 return parent::defaultConfiguration() + [
135 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
137 foreach ($this->typeStorage->loadMultiple() as $type) {
138 $options[$type->id()] = $type->label();
141 $form['crop_type'] = [
143 '#title' => t('Crop type'),
144 '#default_value' => $this->configuration['crop_type'],
145 '#options' => $options,
146 '#description' => t('Crop type to be used for the image style.'),
155 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
156 parent::submitConfigurationForm($form, $form_state);
157 $this->configuration['crop_type'] = $form_state->getValue('crop_type');
161 * Gets crop entity for the image.
163 * @param ImageInterface $image
166 * @return \Drupal\Core\Entity\EntityInterface|\Drupal\crop\CropInterface|false
167 * Crop entity or FALSE if crop doesn't exist.
169 protected function getCrop(ImageInterface $image) {
170 if (!isset($this->crop)) {
172 if ($crop = Crop::findCrop($image->getSource(), $this->configuration['crop_type'])) {
183 public function transformDimensions(array &$dimensions, $uri) {
184 /** @var \Drupal\Core\Image\Image $image */
185 $image = $this->imageFactory->get($uri);
187 /** @var \Drupal\crop\CropInterface $crop */
188 $crop = $this->getCrop($image);
189 if (!$crop instanceof CropInterface) {
192 $size = $crop->size();
194 // The new image will have the exact dimensions defined for the crop effect.
195 $dimensions['width'] = $size['width'];
196 $dimensions['height'] = $size['height'];