You are here

protected function ImagemagickToolkit::parseFileViaIdentify in ImageMagick 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/ImageToolkit/ImagemagickToolkit.php \Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit::parseFileViaIdentify()

Parses the image file using the 'identify' executable.

Return value

bool TRUE if the file could be found and is an image, FALSE otherwise.

1 call to ImagemagickToolkit::parseFileViaIdentify()
ImagemagickToolkit::parseFile in src/Plugin/ImageToolkit/ImagemagickToolkit.php
Determines if a file contains a valid image.

File

src/Plugin/ImageToolkit/ImagemagickToolkit.php, line 984

Class

ImagemagickToolkit
Provides ImageMagick integration toolkit for image manipulation.

Namespace

Drupal\imagemagick\Plugin\ImageToolkit

Code

protected function parseFileViaIdentify() {

  // Prepare the -format argument according to the graphics package in use.
  switch ($this
    ->getPackage()) {
    case 'imagemagick':
      $this
        ->addArgument('-format ' . $this
        ->escapeShellArg("format:%[magick]|width:%[width]|height:%[height]|exif_orientation:%[EXIF:Orientation]\\n"));
      break;
    case 'graphicsmagick':
      $this
        ->addArgument('-format ' . $this
        ->escapeShellArg("format:%m|width:%w|height:%h|exif_orientation:%[EXIF:Orientation]\\n"));
      break;
  }
  if ($identify_output = $this
    ->identify()) {
    $frames = explode("\n", $identify_output);

    // Remove empty items at the end of the array.
    while (empty($frames[count($frames) - 1])) {
      array_pop($frames);
    }

    // If remaining items are more than one, we have a multi-frame image.
    if (count($frames) > 1) {
      $this
        ->setFrames(count($frames));
    }

    // Take information from the first frame.
    $info = explode('|', $frames[0]);
    $data = [];
    foreach ($info as $item) {
      list($key, $value) = explode(':', $item);
      $data[trim($key)] = trim($value);
    }
    $format = isset($data['format']) ? $data['format'] : NULL;
    if ($this->formatMapper
      ->isFormatEnabled($format)) {
      $this
        ->setSourceFormat($format)
        ->setWidth((int) $data['width'])
        ->setHeight((int) $data['height'])
        ->setExifOrientation($data['exif_orientation']);
      return TRUE;
    }
  }
  return FALSE;
}