You are here

protected function ImagemagickIdentify::identify in ImageMagick 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/FileMetadata/ImagemagickIdentify.php \Drupal\imagemagick\Plugin\FileMetadata\ImagemagickIdentify::identify()

Calls the identify executable on the specified file.

Return value

array The array with identify metadata, if the file was parsed correctly. NULL otherwise.

1 call to ImagemagickIdentify::identify()
ImagemagickIdentify::doGetMetadataFromFile in src/Plugin/FileMetadata/ImagemagickIdentify.php

File

src/Plugin/FileMetadata/ImagemagickIdentify.php, line 229

Class

ImagemagickIdentify
FileMetadata plugin for ImageMagick's identify results.

Namespace

Drupal\imagemagick\Plugin\FileMetadata

Code

protected function identify() {
  $arguments = new ImagemagickExecArguments($this->execManager);

  // Add source file.
  $arguments
    ->setSource($this
    ->getLocalTempPath());

  // Prepare the -format argument according to the graphics package in use.
  switch ($this->execManager
    ->getPackage()) {
    case 'imagemagick':
      $arguments
        ->add('-format ' . $arguments
        ->escape("format:%[magick]|width:%[width]|height:%[height]|colorspace:%[colorspace]|profiles:%[profiles]|exif_orientation:%[EXIF:Orientation]\\n"), ImagemagickExecArguments::PRE_SOURCE);
      break;
    case 'graphicsmagick':
      $arguments
        ->add('-format ' . $arguments
        ->escape("format:%m|width:%w|height:%h|exif_orientation:%[EXIF:Orientation]\\n"), ImagemagickExecArguments::PRE_SOURCE);
      break;
  }

  // Allow modules to alter source file and the command line parameters.
  $command = 'identify';
  $this->moduleHandler
    ->alterDeprecated('Deprecated in 8.x-2.5, will be removed in 8.x-3.0. Use an event subscriber to react on a ImagemagickExecutionEvent::ENSURE_SOURCE_LOCAL_PATH event. See https://www.drupal.org/project/imagemagick/issues/3043136.', 'imagemagick_pre_parse_file', $arguments);
  $this->moduleHandler
    ->alterDeprecated('Deprecated in 8.x-2.5, will be removed in 8.x-3.0. Use an event subscriber to react on a ImagemagickExecutionEvent::PRE_IDENTIFY_EXECUTE or a ImagemagickExecutionEvent::PRE_CONVERT_EXECUTE event. See https://www.drupal.org/project/imagemagick/issues/3043136.', 'imagemagick_arguments', $arguments, $command);
  $this->eventDispatcher
    ->dispatch(ImagemagickExecutionEvent::ENSURE_SOURCE_LOCAL_PATH, new ImagemagickExecutionEvent($arguments));
  $this->eventDispatcher
    ->dispatch(ImagemagickExecutionEvent::PRE_IDENTIFY_EXECUTE, new ImagemagickExecutionEvent($arguments));

  // Execute the 'identify' command.
  $output = NULL;
  $ret = $this->execManager
    ->execute($command, $arguments, $output);

  // Process results.
  $data = [];
  if ($ret) {

    // Remove any CR character (GraphicsMagick on Windows produces such).
    $output = str_replace("\r", '', $output);

    // Builds the frames info.
    $frames = [];
    $frames_tmp = explode("\n", $output);

    // Remove empty items at the end of the array.
    while (empty($frames_tmp[count($frames_tmp) - 1])) {
      array_pop($frames_tmp);
    }
    foreach ($frames_tmp as $i => $frame) {
      $info = explode('|', $frame);
      foreach ($info as $item) {
        list($key, $value) = explode(':', $item);
        if (trim($key) === 'profiles') {
          $profiles_tmp = empty($value) ? [] : explode(',', $value);
          $frames[$i][trim($key)] = $profiles_tmp;
        }
        else {
          $frames[$i][trim($key)] = trim($value);
        }
      }
    }
    $data['frames'] = $frames;

    // Adds the local file path that was resolved via
    // event subscriber implementations.
    $data['source_local_path'] = $arguments
      ->getSourceLocalPath();
  }
  return $data;
}