You are here

protected function SetCanvas::execute in Image Effects 8.2

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

File

src/Plugin/ImageToolkit/Operation/imagemagick/SetCanvas.php, line 27

Class

SetCanvas
Defines ImageMagick set canvas operation.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick

Code

protected function execute(array $arguments) {
  $toolkit_arguments = $this
    ->getToolkit()
    ->arguments();

  // Calculate geometry.
  $geometry = sprintf('%dx%d', $arguments['width'], $arguments['height']);
  if ($arguments['x_pos'] || $arguments['y_pos']) {
    $geometry .= sprintf('%+d%+d', -$arguments['x_pos'], -$arguments['y_pos']);
  }

  // Determine background.
  if ($arguments['canvas_color']) {
    $bg = '-background ' . $this
      ->escapeArgument($arguments['canvas_color']);
  }
  else {
    $format = $toolkit_arguments
      ->getDestinationFormat() ?: $toolkit_arguments
      ->getSourceFormat();
    $mime_type = $this
      ->getFormatMapper()
      ->getMimeTypeFromFormat($format);
    if ($mime_type === 'image/jpeg') {

      // JPEG does not allow transparency. Set to white.
      // @todo allow to be configurable.
      $bg = '-background ' . $this
        ->escapeArgument('#FFFFFF');
    }
    else {
      $bg = '-background transparent';
    }
  }

  // Add argument.
  $this
    ->addArgument("-gravity none {$bg} -compose src-over -extent {$geometry}");

  // Set dimensions.
  $this
    ->getToolkit()
    ->setWidth($arguments['width'])
    ->setHeight($arguments['height']);
  return TRUE;
}