3 namespace Drupal\image\Plugin\ImageEffect;
5 use Drupal\Component\Utility\Image;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Image\ImageInterface;
10 * Scales an image resource.
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.")
18 class ScaleImageEffect extends ResizeImageEffect {
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()]);
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']);
43 public function getSummary() {
45 '#theme' => 'image_scale_summary',
46 '#data' => $this->configuration,
48 $summary += parent::getSummary();
56 public function defaultConfiguration() {
57 return parent::defaultConfiguration() + [
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;
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.'),
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.'));
91 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
92 parent::submitConfigurationForm($form, $form_state);
94 $this->configuration['upscale'] = $form_state->getValue('upscale');