You are here

trait SmartCropTrait in Image Effects 8.3

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

Base trait for Smart Crop operations.

Hierarchy

  • trait \Drupal\image_effects\Plugin\ImageToolkit\Operation\SmartCropTrait
2 files declare their use of SmartCropTrait
SmartCrop.php in src/Plugin/ImageToolkit/Operation/gd/SmartCrop.php
SmartCrop.php in src/Plugin/ImageToolkit/Operation/imagemagick/SmartCrop.php

File

src/Plugin/ImageToolkit/Operation/SmartCropTrait.php, line 8

Namespace

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

  /**
   * {@inheritdoc}
   */
  protected function arguments() {
    return [
      'algorithm' => [
        'description' => 'The calculation algorithm for the crop',
        'required' => TRUE,
      ],
      'algorithm_params' => [
        'description' => 'The calculation algorithm parameters',
        'required' => FALSE,
        'default' => [],
      ],
      'simulate' => [
        'description' => 'Boolean indicating the crop shall not be executed, but just the crop area highlighted on the source image',
        'required' => FALSE,
        'default' => FALSE,
      ],
      'width' => [
        'description' => 'An integer representing the desired width in pixels',
        'required' => TRUE,
      ],
      'height' => [
        'description' => 'An integer representing the desired height in pixels',
        'required' => TRUE,
      ],
    ];
  }

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

    // Assure integers for all arguments.
    foreach ([
      'width',
      'height',
    ] as $key) {
      $arguments[$key] = (int) round($arguments[$key]);
    }

    // Fail when width or height are 0 or negative.
    if ($arguments['width'] <= 0) {
      throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image '{$this->getPluginDefinition()['operation']}' operation");
    }
    if ($arguments['height'] <= 0) {
      throw new \InvalidArgumentException("Invalid height ('{$arguments['height']}') specified for the image '{$this->getPluginDefinition()['operation']}' operation");
    }
    switch ($arguments['algorithm']) {
      case 'entropy_grid':
        $arguments['algorithm_params'] = array_merge([
          'grid_width' => 100,
          'grid_height' => 100,
          'grid_rows' => 5,
          'grid_cols' => 5,
          'grid_sub_rows' => 3,
          'grid_sub_cols' => 3,
        ], $arguments['algorithm_params']);
        break;
      default:
        break;
    }
    return $arguments;
  }

}

Members