You are here

public function ImagemagickExecManager::checkPath in ImageMagick 8.2

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

Verifies file path of the executable binary by checking its version.

Parameters

string $path: The user-submitted file path to the convert binary.

string $package: (optional) The graphics package to use.

Return value

array An associative array containing:

  • output: The shell output of 'convert -version', if any.
  • errors: A list of error messages indicating if the executable could not be found or executed.

Overrides ImagemagickExecManagerInterface::checkPath

File

src/ImagemagickExecManager.php, line 184

Class

ImagemagickExecManager
Manage execution of ImageMagick/GraphicsMagick commands.

Namespace

Drupal\imagemagick

Code

public function checkPath($path, $package = NULL) {
  $status = [
    'output' => '',
    'errors' => [],
  ];

  // Execute gm or convert based on settings.
  $package = $package ?: $this
    ->getPackage();
  $binary = $package === 'imagemagick' ? 'convert' : 'gm';
  $executable = $this
    ->getExecutable($binary, $path);

  // If a path is given, we check whether the binary exists and can be
  // invoked.
  if (!empty($path)) {

    // Check whether the given file exists.
    if (!is_file($executable)) {
      $status['errors'][] = $this
        ->t('The @suite executable %file does not exist.', [
        '@suite' => $this
          ->getPackageLabel($package),
        '%file' => $executable,
      ]);
    }
    elseif (!is_executable($executable)) {
      $status['errors'][] = $this
        ->t('The @suite file %file is not executable.', [
        '@suite' => $this
          ->getPackageLabel($package),
        '%file' => $executable,
      ]);
    }
  }

  // In case of errors, check for open_basedir restrictions.
  if ($status['errors'] && ($open_basedir = ini_get('open_basedir'))) {
    $status['errors'][] = $this
      ->t('The PHP <a href=":php-url">open_basedir</a> security restriction is set to %open-basedir, which may prevent to locate the @suite executable.', [
      '@suite' => $this
        ->getPackageLabel($package),
      '%open-basedir' => $open_basedir,
      ':php-url' => 'http://php.net/manual/en/ini.core.php#ini.open-basedir',
    ]);
  }

  // Unless we had errors so far, try to invoke convert.
  if (!$status['errors']) {
    $error = NULL;
    $this
      ->runOsShell($executable, '-version', $package, $status['output'], $error);
    if ($error !== '') {
      $status['errors'][] = $error;
    }
  }
  return $status;
}