View source
<?php
namespace Drupal\imagemagick\Plugin\FileMetadata;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\file_mdm\FileMetadataException;
use Drupal\file_mdm\Plugin\FileMetadata\FileMetadataPluginBase;
use Drupal\imagemagick\Event\ImagemagickExecutionEvent;
use Drupal\imagemagick\ImagemagickExecArguments;
use Drupal\imagemagick\ImagemagickExecManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ImagemagickIdentify extends FileMetadataPluginBase {
protected $eventDispatcher;
protected $execManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, CacheBackendInterface $cache_service, ConfigFactoryInterface $config_factory, ImagemagickExecManagerInterface $exec_manager, EventDispatcherInterface $dispatcher, StreamWrapperManagerInterface $stream_wrapper_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $cache_service, $config_factory, $stream_wrapper_manager);
$this->execManager = $exec_manager;
$this->eventDispatcher = $dispatcher;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('cache.file_mdm'), $container
->get('config.factory'), $container
->get('imagemagick.exec_manager'), $container
->get('event_dispatcher'), $container
->get('stream_wrapper_manager'));
}
public function getSupportedKeys($options = NULL) {
return [
'format',
'width',
'height',
'colorspace',
'profiles',
'exif_orientation',
'source_local_path',
'frames_count',
];
}
protected function doGetMetadataFromFile() {
$data = $this
->identify();
return !empty($data) ? $data : NULL;
}
protected function validateKey($key, $method) {
if (!is_string($key)) {
throw new FileMetadataException("Invalid metadata key specified", $this
->getPluginId(), $method);
}
if (!in_array($key, $this
->getSupportedKeys(), TRUE)) {
throw new FileMetadataException("Invalid metadata key '{$key}' specified", $this
->getPluginId(), $method);
}
return TRUE;
}
protected function doGetMetadata($key = NULL) {
if ($key === NULL) {
return $this->metadata;
}
else {
$this
->validateKey($key, __FUNCTION__);
switch ($key) {
case 'source_local_path':
return isset($this->metadata['source_local_path']) ? $this->metadata['source_local_path'] : NULL;
case 'frames_count':
return isset($this->metadata['frames']) ? count($this->metadata['frames']) : 0;
default:
return isset($this->metadata['frames'][0][$key]) ? $this->metadata['frames'][0][$key] : NULL;
}
}
}
protected function doSetMetadata($key, $value) {
$this
->validateKey($key, __FUNCTION__);
switch ($key) {
case 'source_local_path':
$this->metadata['source_local_path'] = $value;
return TRUE;
case 'frames_count':
return FALSE;
default:
$this->metadata['frames'][0][$key] = $value;
return TRUE;
}
}
protected function doRemoveMetadata($key) {
$this
->validateKey($key, __FUNCTION__);
switch ($key) {
case 'source_local_path':
if (isset($this->metadata['source_local_path'])) {
unset($this->metadata['source_local_path']);
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
protected function getMetadataToCache() {
$metadata = $this->metadata;
unset($metadata['source_local_path']);
return $metadata;
}
protected function identify() : array {
$arguments = new ImagemagickExecArguments($this->execManager);
$arguments
->setSource($this
->getLocalTempPath());
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;
}
$command = 'identify';
$this->eventDispatcher
->dispatch(ImagemagickExecutionEvent::ENSURE_SOURCE_LOCAL_PATH, new ImagemagickExecutionEvent($arguments));
$this->eventDispatcher
->dispatch(ImagemagickExecutionEvent::PRE_IDENTIFY_EXECUTE, new ImagemagickExecutionEvent($arguments));
$output = NULL;
$ret = $this->execManager
->execute($command, $arguments, $output);
$data = [];
if ($ret) {
$output = str_replace("\r", '', $output);
$frames = [];
$frames_tmp = explode("\n", $output);
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;
$data['source_local_path'] = $arguments
->getSourceLocalPath();
}
return $data;
}
}