You are here

protected function TextOverlayImageEffect::canvasResizeNeeded in Image Effects 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageEffect/TextOverlayImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\TextOverlayImageEffect::canvasResizeNeeded()
  2. 8 src/Plugin/ImageEffect/TextOverlayImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\TextOverlayImageEffect::canvasResizeNeeded()

Recalculate background image size.

When wrapper overflows the original image, and autoextent is set on.

2 calls to TextOverlayImageEffect::canvasResizeNeeded()
TextOverlayImageEffect::applyEffect in src/Plugin/ImageEffect/TextOverlayImageEffect.php
Applies an image effect to the image object.
TextOverlayImageEffect::transformDimensions in src/Plugin/ImageEffect/TextOverlayImageEffect.php
Determines the dimensions of the styled image.

File

src/Plugin/ImageEffect/TextOverlayImageEffect.php, line 989

Class

TextOverlayImageEffect
Overlays text on the image, defining text font, size and positioning.

Namespace

Drupal\image_effects\Plugin\ImageEffect

Code

protected function canvasResizeNeeded(ImageInterface $wrapper) {
  $resized = FALSE;

  // Background image dimensions.
  $image_width = $this->info['image_width'];
  $image_height = $this->info['image_height'];

  // Wrapper image dimensions.
  $wrapper_width = $wrapper
    ->getWidth();
  $wrapper_height = $wrapper
    ->getHeight();

  // Determine wrapper offset, based on placement option.
  // This is just taking into account the image and wrapper dimensions;
  // additional offset explicitly specified is considered later.
  $x_offset = ceil(image_filter_keyword($this->configuration['layout']['x_pos'], $image_width, $wrapper_width));
  $y_offset = ceil(image_filter_keyword($this->configuration['layout']['y_pos'], $image_height, $wrapper_height));

  // The position of the wrapper, once offset as per explicit
  // input. Width and height are not relevant for the algorithm,
  // but would be determined as follows:
  // @codingStandardsIgnoreStart
  // @code
  //  'width' => ($wrapper_width < $image_width) ? $wrapper_width + abs($this->configuration['layout']['x_offset']) : $wrapper_width;
  //  'height' = ($wrapper_height < $image_height) ? $wrapper_height + abs($this->configuration['layout']['y_offset']) : $wrapper_height;
  // @endcode
  // @codingStandardsIgnoreEnd
  $this->info['wrapper_xpos'] = $x_offset + $this->configuration['layout']['x_offset'];
  $this->info['wrapper_ypos'] = $y_offset + $this->configuration['layout']['y_offset'];

  // If offset wrapper overflows to the left, background image
  // will be shifted to the right.
  if ($this->info['wrapper_xpos'] < 0) {
    $this->info['image_width'] = $image_width - $this->info['wrapper_xpos'];
    $this->info['image_xpos'] = -$this->info['wrapper_xpos'];
    $this->info['wrapper_xpos'] = 0;
    $this->info['frame_left'] = $this->info['image_width'] - $image_width;
    $resized = TRUE;
  }

  // If offset wrapper overflows to the top, background image
  // will be shifted to the bottom.
  if ($this->info['wrapper_ypos'] < 0) {
    $this->info['image_height'] = $image_height - $this->info['wrapper_ypos'];
    $this->info['image_ypos'] = -$this->info['wrapper_ypos'];
    $this->info['wrapper_ypos'] = 0;
    $this->info['frame_top'] = $this->info['image_height'] - $image_height;
    $resized = TRUE;
  }

  // If offset wrapper overflows to the right, background image
  // will be extended to the right.
  if ($this->info['wrapper_xpos'] + $wrapper_width > $this->info['image_width']) {
    $tmp = $this->info['image_width'];
    $this->info['image_width'] = $this->info['wrapper_xpos'] + $wrapper_width;
    $this->info['frame_right'] = $this->info['image_width'] - $tmp;
    $resized = TRUE;
  }

  // If offset wrapper overflows to the bottom, background image
  // will be extended to the bottom.
  if ($this->info['wrapper_ypos'] + $wrapper_height > $this->info['image_height']) {
    $tmp = $this->info['image_height'];
    $this->info['image_height'] = $this->info['wrapper_ypos'] + $wrapper_height;
    $this->info['frame_bottom'] = $this->info['image_height'] - $tmp;
    $resized = TRUE;
  }
  return $resized;
}