You are here

function imagemagick_imagemagick_pre_parse_file_alter in ImageMagick 8

Implements hook_imagemagick_pre_parse_file_alter().

File

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

Code

function imagemagick_imagemagick_pre_parse_file_alter(ImagemagickToolkit $toolkit) {

  // Convert source image URI to filepath.
  $local_path = $toolkit
    ->getSourceLocalPath();
  if (empty($local_path)) {
    $source = $toolkit
      ->getSource();
    if (!file_valid_uri($source)) {

      // The value of $source is likely a file path already.
      $toolkit
        ->setSourceLocalPath($source);
    }
    else {

      // If we can resolve the realpath of the file, then the file is local and
      // we can assign the actual file path.
      $file_system = \Drupal::service('file_system');
      $path = $file_system
        ->realpath($source);
      if ($path) {
        $toolkit
          ->setSourceLocalPath($path);
      }
      else {

        // We are working with a remote file, copy the remote source file to a
        // temp one and set the local path to it.
        $temp_path = $file_system
          ->tempnam('temporary://', 'imagemagick_');
        $file_system
          ->unlink($temp_path);
        $temp_path .= '.' . pathinfo($source, PATHINFO_EXTENSION);
        $path = file_unmanaged_copy($toolkit
          ->getSource(), $temp_path, FILE_EXISTS_ERROR);
        $toolkit
          ->setSourceLocalPath($file_system
          ->realpath($path));
      }
    }
  }
}