diff --git a/src/Content/Post/Entity/PostMedia.php b/src/Content/Post/Entity/PostMedia.php index deb4ec1b36..e03246315c 100644 --- a/src/Content/Post/Entity/PostMedia.php +++ b/src/Content/Post/Entity/PostMedia.php @@ -23,6 +23,7 @@ namespace Friendica\Content\Post\Entity; use Friendica\BaseEntity; use Friendica\Network\Entity\MimeType; +use Friendica\Util\Images; use Friendica\Util\Proxy; use Psr\Http\Message\UriInterface; @@ -186,4 +187,80 @@ class PostMedia extends BaseEntity $this->id; } + + /** + * Return a new PostMedia entity with a different preview URI and an optional proxy size name. + * The new entity preview's width and height are rescaled according to the provided size. + * + * @param \GuzzleHttp\Psr7\Uri $preview + * @param string $size + * @return $this + */ + public function withPreview(\GuzzleHttp\Psr7\Uri $preview, string $size = ''): self + { + if ($this->width || $this->height) { + $newWidth = $this->width; + $newHeight = $this->height; + } else { + $newWidth = $this->previewWidth; + $newHeight = $this->previewHeight; + } + + if ($newWidth && $newHeight && $size) { + $dimensionts = Images::getScalingDimensions($newWidth, $newHeight, Proxy::getPixelsFromSize($size)); + $newWidth = $dimensionts['width']; + $newHeight = $dimensionts['height']; + } + + return new static( + $this->uriId, + $this->url, + $this->type, + $this->mimetype, + $this->activityUriId, + $this->width, + $this->height, + $this->size, + $preview, + $newWidth, + $newHeight, + $this->description, + $this->name, + $this->authorUrl, + $this->authorName, + $this->authorImage, + $this->publisherUrl, + $this->publisherName, + $this->publisherImage, + $this->blurhash, + $this->id, + ); + } + + public function withUrl(\GuzzleHttp\Psr7\Uri $url): self + { + return new static( + $this->uriId, + $url, + $this->type, + $this->mimetype, + $this->activityUriId, + $this->width, + $this->height, + $this->size, + $this->preview, + $this->previewWidth, + $this->previewHeight, + $this->description, + $this->name, + $this->authorUrl, + $this->authorName, + $this->authorImage, + $this->publisherUrl, + $this->publisherName, + $this->publisherImage, + $this->blurhash, + $this->id, + ); + } } diff --git a/src/Model/Item.php b/src/Model/Item.php index 0293517377..53183f1d2f 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3286,11 +3286,11 @@ class Item } /** - * @param array $images + * @param PostMedias $images * @return string * @throws \Friendica\Network\HTTPException\ServiceUnavailableException */ - private static function makeImageGrid(array $images): string + private static function makeImageGrid(PostMedias $images): string { // Image for first column (fc) and second column (sc) $images_fc = []; @@ -3431,7 +3431,7 @@ class Item DI::profiler()->startRecording('rendering'); $leading = ''; $trailing = ''; - $images = []; + $images = new PostMedias(); // @todo In the future we should make a single for the template engine with all media in it. This allows more flexibilty. foreach ($PostMedias as $PostMedia) { @@ -3440,10 +3440,13 @@ class Item } if ($PostMedia->mimetype->type == 'image') { - $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($PostMedia->width > $PostMedia->height ? Proxy::SIZE_MEDIUM : Proxy::SIZE_LARGE); + $preview_size = $PostMedia->width > $PostMedia->height ? Proxy::SIZE_MEDIUM : Proxy::SIZE_LARGE; + $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($preview_size); } elseif ($PostMedia->preview) { - $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE); + $preview_size = Proxy::SIZE_LARGE; + $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($preview_size); } else { + $preview_size = 0; $preview_url = ''; } @@ -3487,11 +3490,7 @@ class Item continue; } - $images[] = [ - 'src' => $src_url, - 'preview' => $preview_url, - 'attachment' => $PostMedia, - ]; + $images[] = $PostMedia->withUrl(new Uri($src_url))->withPreview(new Uri($preview_url), $preview_size); } } diff --git a/view/templates/content/image.tpl b/view/templates/content/image.tpl index e31326a755..b3dfb74139 100644 --- a/view/templates/content/image.tpl +++ b/view/templates/content/image.tpl @@ -1,5 +1,5 @@ -{{if $image.preview}} -{{$image.attachment->description}} +{{if $image->preview}} +{{$image->description}} {{else}} -{{$image.attachment->description}} +{{$image->description}} {{/if}}