diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f522f1ea..3ef81b007 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Added support for `assets.addJsModule()` with full pipeline support * Added `Utils::getExtensionsByMime()` method to get all the registered extensions for the specific mime type * Added `Media::getRoute()` and `Media::getRawRoute()` methods to get page route if available + * Added `Medium::getAlternatives()` to be able to list all the retina sizes 2. [](#improved) * Improved `Utils::download()` method to allow overrides on download name, mime and expires header * Improved `onPageFallBackUrl` event diff --git a/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php b/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php index c46dd38c7..4af3052b0 100644 --- a/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php +++ b/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php @@ -63,6 +63,14 @@ interface MediaObjectInterface extends \Grav\Framework\Media\Interfaces\MediaObj */ public function addAlternative($ratio, MediaObjectInterface $alternative); + /** + * Get list of image alternatives. Includes the current media image as well. + * + * @param bool $withDerived If true, include generated images as well. If false, only return existing files. + * @return array + */ + public function getAlternatives(bool $withDerived = true): array; + /** * Return string representation of the object (html). * diff --git a/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php b/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php index 492b3af85..884b3aa19 100644 --- a/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php +++ b/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php @@ -128,11 +128,29 @@ trait MediaObjectTrait } $alternative->set('ratio', $ratio); - $width = $alternative->get('width'); + $width = $alternative->get('width', 0); $this->alternatives[$width] = $alternative; } + /** + * @param bool $withDerived + * @return array + */ + public function getAlternatives(bool $withDerived = true): array + { + $alternatives = []; + foreach ($this->alternatives + [$this->get('width', 0) => $this] as $size => $alternative) { + if ($withDerived || $alternative->filename === basename($alternative->filepath)) { + $alternatives[$size] = $alternative; + } + } + + ksort($alternatives); + + return $alternatives; + } + /** * Return string representation of the object (html). *