Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\image\Plugin\ImageEffect;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Image;
|
Chris@0
|
6 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
7 use Drupal\Core\Image\ImageInterface;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Scales an image resource.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @ImageEffect(
|
Chris@0
|
13 * id = "image_scale",
|
Chris@0
|
14 * label = @Translation("Scale"),
|
Chris@0
|
15 * description = @Translation("Scaling will maintain the aspect-ratio of the original image. If only a single dimension is specified, the other dimension will be calculated.")
|
Chris@0
|
16 * )
|
Chris@0
|
17 */
|
Chris@0
|
18 class ScaleImageEffect extends ResizeImageEffect {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * {@inheritdoc}
|
Chris@0
|
22 */
|
Chris@0
|
23 public function applyEffect(ImageInterface $image) {
|
Chris@0
|
24 if (!$image->scale($this->configuration['width'], $this->configuration['height'], $this->configuration['upscale'])) {
|
Chris@0
|
25 $this->logger->error('Image scale failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
|
Chris@0
|
26 return FALSE;
|
Chris@0
|
27 }
|
Chris@0
|
28 return TRUE;
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * {@inheritdoc}
|
Chris@0
|
33 */
|
Chris@0
|
34 public function transformDimensions(array &$dimensions, $uri) {
|
Chris@0
|
35 if ($dimensions['width'] && $dimensions['height']) {
|
Chris@0
|
36 Image::scaleDimensions($dimensions, $this->configuration['width'], $this->configuration['height'], $this->configuration['upscale']);
|
Chris@0
|
37 }
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * {@inheritdoc}
|
Chris@0
|
42 */
|
Chris@0
|
43 public function getSummary() {
|
Chris@0
|
44 $summary = [
|
Chris@0
|
45 '#theme' => 'image_scale_summary',
|
Chris@0
|
46 '#data' => $this->configuration,
|
Chris@0
|
47 ];
|
Chris@0
|
48 $summary += parent::getSummary();
|
Chris@0
|
49
|
Chris@0
|
50 return $summary;
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * {@inheritdoc}
|
Chris@0
|
55 */
|
Chris@0
|
56 public function defaultConfiguration() {
|
Chris@0
|
57 return parent::defaultConfiguration() + [
|
Chris@0
|
58 'upscale' => FALSE,
|
Chris@0
|
59 ];
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * {@inheritdoc}
|
Chris@0
|
64 */
|
Chris@0
|
65 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
66 $form = parent::buildConfigurationForm($form, $form_state);
|
Chris@0
|
67 $form['width']['#required'] = FALSE;
|
Chris@0
|
68 $form['height']['#required'] = FALSE;
|
Chris@0
|
69 $form['upscale'] = [
|
Chris@0
|
70 '#type' => 'checkbox',
|
Chris@0
|
71 '#default_value' => $this->configuration['upscale'],
|
Chris@0
|
72 '#title' => t('Allow Upscaling'),
|
Chris@0
|
73 '#description' => t('Let scale make images larger than their original size.'),
|
Chris@0
|
74 ];
|
Chris@0
|
75 return $form;
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * {@inheritdoc}
|
Chris@0
|
80 */
|
Chris@0
|
81 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
82 parent::validateConfigurationForm($form, $form_state);
|
Chris@0
|
83 if ($form_state->isValueEmpty('width') && $form_state->isValueEmpty('height')) {
|
Chris@0
|
84 $form_state->setErrorByName('data', $this->t('Width and height can not both be blank.'));
|
Chris@0
|
85 }
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * {@inheritdoc}
|
Chris@0
|
90 */
|
Chris@0
|
91 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
92 parent::submitConfigurationForm($form, $form_state);
|
Chris@0
|
93
|
Chris@0
|
94 $this->configuration['upscale'] = $form_state->getValue('upscale');
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 }
|