Compare commits

...

1 Commits

Author SHA1 Message Date
Alexander O'Neill 42eec19ec0 Issue #959: Improve IIIF manifest generation performance. 1 year ago
  1. 65
      modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php

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

@ -214,7 +214,7 @@ class IIIFManifest extends StylePluginBase {
$canvas_id = $iiif_base_id . '/canvas/' . $entity->id(); $canvas_id = $iiif_base_id . '/canvas/' . $entity->id();
$annotation_id = $iiif_base_id . '/annotation/' . $entity->id(); $annotation_id = $iiif_base_id . '/annotation/' . $entity->id();
[$width, $height] = $this->getCanvasDimensions($iiif_url, $image, $mime_type); [$width, $height] = $this->getCanvasDimensions($iiif_url, $image, $mime_type, $entity);
$tmp_canvas = [ $tmp_canvas = [
// @see https://iiif.io/api/presentation/2.1/#canvas // @see https://iiif.io/api/presentation/2.1/#canvas
@ -272,39 +272,54 @@ class IIIFManifest extends StylePluginBase {
* The image field. * The image field.
* @param string $mime_type * @param string $mime_type
* The mime type of the image. * The mime type of the image.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The image entity.
* *
* @return [string] * @return [string]
* The width and height of the image. * The width and height of the image.
*/ */
protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $image, string $mime_type) { protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $image, string $mime_type, $entity) {
try { if ($entity->hasField('field_height') && !$entity->get('field_height')->isEmpty() && $entity->hasField('field_width') && !$entity->get('field_width')->isEmpty()) {
$info_json = $this->httpClient->get($iiif_url)->getBody(); $width = $entity->get('field_width')->value;
$resource = json_decode($info_json, TRUE); $height = $entity->get('field_height')->value;
$width = $resource['width'];
$height = $resource['height'];
} }
catch (ClientException | ServerException | ConnectException $e) { else {
// If we couldn't get the info.json from IIIF // Try to fetch the IIIF metadata for the image.
// try seeing if we can get it from Drupal. try {
if (empty($width) || empty($height)) { $info_json = $this->httpClient->get($iiif_url)->getBody();
// Get the image properties so we know the image width/height. $resource = json_decode($info_json, TRUE);
$properties = $image->getProperties(); $width = $resource['width'];
$width = isset($properties['width']) ? $properties['width'] : 0; $height = $resource['height'];
$height = isset($properties['height']) ? $properties['height'] : 0; }
catch (ClientException | ServerException | ConnectException $e) {
// If this is a TIFF AND we don't know the width/height // If we couldn't get the info.json from IIIF
// see if we can get the image size via PHP's core function. // try seeing if we can get it from Drupal.
if ($mime_type === 'image/tiff' && !$width || !$height) { if (empty($width) || empty($height)) {
$uri = $image->entity->getFileUri(); // Get the image properties so we know the image width/height.
$path = $this->fileSystem->realpath($uri); $properties = $image->getProperties();
$image_size = getimagesize($path); $width = isset($properties['width']) ? $properties['width'] : 0;
if ($image_size) { $height = isset($properties['height']) ? $properties['height'] : 0;
$width = $image_size[0];
$height = $image_size[1]; // 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];
}
} }
} }
} }
} }
if ($entity->hasField('field_height') && $entity->get('field_height')->isEmpty() && $entity->hasField('field_width') && $entity->get('field_width')->isEmpty()) {
$entity->set('field_width', $width);
$entity->set('field_height', $height);
$entity->save();
}
return [$width, $height]; return [$width, $height];
} }

Loading…
Cancel
Save