Skip to content

Commit

Permalink
Islandora IIIF: Get image dimensions from field on media if they exist.:
Browse files Browse the repository at this point in the history
  • Loading branch information
alxp committed Oct 18, 2023
1 parent 21d4682 commit 565a1b4
Showing 1 changed file with 51 additions and 28 deletions.
79 changes: 51 additions & 28 deletions modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\iiif_presentation_api\Encoder\V3\IiifP;
use Drupal\islandora\IslandoraUtils;
use Drupal\islandora_iiif\IiiffInfo;
use Drupal\islandora_iiif\IiifInfo;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\views\ResultRow;
use GuzzleHttp\Client;
Expand Down Expand Up @@ -68,6 +71,13 @@ class IIIFManifest extends StylePluginBase {
*/
protected $serializer;

/**
* The IIIF Info service.
*
* @var IiifInfo
*/
protected $iiifInfo;

/**
* The request service.
*
Expand Down Expand Up @@ -113,7 +123,7 @@ class IIIFManifest extends StylePluginBase {
/**
* {@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);

$this->serializer = $serializer;
Expand All @@ -125,6 +135,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
$this->messenger = $messenger;
$this->moduleHandler = $moduleHandler;
$this->utils = $utils;
$this->iiifInfo = $iiif_info;
}

/**
Expand All @@ -143,7 +154,8 @@ public static function create(ContainerInterface $container, array $configuratio
$container->get('http_client'),
$container->get('messenger'),
$container->get('module_handler'),
$container->get('islandora.utils')
$container->get('islandora.utils'),
$container->get('islandora_iiif')
);
}

Expand Down Expand Up @@ -334,38 +346,49 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima

if (isset($image->width) && is_numeric($image->width)
&& isset($image->height) && is_numeric($image->height)) {
return [intval($image->width), intval($image->height)];
return [intval($image->width),
intval($image->height)];
}

try {
$info_json = $this->httpClient->get($iiif_url)->getBody();
$resource = json_decode($info_json, TRUE);
$width = $resource['width'];
$height = $resource['height'];
if ($properties = $image->getProperties()
&& isset($properties['width']) && is_numeric($properties['width'])
&& isset($properties['height']) && is_numeric($properties['width'])) {
return [intval($properties['width']),
intval($properties['height'])];
}
catch (ClientException | ServerException | ConnectException $e) {
// If we couldn't get the info.json from IIIF
// try seeing if we can get it from Drupal.
if (empty($width) || empty($height)) {
// Get the image properties so we know the image width/height.
$properties = $image->getProperties();
$width = isset($properties['width']) ? $properties['width'] : 0;
$height = isset($properties['height']) ? $properties['height'] : 0;

// 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.
if ($mime_type === 'image/tiff' && !$width || !$height) {
$uri = $image->entity->getFileUri();
$path = $this->fileSystem->realpath($uri);
$image_size = getimagesize($path);
if ($image_size) {
$width = $image_size[0];
$height = $image_size[1];
}

$entity = $image->entity;
if ($entity->hasField('field_height') && !$entity->get('field_height')->isEmpty()
&& $entity->get('field_height')->value > 0
&& $entity->hasField('field_width')
&& !$entity->get('field_width')->isEmpty()
&& $entity->get('field_width')->value > 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
// see if we can get the image size via PHP's core function.
$uri = $image->entity->getFileUri();
$path = $this->fileSystem->realpath($uri);
if (!empty($path)) {
$image_size = getimagesize($path);
if ($image_size) {
return [intval($image_size[0]),
intval($image_size[1])];
}
}
}
return [$width, $height];

// 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 [0, 0];
}

/**
Expand Down

0 comments on commit 565a1b4

Please sign in to comment.