You are here

protected function Convert::execute in Imagick 8

Overrides ImagickOperationTrait::execute

File

src/Plugin/ImageToolkit/Operation/imagick/Convert.php, line 48

Class

Convert
Defines imagick convert operation.

Namespace

Drupal\imagick\Plugin\ImageToolkit\Operation\imagick

Code

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

  /* @var $resource Imagick */
  $resource = $this
    ->getToolkit()
    ->getResource();
  $format = strtoupper($arguments['format']);
  $quality = $arguments['quality'];

  // Set a white background color when converting to JPG because this file
  // format does not support transparency
  if (in_array($format, [
    'JPEG',
    'JPG',
    'JPE',
  ])) {
    $background = new Imagick();
    $background
      ->newImage($resource
      ->getImageWidth(), $resource
      ->getImageHeight(), 'white');
    $resource
      ->compositeImage($background, Imagick::COMPOSITE_DSTOVER, 0, 0);
  }
  $formatSuccess = $resource
    ->setImageFormat($format);
  $qualitySuccess = $resource
    ->setImageProperty('quality', (int) $quality);
  return $formatSuccess && $qualitySuccess;
}