You are here

public function SetCanvasImageEffect::applyEffect in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageEffect/SetCanvasImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\SetCanvasImageEffect::applyEffect()
  2. 8.2 src/Plugin/ImageEffect/SetCanvasImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\SetCanvasImageEffect::applyEffect()

Applies an image effect to the image object.

Parameters

\Drupal\Core\Image\ImageInterface $image: An image file object.

Return value

bool TRUE on success. FALSE if unable to perform the image effect on the image.

Overrides ImageEffectInterface::applyEffect

File

src/Plugin/ImageEffect/SetCanvasImageEffect.php, line 215

Class

SetCanvasImageEffect
Class SetCanvasImageEffect.

Namespace

Drupal\image_effects\Plugin\ImageEffect

Code

public function applyEffect(ImageInterface $image) {
  $data = [];
  $data['canvas_color'] = $this->configuration['canvas_color'];

  // Get resulting dimensions.
  $dimensions = $this
    ->getDimensions($image
    ->getWidth(), $image
    ->getHeight());
  $data['width'] = $dimensions['width'];
  $data['height'] = $dimensions['height'];

  // Get offset of original image.
  if ($this->configuration['canvas_size'] === 'exact') {
    list($x_pos, $y_pos) = explode('-', $this->configuration['exact']['placement']);
    $data['x_pos'] = image_filter_keyword($x_pos, $data['width'], $image
      ->getWidth()) + $this->configuration['exact']['x_offset'];
    $data['y_pos'] = image_filter_keyword($y_pos, $data['height'], $image
      ->getHeight()) + $this->configuration['exact']['y_offset'];
  }
  else {
    $data['x_pos'] = $this->configuration['relative']['left'];
    $data['y_pos'] = $this->configuration['relative']['top'];
  }

  // All the math is done, now defer to the toolkit in use.
  return $image
    ->apply('set_canvas', $data);
}