From d270a98e862624f4d240c849d2a42c6bf57216e2 Mon Sep 17 00:00:00 2001 From: Marek Bachmann Date: Thu, 8 Dec 2022 20:39:51 +0100 Subject: [PATCH] Image grid logic --- src/Model/Item.php | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index e7bf057b4c..1d3e3afbac 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3193,9 +3193,9 @@ class Item { if ($item['has-media']) { if (count($attachments['visual']) > 1) { - $img_tags_fc = array(); - $img_tags_sc = array(); - $count = 0; + + $img_tags_landscape = array(); + $img_tags_portrait = array(); foreach ($attachments['visual'] as $attachment) { $src_url = Post\Media::getUrlForId($attachment['id']); $preview_url = Post\Media::getPreviewUrlForId($attachment['id'], ($attachment['width'] > $attachment['height']) ? Proxy::SIZE_MEDIUM : Proxy::SIZE_LARGE); @@ -3205,22 +3205,34 @@ class Item 'preview' => $preview_url, 'attachment' => $attachment, ]); - // @todo add some fany ai to divide images equally on both columns - if ($count % 2 == 0) { - $img_tags_fc[] = $img_tag; - } else { - $img_tags_sc[] = $img_tag; + ($attachment['width'] > $attachment['height']) ? ($img_tags_landscape[] = $img_tag) : ($img_tags_portrait[] = $img_tag); + } + + $landscapesCount = count($img_tags_landscape); + $portraitsCount = count($img_tags_portrait); + + // @todo add some fany ai to divide images equally on both columns + $img_tags_fc = array(); + $img_tags_sc = array(); + if ($landscapesCount == 0) { + // only portrait + for ($i = 0; $i < $portraitsCount; $i++) { + ($i % 2 == 0) ? ($img_tags_fc[] = $img_tags_portrait[$i]) : ($img_tags_sc[] = $img_tags_portrait[$i]); + } + } + if ($portraitsCount == 0) { + // ony landscapes + for ($i = 0; $i < $landscapesCount; $i++) { + ($i % 2 == 0) ? ($img_tags_fc[] = $img_tags_landscape[$i]) : ($img_tags_sc[] = $img_tags_landscape[$i]); } - ++$count; } - $img_grid = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image_grid.tpl'), [ + return Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image_grid.tpl'), [ 'columns' => [ 'fc' => $img_tags_fc, 'sc' => $img_tags_sc, ], ]); - return $img_grid; } } -- 2.39.5