3 namespace Drupal\image\Plugin\ImageEffect;
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;
12 * Rotates an image resource.
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.")
20 class RotateImageEffect extends ConfigurableImageEffectBase {
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);
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()]);
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();
51 $dimensions['width'] = $dimensions['height'] = NULL;
58 public function getSummary() {
60 '#theme' => 'image_rotate_summary',
61 '#data' => $this->configuration,
63 $summary += parent::getSummary();
71 public function defaultConfiguration() {
82 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
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' => '°',
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.'),
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.'),
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.'));
120 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
121 parent::submitConfigurationForm($form, $form_state);
123 $this->configuration['degrees'] = $form_state->getValue('degrees');
124 $this->configuration['bgcolor'] = $form_state->getValue('bgcolor');
125 $this->configuration['random'] = $form_state->getValue('random');