You are here

protected function ImagemagickToolkit::runOsShell in ImageMagick 8

Executes a command on the operating system.

Parameters

string $command: The command to run.

string $arguments: The arguments of the command to run.

string $id: An identifier for the process to be spawned on the operating system.

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.

Return value

int|bool The operating system returned code, or FALSE if it was not possible to execute the command.

2 calls to ImagemagickToolkit::runOsShell()
ImagemagickToolkit::checkPath in src/Plugin/ImageToolkit/ImagemagickToolkit.php
Verifies file path of the executable binary by checking its version.
ImagemagickToolkit::imagemagickExec in src/Plugin/ImageToolkit/ImagemagickToolkit.php
Executes the convert executable as shell command.

File

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

Class

ImagemagickToolkit
Provides ImageMagick integration toolkit for image manipulation.

Namespace

Drupal\imagemagick\Plugin\ImageToolkit

Code

protected function runOsShell($command, $arguments, $id, &$output = NULL, &$error = NULL) {
  if ($this->isWindows) {

    // Use Window's start command with the /B flag to make the process run in
    // the background and avoid a shell command line window from showing up.
    // @see http://us3.php.net/manual/en/function.exec.php#56599
    // Use /D to run the command from PHP's current working directory so the
    // file paths don't have to be absolute.
    $command = 'start "' . $id . '" /D ' . $this
      ->escapeShellArg($this->appRoot) . ' /B ' . $this
      ->escapeShellArg($command);
  }
  $command_line = $command . ' ' . $arguments;

  // Executes the command on the OS via proc_open().
  $descriptors = [
    // This is stdin.
    0 => [
      'pipe',
      'r',
    ],
    // This is stdout.
    1 => [
      'pipe',
      'w',
    ],
    // This is stderr.
    2 => [
      'pipe',
      'w',
    ],
  ];
  Timer::start('imagemagick:runOsShell');
  if ($h = proc_open($command_line, $descriptors, $pipes, $this->appRoot)) {
    $output = '';
    while (!feof($pipes[1])) {
      $output .= fgets($pipes[1]);
    }
    $output = utf8_encode($output);
    $error = '';
    while (!feof($pipes[2])) {
      $error .= fgets($pipes[2]);
    }
    $error = utf8_encode($error);
    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    $return_code = proc_close($h);
  }
  else {
    $return_code = FALSE;
  }
  $execution_time = Timer::stop('imagemagick:runOsShell')['time'];

  // Process debugging information if required.
  if ($this->configFactory
    ->get('imagemagick.settings')
    ->get('debug')) {
    $this
      ->debugMessage('@suite command: <pre>@raw</pre> executed in @execution_timems', [
      '@suite' => $this
        ->getPackageLabel($id),
      '@raw' => print_r($command_line, TRUE),
      '@execution_time' => $execution_time,
    ]);
    if ($output !== '') {
      $this
        ->debugMessage('@suite output: <pre>@raw</pre>', [
        '@suite' => $this
          ->getPackageLabel($id),
        '@raw' => print_r($output, TRUE),
      ]);
    }
    if ($error !== '') {
      $this
        ->debugMessage('@suite error @return_code: <pre>@raw</pre>', [
        '@suite' => $this
          ->getPackageLabel($id),
        '@return_code' => $return_code,
        '@raw' => print_r($error, TRUE),
      ]);
    }
  }
  return $return_code;
}