Version 1
[yaffs-website] / web / core / modules / image / src / Plugin / ImageEffect / CropImageEffect.php
1 <?php
2
3 namespace Drupal\image\Plugin\ImageEffect;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Image\ImageInterface;
7
8 /**
9  * Crops an image resource.
10  *
11  * @ImageEffect(
12  *   id = "image_crop",
13  *   label = @Translation("Crop"),
14  *   description = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.")
15  * )
16  */
17 class CropImageEffect extends ResizeImageEffect {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function applyEffect(ImageInterface $image) {
23     list($x, $y) = explode('-', $this->configuration['anchor']);
24     $x = image_filter_keyword($x, $image->getWidth(), $this->configuration['width']);
25     $y = image_filter_keyword($y, $image->getHeight(), $this->configuration['height']);
26     if (!$image->crop($x, $y, $this->configuration['width'], $this->configuration['height'])) {
27       $this->logger->error('Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
28       return FALSE;
29     }
30     return TRUE;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function getSummary() {
37     $summary = [
38       '#theme' => 'image_crop_summary',
39       '#data' => $this->configuration,
40     ];
41     $summary += parent::getSummary();
42
43     return $summary;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function defaultConfiguration() {
50     return parent::defaultConfiguration() + [
51       'anchor' => 'center-center',
52     ];
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
59     $form = parent::buildConfigurationForm($form, $form_state);
60     $form['anchor'] = [
61       '#type' => 'radios',
62       '#title' => t('Anchor'),
63       '#options' => [
64         'left-top' => t('Top left'),
65         'center-top' => t('Top center'),
66         'right-top' => t('Top right'),
67         'left-center' => t('Center left'),
68         'center-center' => t('Center'),
69         'right-center' => t('Center right'),
70         'left-bottom' => t('Bottom left'),
71         'center-bottom' => t('Bottom center'),
72         'right-bottom' => t('Bottom right'),
73       ],
74       '#theme' => 'image_anchor',
75       '#default_value' => $this->configuration['anchor'],
76       '#description' => t('The part of the image that will be retained during the crop.'),
77     ];
78     return $form;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
85     parent::submitConfigurationForm($form, $form_state);
86
87     $this->configuration['anchor'] = $form_state->getValue('anchor');
88   }
89
90 }