3 namespace Drupal\image\Plugin\ImageEffect;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Image\ImageInterface;
7 use Drupal\image\ConfigurableImageEffectBase;
10 * Resizes an image resource.
13 * id = "image_resize",
14 * label = @Translation("Resize"),
15 * description = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.")
18 class ResizeImageEffect extends ConfigurableImageEffectBase {
23 public function applyEffect(ImageInterface $image) {
24 if (!$image->resize($this->configuration['width'], $this->configuration['height'])) {
25 $this->logger->error('Image resize 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 // The new image will have the exact dimensions defined for the effect.
36 $dimensions['width'] = $this->configuration['width'];
37 $dimensions['height'] = $this->configuration['height'];
43 public function getSummary() {
45 '#theme' => 'image_resize_summary',
46 '#data' => $this->configuration,
48 $summary += parent::getSummary();
56 public function defaultConfiguration() {
66 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
69 '#title' => t('Width'),
70 '#default_value' => $this->configuration['width'],
71 '#field_suffix' => ' ' . t('pixels'),
77 '#title' => t('Height'),
78 '#default_value' => $this->configuration['height'],
79 '#field_suffix' => ' ' . t('pixels'),
89 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
90 parent::submitConfigurationForm($form, $form_state);
92 $this->configuration['height'] = $form_state->getValue('height');
93 $this->configuration['width'] = $form_state->getValue('width');