From d786f225eec56dbef7dacdc65af0b65ed4643163 Mon Sep 17 00:00:00 2001 From: Marek Bachmann Date: Sat, 10 Dec 2022 22:54:50 +0100 Subject: [PATCH] Added image grid generation to addVisualAttachments --- src/Model/Item.php | 73 +++++++++++++++++++++------ view/templates/content/image_grid.tpl | 8 +-- 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 5f7c64a13..0c31d437f 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3130,18 +3130,56 @@ class Item $s = HTML::applyContentFilter($s, $filter_reasons); - if (count($attachments['visual']) > 1) { - // make imgae grid only for multiple images - $s = self::cutAttachedImages($s); - $grid = self::make_image_grid($item, $attachments); - $s .= $grid; - } +// if (count($attachments['visual']) > 1) { +// // make imgae grid only for multiple images +// $s = self::cutAttachedImages($s); +// $grid = self::make_image_grid($item, $attachments); +// $s .= $grid; +// } $hook_data = ['item' => $item, 'html' => $s]; Hook::callAll('prepare_body_final', $hook_data); return $hook_data['html']; } + /** + * @param array $images + * @return string + * @throws \Friendica\Network\HTTPException\ServiceUnavailableException + */ + public static function makeImageGrid(array $images): string + { + $img_tags_landscape = array(); + $img_tags_portrait = array(); + foreach ($images as $image) { + ($image['attachment']['width'] > $image['attachment']['height']) ? ($img_tags_landscape[] = $image) : ($img_tags_portrait[] = $image); + } + + // @todo add some fany ai to divide images equally on both columns + $img_tags_fc = array(); + $img_tags_sc = array(); + if (count($img_tags_landscape) == 0) { + // only portrait + for ($i = 0; $i < count($img_tags_portrait); $i++) { + ($i % 2 == 0) ? ($img_tags_fc[] = $img_tags_portrait[$i]) : ($img_tags_sc[] = $img_tags_portrait[$i]); + } + } + if (count($img_tags_portrait) == 0) { + // ony landscapes + for ($i = 0; $i < count($img_tags_landscape); $i++) { + ($i % 2 == 0) ? ($img_tags_fc[] = $img_tags_landscape[$i]) : ($img_tags_sc[] = $img_tags_landscape[$i]); + } + } + + $media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image_grid.tpl'), [ + 'columns' => [ + 'fc' => $img_tags_fc, + 'sc' => $img_tags_sc, + ], + ]); + return $media; + } + /** * This function removes images at the very end of a post based on the assumption that this images are interpreted * as attachments @@ -3396,16 +3434,21 @@ class Item } } - foreach ($images as $image) { - $media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image.tpl'), [ - '$image' => $image, + $media = ''; + if (count($images) > 1) { + $media = self::makeImageGrid($images); + } + elseif (count($images) == 1) { + $media = $media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image.tpl'), [ + '$image' => $images[0], ]); - // On Diaspora posts the attached pictures are leading - if ($item['network'] == Protocol::DIASPORA) { - $leading .= $media; - } else { - $trailing .= $media; - } + } + + // On Diaspora posts the attached pictures are leading + if ($item['network'] == Protocol::DIASPORA) { + $leading .= $media; + } else { + $trailing .= $media; } if ($shared) { diff --git a/view/templates/content/image_grid.tpl b/view/templates/content/image_grid.tpl index 1a8648d18..8f5cbabca 100644 --- a/view/templates/content/image_grid.tpl +++ b/view/templates/content/image_grid.tpl @@ -2,17 +2,13 @@
- {{foreach $columns.fc as $fc}} - {{foreach $fc as $img}} + {{foreach $columns.fc as $img}} {{include file="content/image.tpl" image=$img}} - {{/foreach}} {{/foreach}}
- {{foreach $columns.sc as $sc}} - {{foreach $sc as $img}} + {{foreach $columns.sc as $img}} {{include file="content/image.tpl" image=$img}} - {{/foreach}} {{/foreach}}
\ No newline at end of file