]> git.mxchange.org Git - friendica.git/commitdiff
Added image grid generation to addVisualAttachments
authorMarek Bachmann <marek.bachmann@comtec.eecs.uni-kassel.de>
Sat, 10 Dec 2022 21:54:50 +0000 (22:54 +0100)
committerMarek Bachmann <marek.bachmann@comtec.eecs.uni-kassel.de>
Sat, 10 Dec 2022 21:54:50 +0000 (22:54 +0100)
src/Model/Item.php
view/templates/content/image_grid.tpl

index 5f7c64a13a9a8495bc4c1d4f231322f7feaf1024..0c31d437ff0b3ddbd52db31070fbbd480bfed9d2 100644 (file)
@@ -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) {
index 1a8648d189faf407a6a871e1bacf62c13d481e6c..8f5cbabcae54894895e3e92feb6537bbb4403065 100644 (file)
@@ -2,17 +2,13 @@
 
 <div id="row" class="row">
        <div class="column">
-        {{foreach $columns.fc as $fc}}
-               {{foreach $fc as $img}}
+        {{foreach $columns.fc as $img}}
                        {{include file="content/image.tpl" image=$img}}
-                       {{/foreach}}
                {{/foreach}}
        </div>
        <div class="column">
-        {{foreach $columns.sc as $sc}}
-                       {{foreach $sc as $img}}
+        {{foreach $columns.sc as $img}}
                                {{include file="content/image.tpl" image=$img}}
-                       {{/foreach}}
                {{/foreach}}
        </div>
 </div>
\ No newline at end of file