function Crop::execute

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

Overrides ImageToolkitOperationBase::execute

File

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

Class

Crop
Defines GD2 Crop operation.

Namespace

Drupal\system\Plugin\ImageToolkit\Operation\gd

Code

protected function execute(array $arguments) {
    // Create a new image of the required dimensions, and copy and resize
    // the original image on it with resampling. Restore the original image upon
    // failure.
    $original_image = $this->getToolkit()
        ->getImage();
    $data = [
        'width' => $arguments['width'],
        'height' => $arguments['height'],
        'extension' => image_type_to_extension($this->getToolkit()
            ->getType(), FALSE),
        'transparent_color' => $this->getToolkit()
            ->getTransparentColor(),
        'is_temp' => TRUE,
    ];
    if ($this->getToolkit()
        ->apply('create_new', $data)) {
        if (imagecopyresampled($this->getToolkit()
            ->getImage(), $original_image, 0, 0, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'], $arguments['width'], $arguments['height'])) {
            return TRUE;
        }
        // In case of failure, restore the original image.
        $this->getToolkit()
            ->setImage($original_image);
    }
    return FALSE;
}

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