You are here

public function ImagemagickEventSubscriber::preConvertExecute in ImageMagick 8.3

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/ImagemagickEventSubscriber.php \Drupal\imagemagick\EventSubscriber\ImagemagickEventSubscriber::preConvertExecute()

Fires before the 'convert' command is executed.

It allows to change file paths for source and destination image files, and/or to alter the command line arguments that are passed to the binaries. The toolkit provides methods to prepend, add, find, get and reset arguments that have already been set by image effects.

In addition to arguments that are passed to the binaries command line for execution, it is possible to push arguments to be used only by the toolkit or the event subscribers. You can add/get/find such arguments by specifying ImagemagickExecArguments::INTERNAL as the argument $mode in the methods.

ImageMagick automatically converts the target image to the format denoted by the file extension. However, since changing the file extension is not always an option, you can specify an alternative image format via $arguments->setDestinationFormat('format'), where 'format' is a string denoting an Imagemagick supported format, or via $arguments->setDestinationFormatFromExtension('extension'), where 'extension' is a string denoting an image file extension.

When the destination format is set, it is passed to ImageMagick's convert binary with the syntax "[format]:[destination]".

Parameters

\Drupal\imagemagick\Event\ImagemagickExecutionEvent $event: Imagemagick execution event.

See also

http://www.imagemagick.org/script/command-line-processing.php#output

http://www.imagemagick.org/Usage/files/#save

\Drupal\imagemagick\ImagemagickExecArguments

\Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit::convert()

File

src/EventSubscriber/ImagemagickEventSubscriber.php, line 310

Class

ImagemagickEventSubscriber
Imagemagick's module Event Subscriber.

Namespace

Drupal\imagemagick\EventSubscriber

Code

public function preConvertExecute(ImagemagickExecutionEvent $event) {
  $arguments = $event
    ->getExecArguments();
  $this
    ->prependArguments($arguments);
  $this
    ->doEnsureDestinationLocalPath($arguments);

  // Coalesce Animated GIFs, if required.
  if (empty($arguments
    ->find('/^\\-coalesce/')) && (bool) $this->imagemagickSettings
    ->get('advanced.coalesce') && in_array($arguments
    ->getSourceFormat(), [
    'GIF',
    'GIF87',
  ])) {
    $file_md = $this->fileMetadataManager
      ->uri($arguments
      ->getSource());
    if ($file_md && $file_md
      ->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID, 'frames_count') > 1) {
      $arguments
        ->add("-coalesce", ImagemagickExecArguments::POST_SOURCE, 0);
    }
  }

  // Change output image resolution to 72 ppi, if specified in settings.
  if (empty($arguments
    ->find('/^\\-density/')) && ($density = (int) $this->imagemagickSettings
    ->get('advanced.density'))) {
    $arguments
      ->add("-density {$density} -units PixelsPerInch");
  }

  // Apply color profile.
  if ($profile = $this->imagemagickSettings
    ->get('advanced.profile')) {
    if (file_exists($profile)) {
      $arguments
        ->add('-profile ' . $arguments
        ->escape($profile));
    }
  }
  elseif ($colorspace = $this->imagemagickSettings
    ->get('advanced.colorspace')) {

    // Do not hi-jack settings made by effects.
    if (empty($arguments
      ->find('/^\\-colorspace/'))) {
      $arguments
        ->add('-colorspace ' . $arguments
        ->escape($colorspace));
    }
  }

  // Change image quality.
  if (empty($arguments
    ->find('/^\\-quality/'))) {
    $arguments
      ->add('-quality ' . $this->imagemagickSettings
      ->get('quality'));
  }
}