You are here

function _imageapi_imagemagick_convert_exec in ImageAPI 6

Same name and namespace in other branches
  1. 5 imageapi_imagemagick.module \_imageapi_imagemagick_convert_exec()
2 calls to _imageapi_imagemagick_convert_exec()
_imageapi_imagemagick_build_version in ./imageapi_imagemagick.module
_imageapi_imagemagick_convert in ./imageapi_imagemagick.module
Calls the convert executable with the specified filter.

File

./imageapi_imagemagick.module, line 174
ImageMagick toolkit functions

Code

function _imageapi_imagemagick_convert_exec($command_args, &$output, &$errors) {
  $convert_path = variable_get('imageapi_imagemagick_convert', '/usr/bin/convert');
  if ($errors = _imageapi_imagemagick_check_path($convert_path)) {
    watchdog('imageapi imagemagick', '!errors', array(
      '!errors' => implode('<br />', $errors),
    ), WATCHDOG_ERROR);
    return FALSE;
  }

  // Specify Drupal's root as the working a working directory so that relative
  // paths are interpreted correctly.
  $drupal_path = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME']);
  if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {

    // Use Window's start command to avoid the "black window" from showing up:
    // 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 "window title" /D' . escapeshellarg($drupal_path) . ' /B ' . escapeshellarg($convert_path);
  }
  $descriptors = array(
    0 => array(
      'pipe',
      'r',
    ),
    // stdin
    1 => array(
      'pipe',
      'w',
    ),
    // stdout
    2 => array(
      'pipe',
      'w',
    ),
  );
  if ($h = proc_open($convert_path . ' ' . $command_args, $descriptors, $pipes, $drupal_path)) {
    $output = '';
    while (!feof($pipes[1])) {
      $output .= fgets($pipes[1]);
    }
    $errors = '';
    while (!feof($pipes[2])) {
      $errors .= 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('imageapi_imagemagick_debugging', FALSE) && user_access('administer site configuration')) {
      drupal_set_message(t('ImageMagick command: @command', array(
        '@command' => $convert_path . ' ' . $command_args,
      )));
      drupal_set_message(t('ImageMagick output: @output', array(
        '@output' => $output,
      )));
    }
    if ($return_code != 0) {

      // 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 $errors is empty, only report the error code.
      if (empty($errors)) {
        trigger_error(t('ImageMagick reported error code !code.', array(
          '!code' => $return_code,
        )), E_USER_ERROR);
      }
      else {
        trigger_error(t("ImageMagick reported error code !code.\nMessage:\n!error", array(
          '!code' => $return_code,
          '!error' => $errors,
        )), E_USER_ERROR);
      }
    }
    return $return_code;
  }
  return FALSE;
}