function Crop::validateArguments

Same name in other branches
  1. 8.9.x core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php \Drupal\system\Plugin\ImageToolkit\Operation\gd\Crop::validateArguments()
  2. 10 core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php \Drupal\system\Plugin\ImageToolkit\Operation\gd\Crop::validateArguments()
  3. 11.x core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php \Drupal\system\Plugin\ImageToolkit\Operation\gd\Crop::validateArguments()

Overrides ImageToolkitOperationBase::validateArguments

File

core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php, line 45

Class

Crop
Defines GD2 Crop operation.

Namespace

Drupal\system\Plugin\ImageToolkit\Operation\gd

Code

protected function validateArguments(array $arguments) {
    // Assure at least one dimension.
    if (empty($arguments['width']) && empty($arguments['height'])) {
        throw new \InvalidArgumentException("At least one dimension ('width' or 'height') must be provided to the image 'crop' operation");
    }
    // Preserve aspect.
    $aspect = $this->getToolkit()
        ->getHeight() / $this->getToolkit()
        ->getWidth();
    $arguments['height'] = empty($arguments['height']) ? $arguments['width'] * $aspect : $arguments['height'];
    $arguments['width'] = empty($arguments['width']) ? $arguments['height'] / $aspect : $arguments['width'];
    // Assure integers for all arguments.
    foreach ([
        'x',
        'y',
        '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 'crop' operation");
    }
    if ($arguments['height'] <= 0) {
        throw new \InvalidArgumentException("Invalid height ('{$arguments['height']}') specified for the image 'crop' operation");
    }
    return $arguments;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.