Browse Source

IIIF Presentation API Validator (#166)

* Add a manifest label, and width/height of image per https://iiif.io/api/presentation/validator/service/

* try fetching image metadata from IIIF before using Drupal's settings

* Move $mime_type into scope
pull/729/head
Joe Corall 5 years ago committed by Jared Whiklo
parent
commit
d16e49748a
  1. 96
      modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php

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

@ -9,6 +9,10 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\File\FileSystem;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
/** /**
* Provide serializer format for IIIF Manifest. * Provide serializer format for IIIF Manifest.
@ -62,15 +66,24 @@ class IIIFManifest extends StylePluginBase {
*/ */
protected $iiifConfig; protected $iiifConfig;
/**
* The Drupal Filesystem.
*
* @var \Drupal\Core\File\FileSystem
*/
protected $fileSystem;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer, Request $request, ImmutableConfig $iiif_config) { public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer, Request $request, ImmutableConfig $iiif_config, FileSystem $file_system, Client $http_client) {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->serializer = $serializer; $this->serializer = $serializer;
$this->request = $request; $this->request = $request;
$this->iiifConfig = $iiif_config; $this->iiifConfig = $iiif_config;
$this->fileSystem = $file_system;
$this->httpClient = $http_client;
} }
/** /**
@ -83,7 +96,9 @@ class IIIFManifest extends StylePluginBase {
$plugin_definition, $plugin_definition,
$container->get('serializer'), $container->get('serializer'),
$container->get('request_stack')->getCurrentRequest(), $container->get('request_stack')->getCurrentRequest(),
$container->get('config.factory')->get('islandora_iiif.settings') $container->get('config.factory')->get('islandora_iiif.settings'),
$container->get('file_system'),
$container->get('http_client')
); );
} }
@ -105,12 +120,16 @@ class IIIFManifest extends StylePluginBase {
$json += [ $json += [
'@type' => 'sc:Manifest', '@type' => 'sc:Manifest',
'@id' => $request_url, '@id' => $request_url,
// If the View has a title, set the View title as the manifest label.
'label' => $this->view->getTitle() ?: 'IIIF Manifest',
'@context' => 'http://iiif.io/api/presentation/2/context.json', '@context' => 'http://iiif.io/api/presentation/2/context.json',
// @see https://iiif.io/api/presentation/2.1/#sequence // @see https://iiif.io/api/presentation/2.1/#sequence
'sequences' => [ 'sequences' => [
'@context' => 'http://iiif.io/api/presentation/2/context.json', [
'@id' => $iiif_base_id . '/sequence/normal', '@context' => 'http://iiif.io/api/presentation/2/context.json',
'@type' => 'sc:Sequence', '@id' => $iiif_base_id . '/sequence/normal',
'@type' => 'sc:Sequence',
],
], ],
]; ];
// For each row in the View result. // For each row in the View result.
@ -157,32 +176,73 @@ class IIIFManifest extends StylePluginBase {
// Create the IIIF URL for this file // Create the IIIF URL for this file
// Visiting $iiif_url will resolve to the info.json for the image. // Visiting $iiif_url will resolve to the info.json for the image.
$file_url = $image->entity->url(); $file_url = $image->entity->url();
$mime_type = $image->entity->getMimeType();
$iiif_url = rtrim($iiif_address, '/') . '/' . urlencode($file_url); $iiif_url = rtrim($iiif_address, '/') . '/' . urlencode($file_url);
// Create the necessary ID's for the canvas and annotation. // Create the necessary ID's for the canvas and annotation.
$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();
// Try to fetch the IIIF metadata for the image.
try {
$info_json = $this->httpClient->get($iiif_url)->getBody();
$resource = json_decode($info_json, TRUE);
$width = $resource['width'];
$height = $resource['height'];
}
catch (ClientException $e) {
}
catch (ServerException $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];
}
}
}
$canvases[] = [ $canvases[] = [
// @see https://iiif.io/api/presentation/2.1/#canvas // @see https://iiif.io/api/presentation/2.1/#canvas
'@id' => $canvas_id, '@id' => $canvas_id,
'@type' => 'sc:Canvas', '@type' => 'sc:Canvas',
'label' => $entity->label(), 'label' => $image->entity->label(),
'height' => $height,
'width' => $width,
// @see https://iiif.io/api/presentation/2.1/#image-resources // @see https://iiif.io/api/presentation/2.1/#image-resources
'images' => [[ 'images' => [
'@id' => $annotation_id, [
"@type" => "oa:Annotation", '@id' => $annotation_id,
'motivation' => 'sc:painting', "@type" => "oa:Annotation",
'resource' => [ 'motivation' => 'sc:painting',
'@id' => $iiif_url . '/full/full/0/default.jpg', 'resource' => [
'service' => [ '@id' => $iiif_url . '/full/full/0/default.jpg',
'@id' => $iiif_url, "@type" => "dctypes:Image",
'@context' => 'http://iiif.io/api/image/2/context.json', 'format' => $mime_type,
'profile' => 'http://iiif.io/api/image/2/profiles/level2.json', 'height' => $height,
'width' => $width,
'service' => [
'@id' => $iiif_url,
'@context' => 'http://iiif.io/api/image/2/context.json',
'profile' => 'http://iiif.io/api/image/2/profiles/level2.json',
],
], ],
'on' => $canvas_id,
], ],
'on' => $canvas_id,
],
], ],
]; ];
} }

Loading…
Cancel
Save