1
1
Fork 0

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);
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) {

View 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>