You are here

public function ImagemagickExecManager::execute in ImageMagick 8.2

Same name and namespace in other branches
  1. 8.3 src/ImagemagickExecManager.php \Drupal\imagemagick\ImagemagickExecManager::execute()

Executes the convert executable as shell command.

Parameters

string $command: The executable to run.

\Drupal\imagemagick\ImagemagickExecArguments $arguments: An ImageMagick execution arguments object.

string &$output: (optional) A variable to assign the shell stdout to, passed by reference.

string &$error: (optional) A variable to assign the shell stderr to, passed by reference.

string $path: (optional) A custom file path to the executable binary.

Return value

bool TRUE if the command succeeded, FALSE otherwise. The error exit status code integer returned by the executable is logged.

Overrides ImagemagickExecManagerInterface::execute

File

src/ImagemagickExecManager.php, line 232

Class

ImagemagickExecManager
Manage execution of ImageMagick/GraphicsMagick commands.

Namespace

Drupal\imagemagick

Code

public function execute($command, ImagemagickExecArguments $arguments, &$output = NULL, &$error = NULL, $path = NULL) {
  switch ($command) {
    case 'convert':
      $binary = $this
        ->getPackage() === 'imagemagick' ? 'convert' : 'gm';
      break;
    case 'identify':
      $binary = $this
        ->getPackage() === 'imagemagick' ? 'identify' : 'gm';
      break;
  }
  $cmd = $this
    ->getExecutable($binary, $path);
  if ($source_path = $arguments
    ->getSourceLocalPath()) {
    if (($source_frames = $arguments
      ->getSourceFrames()) !== NULL) {
      $source_path .= $source_frames;
    }
    $source_path = $this
      ->escapeShellArg($source_path);
  }
  if ($destination_path = $arguments
    ->getDestinationLocalPath()) {
    $destination_path = $this
      ->escapeShellArg($destination_path);

    // If the format of the derivative image has to be changed, concatenate
    // the new image format and the destination path, delimited by a colon.
    // @see http://www.imagemagick.org/script/command-line-processing.php#output
    if (($format = $arguments
      ->getDestinationFormat()) !== '') {
      $destination_path = $format . ':' . $destination_path;
    }
  }
  switch ($command) {
    case 'identify':
      switch ($this
        ->getPackage()) {
        case 'imagemagick':

          // @codingStandardsIgnoreStart
          // ImageMagick syntax:
          // identify [arguments] source
          // @codingStandardsIgnoreEnd
          $cmdline = $arguments
            ->toString(ImagemagickExecArguments::PRE_SOURCE);

          // @todo BC layer. In 8.x-3.0, remove adding post source path
          // arguments.
          if (($post = $arguments
            ->toString(ImagemagickExecArguments::POST_SOURCE)) !== '') {
            $cmdline .= ' ' . $post;
          }
          $cmdline .= ' ' . $source_path;
          break;
        case 'graphicsmagick':

          // @codingStandardsIgnoreStart
          // GraphicsMagick syntax:
          // gm identify [arguments] source
          // @codingStandardsIgnoreEnd
          $cmdline = 'identify ' . $arguments
            ->toString(ImagemagickExecArguments::PRE_SOURCE);

          // @todo BC layer. In 8.x-3.0, remove adding post source path
          // arguments.
          if (($post = $arguments
            ->toString(ImagemagickExecArguments::POST_SOURCE)) !== '') {
            $cmdline .= ' ' . $post;
          }
          $cmdline .= ' ' . $source_path;
          break;
      }
      break;
    case 'convert':
      switch ($this
        ->getPackage()) {
        case 'imagemagick':

          // @codingStandardsIgnoreStart
          // ImageMagick syntax:
          // convert input [arguments] output
          // @see http://www.imagemagick.org/Usage/basics/#cmdline
          // @codingStandardsIgnoreEnd
          $cmdline = '';
          if (($pre = $arguments
            ->toString(ImagemagickExecArguments::PRE_SOURCE)) !== '') {
            $cmdline .= $pre . ' ';
          }
          $cmdline .= $source_path . ' ' . $arguments
            ->toString(ImagemagickExecArguments::POST_SOURCE) . ' ' . $destination_path;
          break;
        case 'graphicsmagick':

          // @codingStandardsIgnoreStart
          // GraphicsMagick syntax:
          // gm convert [arguments] input output
          // @see http://www.graphicsmagick.org/GraphicsMagick.html
          // @codingStandardsIgnoreEnd
          $cmdline = 'convert ';
          if (($pre = $arguments
            ->toString(ImagemagickExecArguments::PRE_SOURCE)) !== '') {
            $cmdline .= $pre . ' ';
          }
          $cmdline .= $arguments
            ->toString(ImagemagickExecArguments::POST_SOURCE) . ' ' . $source_path . ' ' . $destination_path;
          break;
      }
      break;
  }
  $return_code = $this
    ->runOsShell($cmd, $cmdline, $this
    ->getPackage(), $output, $error);
  if ($return_code !== FALSE) {

    // If the executable returned a non-zero code, log to the watchdog.
    if ($return_code != 0) {
      if ($error === '') {

        // If there is no error message, and allowed in config, log a
        // warning.
        if ($this->configFactory
          ->get('imagemagick.settings')
          ->get('log_warnings') === TRUE) {
          $this->logger
            ->warning("@suite returned with code @code [command: @command @cmdline]", [
            '@suite' => $this
              ->getPackageLabel(),
            '@code' => $return_code,
            '@command' => $cmd,
            '@cmdline' => $cmdline,
          ]);
        }
      }
      else {

        // Log $error with context information.
        $this->logger
          ->error("@suite error @code: @error [command: @command @cmdline]", [
          '@suite' => $this
            ->getPackageLabel(),
          '@code' => $return_code,
          '@error' => $error,
          '@command' => $cmd,
          '@cmdline' => $cmdline,
        ]);
      }

      // Executable exited with an error code, return FALSE.
      return FALSE;
    }

    // The shell command was executed successfully.
    return TRUE;
  }

  // The shell command could not be executed.
  return FALSE;
}