You are here

protected function Background::execute in Image Effects 8.2

Same name in this branch
  1. 8.2 src/Plugin/ImageToolkit/Operation/gd/Background.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\Background::execute()
  2. 8.2 src/Plugin/ImageToolkit/Operation/imagemagick/Background.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\Background::execute()
Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageToolkit/Operation/gd/Background.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\Background::execute()
  2. 8 src/Plugin/ImageToolkit/Operation/gd/Background.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\Background::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/Background.php, line 27

Class

Background
Defines GD Background operation.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\gd

Code

protected function execute(array $arguments) {

  // Preserves original resource, to be destroyed upon success.
  $original_resource = $this
    ->getToolkit()
    ->getResource();

  // Prepare a new image.
  $data = [
    'width' => $arguments['background_image']
      ->getWidth(),
    'height' => $arguments['background_image']
      ->getHeight(),
    'extension' => image_type_to_extension($this
      ->getToolkit()
      ->getType(), FALSE),
    'transparent_color' => $this
      ->getToolkit()
      ->getTransparentColor(),
    'is_temp' => TRUE,
  ];
  if (!$this
    ->getToolkit()
    ->apply('create_new', $data)) {

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

  // Overlay background at 0,0.
  $success = $this
    ->imageCopyMergeAlpha($this
    ->getToolkit()
    ->getResource(), $arguments['background_image']
    ->getToolkit()
    ->getResource(), 0, 0, 0, 0, $arguments['background_image']
    ->getWidth(), $arguments['background_image']
    ->getHeight(), 100);
  if (!$success) {

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

  // Overlay original source at offset.
  $success = $this
    ->imageCopyMergeAlpha($this
    ->getToolkit()
    ->getResource(), $original_resource, $arguments['x_offset'], $arguments['y_offset'], 0, 0, imagesx($original_resource), imagesy($original_resource), $arguments['opacity']);
  if ($success) {
    imagedestroy($original_resource);
  }
  else {
    imagedestroy($this
      ->getToolkit()
      ->getResource());
    $this
      ->getToolkit()
      ->setResource($original_resource);
  }
  return $success;
}