diff --git a/application/src/Api/Representation/AbstractRepresentation.php b/application/src/Api/Representation/AbstractRepresentation.php index e0b22e7d5..d9246d331 100644 --- a/application/src/Api/Representation/AbstractRepresentation.php +++ b/application/src/Api/Representation/AbstractRepresentation.php @@ -97,7 +97,7 @@ public function primaryMedia() /** * Get one thumbnail of this representation. * - * @return Asset + * @return AssetRepresentation */ public function thumbnail() { diff --git a/application/src/Api/Representation/MediaRepresentation.php b/application/src/Api/Representation/MediaRepresentation.php index c47280dbd..063df55d2 100644 --- a/application/src/Api/Representation/MediaRepresentation.php +++ b/application/src/Api/Representation/MediaRepresentation.php @@ -60,48 +60,24 @@ public function originalUrl() * * @param string $type The type of thumbnail * @return string + * + * @uses \Omeka\File\ThumbnailManager::thumbnailUrl() */ public function thumbnailUrl($type) { - $thumbnailManager = $this->getServiceLocator()->get('Omeka\File\ThumbnailManager'); - - if (!$this->hasThumbnails() || !$thumbnailManager->typeExists($type)) { - $fallbacks = $thumbnailManager->getFallbacks(); - $mediaType = $this->mediaType(); - $topLevelType = strstr((string) $mediaType, '/', true); - - if (isset($fallbacks[$mediaType])) { - // Prioritize a match against the full media type, e.g. "image/jpeg" - $fallback = $fallbacks[$mediaType]; - } elseif ($topLevelType && isset($fallbacks[$topLevelType])) { - // Then fall back on a match against the top-level type, e.g. "image" - $fallback = $fallbacks[$topLevelType]; - } else { - $fallback = $thumbnailManager->getDefaultFallback(); - } - - $assetUrl = $this->getServiceLocator()->get('ViewHelperManager')->get('assetUrl'); - return $assetUrl($fallback[0], $fallback[1], true, false, true); - } - return $this->getFileUrl($type, $this->storageId(), 'jpg'); + return $this->getServiceLocator()->get('Omeka\File\ThumbnailManager')->thumbnailUrl($this, $type); } /** * Get all thumbnail URLs, keyed by type. * * @return array + * + * @uses \Omeka\File\ThumbnailManager::thumbnailUrls() */ public function thumbnailUrls() { - if (!$this->hasThumbnails()) { - return []; - } - $thumbnailManager = $this->getServiceLocator()->get('Omeka\File\ThumbnailManager'); - $urls = []; - foreach ($thumbnailManager->getTypes() as $type) { - $urls[$type] = $this->thumbnailUrl($type); - } - return $urls; + return $this->getServiceLocator()->get('Omeka\File\ThumbnailManager')->thumbnailUrls($this); } /** diff --git a/application/src/File/ThumbnailManager.php b/application/src/File/ThumbnailManager.php index ebf2869bb..f1c23e587 100644 --- a/application/src/File/ThumbnailManager.php +++ b/application/src/File/ThumbnailManager.php @@ -2,6 +2,7 @@ namespace Omeka\File; use Laminas\ServiceManager\ServiceLocatorInterface; +use Omeka\Api\Representation\MediaRepresentation; class ThumbnailManager { @@ -26,6 +27,55 @@ public function __construct(ServiceLocatorInterface $services) $this->config = $config['thumbnails']; } + /** + * Get the URL to a thumbnail image of a media. + * + * @param MediaRepresentation $media + * @param string $type The type of thumbnail + * @return string + */ + public function thumbnailUrl(MediaRepresentation $media, $type) + { + if (!$media->hasThumbnails() || !$this->typeExists($type)) { + $fallbacks = $this->getFallbacks(); + $mediaType = $media->mediaType(); + $topLevelType = strtok((string) $mediaType, '/'); + + if (isset($fallbacks[$mediaType])) { + // Prioritize a match against the full media type, e.g. "image/jpeg" + $fallback = $fallbacks[$mediaType]; + } elseif ($topLevelType && isset($fallbacks[$topLevelType])) { + // Then fall back on a match against the top-level type, e.g. "image" + $fallback = $fallbacks[$topLevelType]; + } else { + $fallback = $this->getDefaultFallback(); + } + + $assetUrl = $this->getServiceLocator()->get('ViewHelperManager')->get('assetUrl'); + return $assetUrl($fallback[0], $fallback[1], true, false, true); + } + + return $media->getFileUrl($type, $media->storageId(), 'jpg'); + } + + /** + * Get all thumbnail URLs of a media, keyed by type. + * + * @param MediaRepresentation $media + * @return array + */ + public function thumbnailUrls(MediaRepresentation $media) + { + if (!$media->hasThumbnails()) { + return []; + } + $urls = []; + foreach ($this->getTypes() as $type) { + $urls[$type] = $this->thumbnailUrl($media, $type); + } + return $urls; + } + /** * Build a thumbnailer object. *