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\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,
);
}
}