From bda6b268968e04617a87f868032e2e758e94a59b Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 12 Jan 2022 12:00:41 +0200 Subject: [PATCH 1/2] Added `Medium::getAlternatives()` to be able to list all the retina sizes --- CHANGELOG.md | 1 + .../Media/Interfaces/MediaObjectInterface.php | 8 ++++++++ .../Common/Media/Traits/MediaObjectTrait.php | 20 ++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) 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). * From 67b9623d6cebaf036623ed08cb0e644ea2cf05c9 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 12 Jan 2022 13:29:18 +0200 Subject: [PATCH 2/2] Numeric sort for sizes: MediaObjectTrait::getAlternatives() --- system/src/Grav/Common/Media/Traits/MediaObjectTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php b/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php index 884b3aa19..0949f1d50 100644 --- a/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php +++ b/system/src/Grav/Common/Media/Traits/MediaObjectTrait.php @@ -146,7 +146,7 @@ trait MediaObjectTrait } } - ksort($alternatives); + ksort($alternatives, SORT_NUMERIC); return $alternatives; }