You are here

protected function ImagemagickEventSubscriber::doEnsureDestinationLocalPath in ImageMagick 8.2

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

Ensures destination image URI to a local filesystem path.

Parameters

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

1 call to ImagemagickEventSubscriber::doEnsureDestinationLocalPath()
ImagemagickEventSubscriber::preConvertExecute in src/EventSubscriber/ImagemagickEventSubscriber.php
Fires before the 'convert' command is executed.

File

src/EventSubscriber/ImagemagickEventSubscriber.php, line 120

Class

ImagemagickEventSubscriber
Imagemagick's module Event Subscriber.

Namespace

Drupal\imagemagick\EventSubscriber

Code

protected function doEnsureDestinationLocalPath(ImagemagickExecArguments $arguments) {
  $local_path = $arguments
    ->getDestinationLocalPath();

  // Early return if already set.
  if (!empty($local_path)) {
    return;
  }
  $destination = $arguments
    ->getDestination();
  if (!file_valid_uri($destination)) {

    // The value of $destination is likely a file path already.
    $arguments
      ->setDestinationLocalPath($destination);
  }
  else {

    // If we can resolve the realpath of the file, then the file is local
    // and we can assign its real path.
    $path = $this->fileSystem
      ->realpath($destination);
    if ($path) {
      $arguments
        ->setDestinationLocalPath($path);
    }
    else {

      // We are working with a remote file, set the local destination to
      // a temp local file.
      $temp_path = $this->fileSystem
        ->tempnam('temporary://', 'imagemagick_');
      $this->fileSystem
        ->unlink($temp_path);
      $temp_path .= '.' . pathinfo($destination, PATHINFO_EXTENSION);
      $arguments
        ->setDestinationLocalPath($this->fileSystem
        ->realpath($temp_path));
      drupal_register_shutdown_function([
        static::class,
        'removeTemporaryRemoteCopy',
      ], $arguments
        ->getDestinationLocalPath());
    }
  }
}