Browse Source

Islandora IIIF: Get image dimensions from field on media if they exist.:

959-hocr-tiff-performance
Alexander O'Neill 1 year ago
parent
commit
565a1b42b9
  1. 65
      modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php

65
modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php

@ -11,7 +11,10 @@ use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\iiif_presentation_api\Encoder\V3\IiifP;
use Drupal\islandora\IslandoraUtils; use Drupal\islandora\IslandoraUtils;
use Drupal\islandora_iiif\IiiffInfo;
use Drupal\islandora_iiif\IiifInfo;
use Drupal\views\Plugin\views\style\StylePluginBase; use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\views\ResultRow; use Drupal\views\ResultRow;
use GuzzleHttp\Client; use GuzzleHttp\Client;
@ -68,6 +71,13 @@ class IIIFManifest extends StylePluginBase {
*/ */
protected $serializer; protected $serializer;
/**
* The IIIF Info service.
*
* @var IiifInfo
*/
protected $iiifInfo;
/** /**
* The request service. * The request service.
* *
@ -113,7 +123,7 @@ class IIIFManifest extends StylePluginBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer, Request $request, ImmutableConfig $iiif_config, EntityTypeManagerInterface $entity_type_manager, FileSystemInterface $file_system, Client $http_client, MessengerInterface $messenger, ModuleHandlerInterface $moduleHandler, IslandoraUtils $utils) { public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer, Request $request, ImmutableConfig $iiif_config, EntityTypeManagerInterface $entity_type_manager, FileSystemInterface $file_system, Client $http_client, MessengerInterface $messenger, ModuleHandlerInterface $moduleHandler, IslandoraUtils $utils, IiifInfo $iiif_info) {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->serializer = $serializer; $this->serializer = $serializer;
@ -125,6 +135,7 @@ class IIIFManifest extends StylePluginBase {
$this->messenger = $messenger; $this->messenger = $messenger;
$this->moduleHandler = $moduleHandler; $this->moduleHandler = $moduleHandler;
$this->utils = $utils; $this->utils = $utils;
$this->iiifInfo = $iiif_info;
} }
/** /**
@ -143,7 +154,8 @@ class IIIFManifest extends StylePluginBase {
$container->get('http_client'), $container->get('http_client'),
$container->get('messenger'), $container->get('messenger'),
$container->get('module_handler'), $container->get('module_handler'),
$container->get('islandora.utils') $container->get('islandora.utils'),
$container->get('islandora_iiif')
); );
} }
@ -334,38 +346,49 @@ class IIIFManifest extends StylePluginBase {
if (isset($image->width) && is_numeric($image->width) if (isset($image->width) && is_numeric($image->width)
&& isset($image->height) && is_numeric($image->height)) { && isset($image->height) && is_numeric($image->height)) {
return [intval($image->width), intval($image->height)]; return [intval($image->width),
intval($image->height)];
} }
try { if ($properties = $image->getProperties()
$info_json = $this->httpClient->get($iiif_url)->getBody(); && isset($properties['width']) && is_numeric($properties['width'])
$resource = json_decode($info_json, TRUE); && isset($properties['height']) && is_numeric($properties['width'])) {
$width = $resource['width']; return [intval($properties['width']),
$height = $resource['height']; intval($properties['height'])];
} }
catch (ClientException | ServerException | ConnectException $e) {
// If we couldn't get the info.json from IIIF $entity = $image->entity;
// try seeing if we can get it from Drupal. if ($entity->hasField('field_height') && !$entity->get('field_height')->isEmpty()
if (empty($width) || empty($height)) { && $entity->get('field_height')->value > 0
// Get the image properties so we know the image width/height. && $entity->hasField('field_width')
$properties = $image->getProperties(); && !$entity->get('field_width')->isEmpty()
$width = isset($properties['width']) ? $properties['width'] : 0; && $entity->get('field_width')->value > 0) {
$height = isset($properties['height']) ? $properties['height'] : 0; return [ $entity->get('field_width')->value,
$entity->get('field_height')->value];
}
if ($mime_type === 'image/tiff') {
// If this is a TIFF AND we don't know the width/height // If this is a TIFF AND we don't know the width/height
// see if we can get the image size via PHP's core function. // see if we can get the image size via PHP's core function.
if ($mime_type === 'image/tiff' && !$width || !$height) {
$uri = $image->entity->getFileUri(); $uri = $image->entity->getFileUri();
$path = $this->fileSystem->realpath($uri); $path = $this->fileSystem->realpath($uri);
if (!empty($path)) {
$image_size = getimagesize($path); $image_size = getimagesize($path);
if ($image_size) { if ($image_size) {
$width = $image_size[0]; return [intval($image_size[0]),
$height = $image_size[1]; intval($image_size[1])];
} }
} }
} }
// As a last resort, get it from the IIIF server.
// This can be very slow and will fail if there are too many pages.
$dimensions = $this->iiifInfo->getImageDimensions($image->entity);
if ($dimensions !== FALSE) {
return $dimensions;
} }
return [$width, $height];
return [0, 0];
} }
/** /**

Loading…
Cancel
Save