comparison core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.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\Color;
6 use Drupal\Component\Utility\Rectangle;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Image\ImageInterface;
9 use Drupal\image\ConfigurableImageEffectBase;
10
11 /**
12 * Rotates an image resource.
13 *
14 * @ImageEffect(
15 * id = "image_rotate",
16 * label = @Translation("Rotate"),
17 * description = @Translation("Rotating an image may cause the dimensions of an image to increase to fit the diagonal.")
18 * )
19 */
20 class RotateImageEffect extends ConfigurableImageEffectBase {
21
22 /**
23 * {@inheritdoc}
24 */
25 public function applyEffect(ImageInterface $image) {
26 if (!empty($this->configuration['random'])) {
27 $degrees = abs((float) $this->configuration['degrees']);
28 $this->configuration['degrees'] = rand(-$degrees, $degrees);
29 }
30
31 if (!$image->rotate($this->configuration['degrees'], $this->configuration['bgcolor'])) {
32 $this->logger->error('Image rotate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
33 return FALSE;
34 }
35 return TRUE;
36 }
37
38 /**
39 * {@inheritdoc}
40 */
41 public function transformDimensions(array &$dimensions, $uri) {
42 // If the rotate is not random and current dimensions are set,
43 // then the new dimensions can be determined.
44 if (!$this->configuration['random'] && $dimensions['width'] && $dimensions['height']) {
45 $rect = new Rectangle($dimensions['width'], $dimensions['height']);
46 $rect = $rect->rotate($this->configuration['degrees']);
47 $dimensions['width'] = $rect->getBoundingWidth();
48 $dimensions['height'] = $rect->getBoundingHeight();
49 }
50 else {
51 $dimensions['width'] = $dimensions['height'] = NULL;
52 }
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 public function getSummary() {
59 $summary = [
60 '#theme' => 'image_rotate_summary',
61 '#data' => $this->configuration,
62 ];
63 $summary += parent::getSummary();
64
65 return $summary;
66 }
67
68 /**
69 * {@inheritdoc}
70 */
71 public function defaultConfiguration() {
72 return [
73 'degrees' => 0,
74 'bgcolor' => NULL,
75 'random' => FALSE,
76 ];
77 }
78
79 /**
80 * {@inheritdoc}
81 */
82 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
83 $form['degrees'] = [
84 '#type' => 'number',
85 '#default_value' => $this->configuration['degrees'],
86 '#title' => t('Rotation angle'),
87 '#description' => t('The number of degrees the image should be rotated. Positive numbers are clockwise, negative are counter-clockwise.'),
88 '#field_suffix' => '°',
89 '#required' => TRUE,
90 ];
91 $form['bgcolor'] = [
92 '#type' => 'textfield',
93 '#default_value' => $this->configuration['bgcolor'],
94 '#title' => t('Background color'),
95 '#description' => t('The background color to use for exposed areas of the image. Use web-style hex colors (#FFFFFF for white, #000000 for black). Leave blank for transparency on image types that support it.'),
96 '#size' => 7,
97 '#maxlength' => 7,
98 ];
99 $form['random'] = [
100 '#type' => 'checkbox',
101 '#default_value' => $this->configuration['random'],
102 '#title' => t('Randomize'),
103 '#description' => t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'),
104 ];
105 return $form;
106 }
107
108 /**
109 * {@inheritdoc}
110 */
111 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
112 if (!$form_state->isValueEmpty('bgcolor') && !Color::validateHex($form_state->getValue('bgcolor'))) {
113 $form_state->setErrorByName('bgcolor', $this->t('Background color must be a hexadecimal color value.'));
114 }
115 }
116
117 /**
118 * {@inheritdoc}
119 */
120 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
121 parent::submitConfigurationForm($form, $form_state);
122
123 $this->configuration['degrees'] = $form_state->getValue('degrees');
124 $this->configuration['bgcolor'] = $form_state->getValue('bgcolor');
125 $this->configuration['random'] = $form_state->getValue('random');
126 }
127
128 }