You are here

protected function Background::execute in Image Effects 8.3

Same name in this branch
  1. 8.3 src/Plugin/ImageToolkit/Operation/gd/Background.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\Background::execute()
  2. 8.3 src/Plugin/ImageToolkit/Operation/imagemagick/Background.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\Background::execute()
Same name and namespace in other branches
  1. 8 src/Plugin/ImageToolkit/Operation/imagemagick/Background.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\Background::execute()
  2. 8.2 src/Plugin/ImageToolkit/Operation/imagemagick/Background.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\Background::execute()

File

src/Plugin/ImageToolkit/Operation/imagemagick/Background.php, line 26

Class

Background
Defines ImageMagick Background operation.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick

Code

protected function execute(array $arguments) {

  // Background image local path.
  $local_path = $arguments['background_image']
    ->getToolkit()
    ->ensureSourceLocalPath();
  if ($local_path !== '') {
    $image_path = $this
      ->escapeArgument($local_path);
  }
  else {
    $source_path = $arguments['background_image']
      ->getToolkit()
      ->getSource();
    throw new \InvalidArgumentException("Missing local path for image at {$source_path}");
  }

  // Reset any gravity settings from earlier effects.
  $op = '-gravity None';

  // Set transparent background.
  $op .= " -background transparent";

  // Set the dimensions of the background.
  $w = $arguments['background_image']
    ->getToolkit()
    ->getWidth();
  $h = $arguments['background_image']
    ->getToolkit()
    ->getHeight();

  // Reverse offset sign. Offset arguments require a sign in front.
  // @todo the minus before $arguments gives issues to PHPCS, either as is
  // or with a space in between the minus and the variable. See if later
  // sniffs fix that.
  // @codingStandardsIgnoreStart
  $x = $arguments['x_offset'] > 0 ? '-' . $arguments['x_offset'] : '+' . -$arguments['x_offset'];
  $y = $arguments['y_offset'] > 0 ? '-' . $arguments['y_offset'] : '+' . -$arguments['y_offset'];

  // @codingStandardsIgnoreEnd
  $op .= " -extent {$w}x{$h}{$x}{$y} ";

  // Add the background image.
  $op .= $image_path;

  // Compose it with the destination.
  if ($arguments['opacity'] == 100) {
    $op .= ' -compose dst-over -composite';
  }
  else {
    $op .= " -compose blend -define compose:args=100,{$arguments['opacity']} -composite";
  }
  $this
    ->addArgument($op);
  $this
    ->getToolkit()
    ->setWidth($w)
    ->setHeight($h);
  return TRUE;
}