You are here

function _imagemagick_convert_exec in ImageMagick 7

Executes the ImageMagick convert executable as shell command.

Parameters

$command_args: A string containing arguments to pass to the convert command, which must have been passed through escapeshellarg() already.

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

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

$convert_path: (optional) A custom file path to the convert binary. Internal use only.

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.
2 calls to _imagemagick_convert_exec()
_imagemagick_check_path in ./imagemagick.module
Verifies file path of ImageMagick convert binary by checking its version.
_imagemagick_convert in ./imagemagick.module
Calls the convert executable with the specified filter.

File

./imagemagick.module, line 443
Provides ImageMagick integration.

Code

function _imagemagick_convert_exec($command_args, &$output = NULL, &$error = NULL, $convert_path = NULL) {

  // $convert_path is only passed from the system-wide image toolkit form, on
  // which the path to convert is configured.
  // @see _imagemagick_check_path()
  if (!isset($convert_path)) {

    // By using a default of NULL, we force users to setup the toolkit through
    // the image toolkit administration UI. Sites enforcing a path via
    // settings.php should know what they are doing.
    $convert_path = variable_get('imagemagick_convert', NULL);
    if (!isset($convert_path)) {
      return FALSE;
    }
  }

  // Use Drupal's root as working directory to resolve relative paths correctly.
  $drupal_path = DRUPAL_ROOT;
  if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {

    // 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.
    $convert_path = 'start "ImageMagick" /D ' . escapeshellarg($drupal_path) . ' /B ' . escapeshellarg($convert_path);
  }
  $command = $convert_path . ' ' . $command_args;
  $descriptors = array(
    // stdin
    0 => array(
      'pipe',
      'r',
    ),
    // stdout
    1 => array(
      'pipe',
      'w',
    ),
    // stderr
    2 => array(
      'pipe',
      'w',
    ),
  );
  if ($h = proc_open($command, $descriptors, $pipes, $drupal_path)) {
    $output = '';
    while (!feof($pipes[1])) {
      $output .= fgets($pipes[1]);
    }
    $error = '';
    while (!feof($pipes[2])) {
      $error .= fgets($pipes[2]);
    }
    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    $return_code = proc_close($h);

    // Display debugging information to authorized users.
    if (variable_get('imagemagick_debug', FALSE) && user_access('administer site configuration')) {
      debug($command, t('ImageMagick command'), TRUE);
      if ($output !== '') {
        debug($output, t('ImageMagick output'), TRUE);
      }
      if ($error !== '') {
        debug($error, t('ImageMagick error'), TRUE);
      }
    }

    // If ImageMagick returned a non-zero code, trigger a PHP error that will
    // be caught by Drupal's error handler, logged to the watchdog and
    // eventually displayed to the user if configured to do so.
    if ($return_code != 0) {

      // If there is no error message, clarify this.
      if ($error === '') {
        $error = t('No error message.');
      }

      // Format $error with as full message, passed by reference.
      $error = t('ImageMagick error @code: !error', array(
        '@code' => $return_code,
        '!error' => $error,
      ));

      // @todo Use watchdog() instead? Would hide errors from users during
      //   normal operation, regeardless of error_level setting.
      trigger_error($error, E_USER_ERROR);

      // ImageMagick 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;
}