dba34298def9522a3f4fc17cb423baed8cdfe19f
[yaffs-website] / web / core / modules / image / src / Plugin / ImageEffect / ResizeImageEffect.php
1 <?php
2
3 namespace Drupal\image\Plugin\ImageEffect;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Image\ImageInterface;
7 use Drupal\image\ConfigurableImageEffectBase;
8
9 /**
10  * Resizes an image resource.
11  *
12  * @ImageEffect(
13  *   id = "image_resize",
14  *   label = @Translation("Resize"),
15  *   description = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.")
16  * )
17  */
18 class ResizeImageEffect extends ConfigurableImageEffectBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function applyEffect(ImageInterface $image) {
24     if (!$image->resize($this->configuration['width'], $this->configuration['height'])) {
25       $this->logger->error('Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
26       return FALSE;
27     }
28     return TRUE;
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function transformDimensions(array &$dimensions, $uri) {
35     // The new image will have the exact dimensions defined for the effect.
36     $dimensions['width'] = $this->configuration['width'];
37     $dimensions['height'] = $this->configuration['height'];
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public function getSummary() {
44     $summary = [
45       '#theme' => 'image_resize_summary',
46       '#data' => $this->configuration,
47     ];
48     $summary += parent::getSummary();
49
50     return $summary;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function defaultConfiguration() {
57     return [
58       'width' => NULL,
59       'height' => NULL,
60     ];
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
67     $form['width'] = [
68       '#type' => 'number',
69       '#title' => t('Width'),
70       '#default_value' => $this->configuration['width'],
71       '#field_suffix' => ' ' . t('pixels'),
72       '#required' => TRUE,
73       '#min' => 1,
74     ];
75     $form['height'] = [
76       '#type' => 'number',
77       '#title' => t('Height'),
78       '#default_value' => $this->configuration['height'],
79       '#field_suffix' => ' ' . t('pixels'),
80       '#required' => TRUE,
81       '#min' => 1,
82     ];
83     return $form;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
90     parent::submitConfigurationForm($form, $form_state);
91
92     $this->configuration['height'] = $form_state->getValue('height');
93     $this->configuration['width'] = $form_state->getValue('width');
94   }
95
96 }