You are here

protected function SetCanvas::execute in Image Effects 8

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

Class

SetCanvas
Defines GD2 set canvas operation.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\gd

Code

protected function execute(array $arguments) {

  // Store the original resource.
  $original_res = $this
    ->getToolkit()
    ->getResource();

  // Prepare the canvas.
  $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)) {
    return FALSE;
  }

  // Fill the canvas with required color.
  $data = [
    'rectangle' => new PositionedRectangle($arguments['width'], $arguments['height']),
    'fill_color' => $arguments['canvas_color'],
  ];
  if (!$this
    ->getToolkit()
    ->apply('draw_rectangle', $data)) {
    return FALSE;
  }

  // Overlay the current image on the canvas.
  imagealphablending($original_res, TRUE);
  imagesavealpha($original_res, TRUE);
  imagealphablending($this
    ->getToolkit()
    ->getResource(), TRUE);
  imagesavealpha($this
    ->getToolkit()
    ->getResource(), TRUE);
  if (imagecopy($this
    ->getToolkit()
    ->getResource(), $original_res, $arguments['x_pos'], $arguments['y_pos'], 0, 0, imagesx($original_res), imagesy($original_res))) {
    imagedestroy($original_res);
    return TRUE;
  }
  else {

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