function Convert::execute

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

Overrides ImageToolkitOperationBase::execute

File

core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php, line 42

Class

Convert
Defines GD2 convert operation.

Namespace

Drupal\system\Plugin\ImageToolkit\Operation\gd

Code

protected function execute(array $arguments) {
    // Create a new resource of the required dimensions and format, and copy
    // the original resource on it with resampling. Destroy the original
    // resource upon success.
    $width = $this->getToolkit()
        ->getWidth();
    $height = $this->getToolkit()
        ->getHeight();
    $original_resource = $this->getToolkit()
        ->getResource();
    $original_type = $this->getToolkit()
        ->getType();
    $data = [
        'width' => $width,
        'height' => $height,
        'extension' => $arguments['extension'],
        'transparent_color' => $this->getToolkit()
            ->getTransparentColor(),
        'is_temp' => TRUE,
    ];
    if ($this->getToolkit()
        ->apply('create_new', $data)) {
        if (imagecopyresampled($this->getToolkit()
            ->getResource(), $original_resource, 0, 0, 0, 0, $width, $height, $width, $height)) {
            imagedestroy($original_resource);
            return TRUE;
        }
        // In case of error, reset resource and type to as it was.
        $this->getToolkit()
            ->setResource($original_resource);
        $this->getToolkit()
            ->setType($original_type);
    }
    return FALSE;
}

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