comparison core/modules/image/src/Plugin/ImageEffect/ScaleImageEffect.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\image\Plugin\ImageEffect;
4
5 use Drupal\Component\Utility\Image;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Image\ImageInterface;
8
9 /**
10 * Scales an image resource.
11 *
12 * @ImageEffect(
13 * id = "image_scale",
14 * label = @Translation("Scale"),
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.")
16 * )
17 */
18 class ScaleImageEffect extends ResizeImageEffect {
19
20 /**
21 * {@inheritdoc}
22 */
23 public function applyEffect(ImageInterface $image) {
24 if (!$image->scale($this->configuration['width'], $this->configuration['height'], $this->configuration['upscale'])) {
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()]);
26 return FALSE;
27 }
28 return TRUE;
29 }
30
31 /**
32 * {@inheritdoc}
33 */
34 public function transformDimensions(array &$dimensions, $uri) {
35 if ($dimensions['width'] && $dimensions['height']) {
36 Image::scaleDimensions($dimensions, $this->configuration['width'], $this->configuration['height'], $this->configuration['upscale']);
37 }
38 }
39
40 /**
41 * {@inheritdoc}
42 */
43 public function getSummary() {
44 $summary = [
45 '#theme' => 'image_scale_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 parent::defaultConfiguration() + [
58 'upscale' => FALSE,
59 ];
60 }
61
62 /**
63 * {@inheritdoc}
64 */
65 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
66 $form = parent::buildConfigurationForm($form, $form_state);
67 $form['width']['#required'] = FALSE;
68 $form['height']['#required'] = FALSE;
69 $form['upscale'] = [
70 '#type' => 'checkbox',
71 '#default_value' => $this->configuration['upscale'],
72 '#title' => t('Allow Upscaling'),
73 '#description' => t('Let scale make images larger than their original size.'),
74 ];
75 return $form;
76 }
77
78 /**
79 * {@inheritdoc}
80 */
81 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
82 parent::validateConfigurationForm($form, $form_state);
83 if ($form_state->isValueEmpty('width') && $form_state->isValueEmpty('height')) {
84 $form_state->setErrorByName('data', $this->t('Width and height can not both be blank.'));
85 }
86 }
87
88 /**
89 * {@inheritdoc}
90 */
91 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
92 parent::submitConfigurationForm($form, $form_state);
93
94 $this->configuration['upscale'] = $form_state->getValue('upscale');
95 }
96
97 }