You are here

protected function Replace::execute in Image Replace 8

Same name in this branch
  1. 8 src/Plugin/ImageToolkit/Operation/gd/Replace.php \Drupal\image_replace\Plugin\ImageToolkit\Operation\gd\Replace::execute()
  2. 8 src/Plugin/ImageToolkit/Operation/imagemagick/Replace.php \Drupal\image_replace\Plugin\ImageToolkit\Operation\imagemagick\Replace::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

File

src/Plugin/ImageToolkit/Operation/gd/Replace.php, line 46

Class

Replace
Defines GD2 image_replace operation.

Namespace

Drupal\image_replace\Plugin\ImageToolkit\Operation\gd

Code

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

  // Create a new resource of the required dimensions, and replace the
  // original resource on it with resampling. Destroy the original resource
  // upon success.
  $replacement_toolkit = $this
    ->getReplacementImageToolkit($arguments);
  $original_resource = $this
    ->getToolkit()
    ->getResource();
  $data = [
    'width' => $replacement_toolkit
      ->getWidth(),
    'height' => $replacement_toolkit
      ->getHeight(),
    'extension' => image_type_to_extension($this
      ->getToolkit()
      ->getType(), FALSE),
    'transparent_color' => $replacement_toolkit
      ->getTransparentColor(),
    'is_temp' => TRUE,
  ];
  if ($this
    ->getToolkit()
    ->apply('create_new', $data)) {
    if (imagecopy($this
      ->getToolkit()
      ->getResource(), $replacement_toolkit
      ->getResource(), 0, 0, 0, 0, $data['width'], $data['height'])) {
      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;
}