You are here

trait BackgroundTrait in Image Effects 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageToolkit/Operation/BackgroundTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\BackgroundTrait
  2. 8 src/Plugin/ImageToolkit/Operation/BackgroundTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\BackgroundTrait

Base trait for image_effects Background operations.

Hierarchy

2 files declare their use of BackgroundTrait
Background.php in src/Plugin/ImageToolkit/Operation/gd/Background.php
Background.php in src/Plugin/ImageToolkit/Operation/imagemagick/Background.php

File

src/Plugin/ImageToolkit/Operation/BackgroundTrait.php, line 10

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation
View source
trait BackgroundTrait {

  /**
   * {@inheritdoc}
   */
  protected function arguments() {
    return [
      'x_offset' => [
        'description' => 'X offset for source image.',
      ],
      'y_offset' => [
        'description' => 'Y offset for source image.',
      ],
      'opacity' => [
        'description' => 'Opacity for source image.',
        'required' => FALSE,
        'default' => 100,
      ],
      'background_image' => [
        'description' => 'Image to use for background.',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function validateArguments(array $arguments) {

    // Ensure source image opacity is in the range 0-100.
    if ($arguments['opacity'] > 100 || $arguments['opacity'] < 0) {
      throw new \InvalidArgumentException("Invalid opacity ('{$arguments['opacity']}') specified for the image 'background' operation");
    }

    // Ensure background_image is an expected ImageInterface object.
    if (!$arguments['background_image'] instanceof ImageInterface) {
      throw new \InvalidArgumentException("Background image passed to the 'background' operation is invalid");
    }

    // Ensure background_image is a valid image.
    if (!$arguments['background_image']
      ->isValid()) {
      $source = $arguments['background_image']
        ->getSource();
      throw new \InvalidArgumentException("Invalid image at {$source}");
    }
    return $arguments;
  }

}

Members