You are here

protected function Watermark::execute in Image Effects 8.2

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

File

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

Class

Watermark
Defines ImageMagick Watermark operation.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick

Code

protected function execute(array $arguments) {

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

  // Set the dimensions of the overlay.
  $w = $arguments['watermark_width'] ?: $arguments['watermark_image']
    ->getToolkit()
    ->getWidth();
  $h = $arguments['watermark_height'] ?: $arguments['watermark_image']
    ->getToolkit()
    ->getHeight();

  // Set offset. Offset arguments require a sign in front.
  $x = $arguments['x_offset'] >= 0 ? '+' . $arguments['x_offset'] : $arguments['x_offset'];
  $y = $arguments['y_offset'] >= 0 ? '+' . $arguments['y_offset'] : $arguments['y_offset'];

  // Compose it with the destination.
  switch ($this
    ->getToolkit()
    ->getExecManager()
    ->getPackage()) {
    case 'imagemagick':
      if ($arguments['opacity'] == 100) {
        $op = "-gravity None {$image_path} -geometry {$w}x{$h}!{$x}{$y} -compose src-over -composite";
      }
      else {
        $op = "-gravity None {$image_path} -geometry {$w}x{$h}!{$x}{$y} -compose blend -define compose:args={$arguments['opacity']} -composite";
      }
      break;
    case 'graphicsmagick':

      // @todo see if GraphicsMagick can support opacity setting.
      $op = "-draw 'image Over {$arguments['x_offset']},{$arguments['y_offset']} {$w},{$h} {$image_path}'";
      break;
  }
  $this
    ->addArgument($op);
  return TRUE;
}