You are here

class SetCanvas in Image Effects 8.3

Same name in this branch
  1. 8.3 src/Plugin/ImageToolkit/Operation/gd/SetCanvas.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\SetCanvas
  2. 8.3 src/Plugin/ImageToolkit/Operation/imagemagick/SetCanvas.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\SetCanvas
Same name and namespace in other branches
  1. 8 src/Plugin/ImageToolkit/Operation/imagemagick/SetCanvas.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\SetCanvas
  2. 8.2 src/Plugin/ImageToolkit/Operation/imagemagick/SetCanvas.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\SetCanvas

Defines ImageMagick set canvas operation.

Plugin annotation


@ImageToolkitOperation(
  id = "image_effects_imagemagick_set_canvas",
  toolkit = "imagemagick",
  operation = "set_canvas",
  label = @Translation("Set canvas"),
  description = @Translation("Lay the image over a colored canvas.")
)

Hierarchy

Expanded class hierarchy of SetCanvas

File

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

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick
View source
class SetCanvas extends ImagemagickImageToolkitOperationBase {
  use ImagemagickOperationTrait;
  use SetCanvasTrait;

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImagemagickOperationTrait::$formatMapper protected property The format mapper service.
ImagemagickOperationTrait::getFormatMapper protected function Returns the format mapper service.
SetCanvas::execute protected function
SetCanvasTrait::arguments protected function
SetCanvasTrait::validateArguments protected function