You are here

protected function Resize::execute in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php \Drupal\system\Plugin\ImageToolkit\Operation\gd\Resize::execute()

Performs the actual manipulation on the image.

Image toolkit operation implementers must implement this method. This method is responsible for actually performing the operation on the image. When this method gets called, the implementer may assume all arguments, also the optional ones, to be available, validated and corrected.

Parameters

array $arguments: An associative array of arguments to be used by the toolkit operation.

Return value

bool TRUE if the operation was performed successfully, FALSE otherwise.

Overrides ImageToolkitOperationBase::execute

1 call to Resize::execute()
Scale::execute in core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php
Performs the actual manipulation on the image.
1 method overrides Resize::execute()
Scale::execute in core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php
Performs the actual manipulation on the image.

File

core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php, line 54

Class

Resize
Defines GD2 resize operation.

Namespace

Drupal\system\Plugin\ImageToolkit\Operation\gd

Code

protected function execute(array $arguments = []) {

  // Create a new resource of the required dimensions, and copy and resize
  // the original resource on it with resampling. Destroy the original
  // resource upon success.
  $original_resource = $this
    ->getToolkit()
    ->getResource();
  $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()
      ->getResource(), $original_resource, 0, 0, 0, 0, $arguments['width'], $arguments['height'], imagesx($original_resource), imagesy($original_resource))) {
      imagedestroy($original_resource);
      return TRUE;
    }
    else {

      // In case of failure, destroy the temporary resource and restore
      // the original one.
      imagedestroy($this
        ->getToolkit()
        ->getResource());
      $this
        ->getToolkit()
        ->setResource($original_resource);
    }
  }
  return FALSE;
}