]> git.mxchange.org Git - friendica.git/commitdiff
Added some logic for image grid column balancing
authorMarek Bachmann <marek.bachmann@comtec.eecs.uni-kassel.de>
Sat, 10 Dec 2022 22:37:03 +0000 (23:37 +0100)
committerMarek Bachmann <marek.bachmann@comtec.eecs.uni-kassel.de>
Sat, 10 Dec 2022 22:37:03 +0000 (23:37 +0100)
src/Model/Item.php

index 165e31d12da79725010ac7ce7b79025eecd01dec..dedf905162ca82cdb48810de3a0e8603c509eaac 100644 (file)
@@ -3151,26 +3151,70 @@ class Item
                // @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_landscape) == 0 || count($img_tags_portrait) == 0) {
+                       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]);
+                       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]);
+                               }
+                       }
+               } else {
+                       // Mix of landscape and portrait images
+                       $domformat = (count($img_tags_landscape) > count($img_tags_portrait)) ? 'landscape' : 'portrait';
+                       if ($domformat == 'landscape') {
+                               // More landscapes than portraits
+                               for ($l = 0; $l < count($img_tags_landscape); $l++) {
+                                       // use two landscapes for on portrait
+                                       if ((count($img_tags_landscape) > $l + 1) && (count($img_tags_portrait) > $l)) {
+                                               // we have one more landscape that can be used for the l-th portrait
+                                               $img_tags_fc[] = $img_tags_landscape[$l];
+                                               $img_tags_fc[] = $img_tags_landscape[$l + 1];
+                                               $img_tags_sc[] = $img_tags_portrait[$l];
+                                               $l++;
+                                       } elseif (count($img_tags_portrait) <= $l) {
+                                               // no more portraits available but landscapes
+                                               $img_tags_fc[] = $img_tags_landscape[$l];
+                                       }
+                               }
                        }
+                       if ($domformat == 'portrait') {
+                               // More  portraits than landscapes
+                               $l = 0;
+                               for ($p = 0; $p < count($img_tags_portrait); $p++) {
+                                       // use two landscapes for on portrait
+                                       if ((count($img_tags_landscape) > $l + 1)) {
+                                               // we have one more landscape that can be used for the p-th portrait
+                                               $img_tags_sc[] = $img_tags_landscape[$l];
+                                               $img_tags_sc[] = $img_tags_landscape[$l + 1];
+                                               $img_tags_fc[] = $img_tags_portrait[$p];
+                                               // used to landscapes:
+                                               $l += 2;
+                                       } else {
+                                               // no more landscapes available
+                                               if ($p % 2 == 0 ) {
+                                                       $img_tags_fc[] = $img_tags_landscape[$l];
+                                               } else {
+                                                       $img_tags_sc[] = $img_tags_landscape[$l];
+                                               }
+
+                                       }
+                               }
+                       }
+
                }
 
-               $media = 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 $media;
        }
 
        /**