You are here

protected function TextToWrapper::resizeWrapper in Image Effects 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageToolkit/Operation/gd/TextToWrapper.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\TextToWrapper::resizeWrapper()
  2. 8 src/Plugin/ImageToolkit/Operation/gd/TextToWrapper.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\TextToWrapper::resizeWrapper()

Resizes the text wrapping image.

Parameters

array $arguments: An associative array of arguments.

1 call to TextToWrapper::resizeWrapper()
TextToWrapper::execute in src/Plugin/ImageToolkit/Operation/gd/TextToWrapper.php
Performs the actual manipulation on the image.

File

src/Plugin/ImageToolkit/Operation/gd/TextToWrapper.php, line 239

Class

TextToWrapper
Defines GD Text Overlay text-to-wrapper operation.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\gd

Code

protected function resizeWrapper(array $arguments) {

  // Wrapper image dimensions.
  $original_wrapper_width = $this
    ->getToolkit()
    ->getWidth();
  $original_wrapper_height = $this
    ->getToolkit()
    ->getHeight();

  // Determine wrapper offset, based on placement option and direct
  // offset indicated in settings.
  $wrapper_xpos = ceil(image_filter_keyword($arguments['layout_x_pos'], $arguments['canvas_width'], $original_wrapper_width)) + $arguments['layout_x_offset'];
  $wrapper_ypos = ceil(image_filter_keyword($arguments['layout_y_pos'], $arguments['canvas_height'], $original_wrapper_height)) + $arguments['layout_y_offset'];

  // Position of wrapper's bottom right point.
  $xc_pos = $wrapper_xpos + $original_wrapper_width;
  $yc_pos = $wrapper_ypos + $original_wrapper_height;

  // Redetermine offset wrapper position and size based on
  // background image size.
  $wrapper_xpos = max(0, $wrapper_xpos);
  $wrapper_ypos = max(0, $wrapper_ypos);
  $xc_pos = min($arguments['canvas_width'], $xc_pos);
  $yc_pos = min($arguments['canvas_height'], $yc_pos);
  $wrapper_width = $xc_pos - $wrapper_xpos;
  $wrapper_height = $yc_pos - $wrapper_ypos;

  // If negative width/height, then the wrapper is totally
  // overflowing the background, and we cannot resize it.
  if ($wrapper_width < 0 || $wrapper_height < 0) {
    return;
  }

  // Determine if scaling needed. Take the side that is shrinking
  // most.
  $width_resize_index = $wrapper_width / $original_wrapper_width;
  $height_resize_index = $wrapper_height / $original_wrapper_height;
  if ($width_resize_index < 1 || $height_resize_index < 1) {
    if ($width_resize_index < $height_resize_index) {
      $wrapper_height = NULL;
    }
    else {
      $wrapper_width = NULL;
    }
    $this
      ->getToolkit()
      ->apply('scale', [
      'width' => $wrapper_width,
      'height' => $wrapper_height,
    ]);
  }
}