You are here

protected function ImagemagickEventSubscriber::doEnsureSourceLocalPath in ImageMagick 8.2

Same name and namespace in other branches
  1. 8.3 src/EventSubscriber/ImagemagickEventSubscriber.php \Drupal\imagemagick\EventSubscriber\ImagemagickEventSubscriber::doEnsureSourceLocalPath()

Ensures source image URI to a local filesystem path.

Parameters

\Drupal\imagemagick\ImagemagickExecArguments $arguments: The ImageMagick/GraphicsMagick execution arguments object.

1 call to ImagemagickEventSubscriber::doEnsureSourceLocalPath()
ImagemagickEventSubscriber::ensureSourceLocalPath in src/EventSubscriber/ImagemagickEventSubscriber.php
Reacts to an image being parsed.

File

src/EventSubscriber/ImagemagickEventSubscriber.php, line 80

Class

ImagemagickEventSubscriber
Imagemagick's module Event Subscriber.

Namespace

Drupal\imagemagick\EventSubscriber

Code

protected function doEnsureSourceLocalPath(ImagemagickExecArguments $arguments) {

  // Early return if already set.
  if (!empty($arguments
    ->getSourceLocalPath())) {
    return;
  }
  $source = $arguments
    ->getSource();
  if (!file_valid_uri($source)) {

    // The value of $source is likely a file path already.
    $arguments
      ->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.
    $path = $this->fileSystem
      ->realpath($source);
    if ($path) {
      $arguments
        ->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 = $this->fileSystem
        ->tempnam('temporary://', 'imagemagick_');
      $this->fileSystem
        ->unlink($temp_path);
      $temp_path .= '.' . pathinfo($source, PATHINFO_EXTENSION);
      $path = file_unmanaged_copy($arguments
        ->getSource(), $temp_path, FILE_EXISTS_ERROR);
      $arguments
        ->setSourceLocalPath($this->fileSystem
        ->realpath($path));
      drupal_register_shutdown_function([
        static::class,
        'removeTemporaryRemoteCopy',
      ], $arguments
        ->getSourceLocalPath());
    }
  }
}