annotate core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.php @ 19:fa3358dc1485 tip

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