From 0b66fba803070188bd7f64f8ab5ad44fe3dd44f4 Mon Sep 17 00:00:00 2001 From: dannylamb Date: Fri, 9 Feb 2024 11:45:07 -0400 Subject: [PATCH] Checking for width/height on media first when generating IIIF manifest --- .../src/Plugin/views/style/IIIFManifest.php | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 28dc8e2a..241b4800 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -11,6 +11,7 @@ use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Url; +use Drupal\media\Entity\Media; use Drupal\islandora\IslandoraUtils; use Drupal\islandora_iiif\IiifInfo; use Drupal\views\Plugin\views\style\StylePluginBase; @@ -271,7 +272,7 @@ $this->addSearchEndpoint($json, $url_components); $canvas_id = $iiif_base_id . '/canvas/' . $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, $entity, $image, $mime_type); if ($width == 0) { continue; } @@ -343,8 +344,21 @@ $this->addSearchEndpoint($json, $url_components); * @return [string] * The width and height of the image. */ - protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $image, string $mime_type) { + protected function getCanvasDimensions(string $iiif_url, Media $media, FieldItemInterface $image, string $mime_type) { + + // If the media has field_height and field_width, return those values. + if ($media->hasField('field_height') + && !$media->get('field_height')->isEmpty() + && $media->get('field_height')->value > 0 + && $media->hasField('field_width') + && !$media->get('field_width')->isEmpty() + && $media->get('field_width')->value > 0) { + return [intval($media->get('field_width')->value), + intval($media->get('field_height')->value), + ]; + } + // Otherwise start looking at the field/file level for the numbers. if (isset($image->width) && is_numeric($image->width) && isset($image->height) && is_numeric($image->height)) { return [intval($image->width), @@ -388,11 +402,9 @@ $this->addSearchEndpoint($json, $url_components); // As a last resort, get it from the IIIF server. // This can be very slow and will fail if there are too many pages. - if ($this->options['get_dimensions_from_server']) { - $dimensions = $this->iiifInfo->getImageDimensions($image->entity); - if ($dimensions !== FALSE) { - return $dimensions; - } + $dimensions = $this->iiifInfo->getImageDimensions($image->entity); + if ($dimensions !== FALSE) { + return $dimensions; } return [0, 0]; @@ -565,13 +577,6 @@ $this->addSearchEndpoint($json, $url_components); '#description' => $this->t('Term indicating the media that holds structured text, such as hOCR, for the given object. Use this if the text is on a separate media from the tile source.'), ]; - $form['get_dimensions_from_server'] = [ - '#type' => 'checkbox', - '#title' => $this->t("Retrieve image dimensions from IIIF server"), - '#description' => $this->t("For TIFFs and JP2s, if the media doesn't have width and height values populated, as a last resort, retrieve the info from the IIIF server. This can be very slow and is not recommended."), - '#default_value' => $this->options['get_dimensions_from_server'], - ]; - $form['search_endpoint'] = [ '#type' => 'textfield', '#title' => $this->t("Search endpoint path."),