You are here

class Mask in Image Effects 8.3

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

Defines ImageMagick Mask operation.

Plugin annotation


@ImageToolkitOperation(
  id = "image_effects_imagemagick_mask",
  toolkit = "imagemagick",
  operation = "mask",
  label = @Translation("Mask"),
  description = @Translation("Applies a mask to the source image.")
)

Hierarchy

  • class \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\Mask extends \Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick\ImagemagickImageToolkitOperationBase uses MaskTrait

Expanded class hierarchy of Mask

File

src/Plugin/ImageToolkit/Operation/imagemagick/Mask.php, line 19

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick
View source
class Mask extends ImagemagickImageToolkitOperationBase {
  use MaskTrait;

  /**
   * {@inheritdoc}
   */
  protected function execute(array $arguments) {
    if ($this
      ->getToolkit()
      ->getExecManager()
      ->getPackage() === 'graphicsmagick') {

      // GraphicsMagick does not support this operation, return early.
      // @todo implement a GraphicsMagick solution if possible.
      return FALSE;
    }

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

    // Set the dimensions of the mask. Use of the scale option means that we
    // need to change the dimensions: always set them, they don't harm when the
    // scale option is not used.
    $w = $arguments['mask_width'] ?: $arguments['mask_image']
      ->getToolkit()
      ->getWidth();
    $h = $arguments['mask_height'] ?: $arguments['mask_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'];

    // Prepare argument.
    $op = "-gravity None {$image_path} -geometry {$w}x{$h}!{$x}{$y} -alpha Off -compose CopyOpacity -composite -gravity none -background transparent -compose src-over -extent {$this->getToolkit()->getWidth()}x{$this->getToolkit()->getHeight()}";
    $this
      ->addArgument($op);
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Mask::execute protected function
MaskTrait::arguments protected function
MaskTrait::validateArguments protected function