You are here

protected function ImagemagickToolkit::imagemagickExec in ImageMagick 8

Executes the convert executable as shell command.

Parameters

string $command: The executable to run.

string $command_args: A string containing arguments to pass to the command, which must have been passed through $this->escapeShellArg() already.

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

mixed The return value depends on the shell command result:

  • Boolean TRUE if the command succeeded.
  • Boolean FALSE if the shell process could not be executed.
  • Error exit status code integer returned by the executable.
3 calls to ImagemagickToolkit::imagemagickExec()
ImagemagickToolkit::buildConfigurationForm in src/Plugin/ImageToolkit/ImagemagickToolkit.php
Form constructor.
ImagemagickToolkit::convert in src/Plugin/ImageToolkit/ImagemagickToolkit.php
Calls the convert executable with the specified arguments.
ImagemagickToolkit::identify in src/Plugin/ImageToolkit/ImagemagickToolkit.php
Calls the identify executable on the specified file.

File

src/Plugin/ImageToolkit/ImagemagickToolkit.php, line 1135

Class

ImagemagickToolkit
Provides ImageMagick integration toolkit for image manipulation.

Namespace

Drupal\imagemagick\Plugin\ImageToolkit

Code

protected function imagemagickExec($command, &$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 = $this
    ->getSourceLocalPath()) {
    $source_path = $this
      ->escapeShellArg($source_path);
  }
  if ($destination_path = $this
    ->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 = $this
      ->getDestinationFormat()) !== '') {
      $destination_path = $format . ':' . $destination_path;
    }
  }
  switch ($command) {
    case 'identify':
      switch ($this
        ->getPackage()) {
        case 'imagemagick':

          // ImageMagick syntax:
          // identify [arguments] source
          $cmdline = implode(' ', $this
            ->getArguments()) . ' ' . $source_path;
          break;
        case 'graphicsmagick':

          // GraphicsMagick syntax:
          // gm identify [arguments] source
          $cmdline = 'identify ' . implode(' ', $this
            ->getArguments()) . ' ' . $source_path;
          break;
      }
      break;
    case 'convert':
      switch ($this
        ->getPackage()) {
        case 'imagemagick':

          // ImageMagick syntax:
          // convert input [arguments] output
          // @see http://www.imagemagick.org/Usage/basics/#cmdline
          $cmdline = $source_path . ' ' . implode(' ', $this
            ->getArguments()) . ' ' . $destination_path;
          break;
        case 'graphicsmagick':

          // GraphicsMagick syntax:
          // gm convert [arguments] input output
          // @see http://www.graphicsmagick.org/GraphicsMagick.html
          $cmdline = 'convert ' . implode(' ', $this
            ->getArguments()) . ' ' . $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 it.
      return $return_code;
    }

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

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