Change parameter to PostMedias in Item::makeImageGrid

- Add dimension rescaling when updating the preview URL
This commit is contained in:
Hypolite Petovan 2023-09-29 00:36:06 -04:00
parent 5004471770
commit 3333d4af88
3 changed files with 89 additions and 13 deletions

View file

@ -23,6 +23,7 @@ namespace Friendica\Content\Post\Entity;
use Friendica\BaseEntity; use Friendica\BaseEntity;
use Friendica\Network\Entity\MimeType; use Friendica\Network\Entity\MimeType;
use Friendica\Util\Images;
use Friendica\Util\Proxy; use Friendica\Util\Proxy;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
@ -186,4 +187,80 @@ class PostMedia extends BaseEntity
$this->id; $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,
);
}
} }

View file

@ -3286,11 +3286,11 @@ class Item
} }
/** /**
* @param array $images * @param PostMedias $images
* @return string * @return string
* @throws \Friendica\Network\HTTPException\ServiceUnavailableException * @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) // Image for first column (fc) and second column (sc)
$images_fc = []; $images_fc = [];
@ -3431,7 +3431,7 @@ class Item
DI::profiler()->startRecording('rendering'); DI::profiler()->startRecording('rendering');
$leading = ''; $leading = '';
$trailing = ''; $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. // @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) { foreach ($PostMedias as $PostMedia) {
@ -3440,10 +3440,13 @@ class Item
} }
if ($PostMedia->mimetype->type == 'image') { 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) { } 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 { } else {
$preview_size = 0;
$preview_url = ''; $preview_url = '';
} }
@ -3487,11 +3490,7 @@ class Item
continue; continue;
} }
$images[] = [ $images[] = $PostMedia->withUrl(new Uri($src_url))->withPreview(new Uri($preview_url), $preview_size);
'src' => $src_url,
'preview' => $preview_url,
'attachment' => $PostMedia,
];
} }
} }

View file

@ -1,5 +1,5 @@
{{if $image.preview}} {{if $image->preview}}
<a data-fancybox="{{$image.attachment->uriId}}" href="{{$image.attachment->url}}"><img src="{{$image.preview}}" alt="{{$image.attachment->description}}" title="{{$image.attachment->description}}" loading="lazy"></a> <a data-fancybox="{{$image->uriId}}" href="{{$image->url}}"><img src="{{$image->preview}}" alt="{{$image->description}}" title="{{$image->description}}" loading="lazy"></a>
{{else}} {{else}}
<img src="{{$image.src}}" alt="{{$image.attachment->description}}" title="{{$image.attachment->description}}" loading="lazy"> <img src="{{$image->url}}" alt="{{$image->description}}" title="{{$image->description}}" loading="lazy">
{{/if}} {{/if}}