Skip to content

Commit

Permalink
Moved creation of thumbnail urls from MediaRepresentation to Thumbnai…
Browse files Browse the repository at this point in the history
…lManager.
  • Loading branch information
Daniel Berthereau authored and Daniel Berthereau committed Jan 15, 2024
1 parent 3f3937b commit 28e6f08
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function primaryMedia()
/**
* Get one thumbnail of this representation.
*
* @return Asset
* @return AssetRepresentation
*/
public function thumbnail()
{
Expand Down
36 changes: 6 additions & 30 deletions application/src/Api/Representation/MediaRepresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
50 changes: 50 additions & 0 deletions application/src/File/ThumbnailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Omeka\File;

use Laminas\ServiceManager\ServiceLocatorInterface;
use Omeka\Api\Representation\MediaRepresentation;

class ThumbnailManager
{
Expand All @@ -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.
*
Expand Down

0 comments on commit 28e6f08

Please sign in to comment.