Added image grid generation to addVisualAttachments

This commit is contained in:
Marek Bachmann 2022-12-10 22:54:50 +01:00
parent c7811576cc
commit d786f225ee
2 changed files with 60 additions and 21 deletions

View File

@ -3130,18 +3130,56 @@ class Item
$s = HTML::applyContentFilter($s, $filter_reasons); $s = HTML::applyContentFilter($s, $filter_reasons);
if (count($attachments['visual']) > 1) { // if (count($attachments['visual']) > 1) {
// make imgae grid only for multiple images // // make imgae grid only for multiple images
$s = self::cutAttachedImages($s); // $s = self::cutAttachedImages($s);
$grid = self::make_image_grid($item, $attachments); // $grid = self::make_image_grid($item, $attachments);
$s .= $grid; // $s .= $grid;
} // }
$hook_data = ['item' => $item, 'html' => $s]; $hook_data = ['item' => $item, 'html' => $s];
Hook::callAll('prepare_body_final', $hook_data); Hook::callAll('prepare_body_final', $hook_data);
return $hook_data['html']; 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 * This function removes images at the very end of a post based on the assumption that this images are interpreted
* as attachments * as attachments
@ -3396,16 +3434,21 @@ class Item
} }
} }
foreach ($images as $image) { $media = '';
$media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image.tpl'), [ if (count($images) > 1) {
'$image' => $image, $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; // On Diaspora posts the attached pictures are leading
} else { if ($item['network'] == Protocol::DIASPORA) {
$trailing .= $media; $leading .= $media;
} } else {
$trailing .= $media;
} }
if ($shared) { if ($shared) {

View File

@ -2,17 +2,13 @@
<div id="row" class="row"> <div id="row" class="row">
<div class="column"> <div class="column">
{{foreach $columns.fc as $fc}} {{foreach $columns.fc as $img}}
{{foreach $fc as $img}}
{{include file="content/image.tpl" image=$img}} {{include file="content/image.tpl" image=$img}}
{{/foreach}}
{{/foreach}} {{/foreach}}
</div> </div>
<div class="column"> <div class="column">
{{foreach $columns.sc as $sc}} {{foreach $columns.sc as $img}}
{{foreach $sc as $img}}
{{include file="content/image.tpl" image=$img}} {{include file="content/image.tpl" image=$img}}
{{/foreach}}
{{/foreach}} {{/foreach}}
</div> </div>
</div> </div>