Chris@0: configuration['random'])) { Chris@0: $degrees = abs((float) $this->configuration['degrees']); Chris@0: $this->configuration['degrees'] = rand(-$degrees, $degrees); Chris@0: } Chris@0: Chris@0: if (!$image->rotate($this->configuration['degrees'], $this->configuration['bgcolor'])) { Chris@0: $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: return FALSE; Chris@0: } Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function transformDimensions(array &$dimensions, $uri) { Chris@0: // If the rotate is not random and current dimensions are set, Chris@0: // then the new dimensions can be determined. Chris@0: if (!$this->configuration['random'] && $dimensions['width'] && $dimensions['height']) { Chris@0: $rect = new Rectangle($dimensions['width'], $dimensions['height']); Chris@0: $rect = $rect->rotate($this->configuration['degrees']); Chris@0: $dimensions['width'] = $rect->getBoundingWidth(); Chris@0: $dimensions['height'] = $rect->getBoundingHeight(); Chris@0: } Chris@0: else { Chris@0: $dimensions['width'] = $dimensions['height'] = NULL; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getSummary() { Chris@0: $summary = [ Chris@0: '#theme' => 'image_rotate_summary', Chris@0: '#data' => $this->configuration, Chris@0: ]; Chris@0: $summary += parent::getSummary(); Chris@0: Chris@0: return $summary; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function defaultConfiguration() { Chris@0: return [ Chris@0: 'degrees' => 0, Chris@0: 'bgcolor' => NULL, Chris@0: 'random' => FALSE, Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function buildConfigurationForm(array $form, FormStateInterface $form_state) { Chris@0: $form['degrees'] = [ Chris@0: '#type' => 'number', Chris@0: '#default_value' => $this->configuration['degrees'], Chris@0: '#title' => t('Rotation angle'), Chris@0: '#description' => t('The number of degrees the image should be rotated. Positive numbers are clockwise, negative are counter-clockwise.'), Chris@0: '#field_suffix' => '°', Chris@0: '#required' => TRUE, Chris@0: ]; Chris@0: $form['bgcolor'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#default_value' => $this->configuration['bgcolor'], Chris@0: '#title' => t('Background color'), Chris@0: '#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: '#size' => 7, Chris@0: '#maxlength' => 7, Chris@0: ]; Chris@0: $form['random'] = [ Chris@0: '#type' => 'checkbox', Chris@0: '#default_value' => $this->configuration['random'], Chris@0: '#title' => t('Randomize'), Chris@0: '#description' => t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'), Chris@0: ]; Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { Chris@0: if (!$form_state->isValueEmpty('bgcolor') && !Color::validateHex($form_state->getValue('bgcolor'))) { Chris@0: $form_state->setErrorByName('bgcolor', $this->t('Background color must be a hexadecimal color value.')); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { Chris@0: parent::submitConfigurationForm($form, $form_state); Chris@0: Chris@0: $this->configuration['degrees'] = $form_state->getValue('degrees'); Chris@0: $this->configuration['bgcolor'] = $form_state->getValue('bgcolor'); Chris@0: $this->configuration['random'] = $form_state->getValue('random'); Chris@0: } Chris@0: Chris@0: }