]> git.mxchange.org Git - friendica.git/commitdiff
Change parameter to PostMedias in Item::makeImageGrid
authorHypolite Petovan <hypolite@mrpetovan.com>
Fri, 29 Sep 2023 04:36:06 +0000 (00:36 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 3 Oct 2023 23:58:51 +0000 (19:58 -0400)
- Add dimension rescaling when updating the preview URL

src/Content/Post/Entity/PostMedia.php
src/Model/Item.php
view/templates/content/image.tpl

index deb4ec1b36dc094019cd571c7beb9f8e6263ec56..e03246315c24506363cd06e7e7829e66a3423b20 100644 (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,
+               );
+       }
 }
index 0293517377e7024239db75ccf1b53a3b2ec052c8..53183f1d2f60179c1fb1b77bbf7bc1335dde08a4 100644 (file)
@@ -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);
                        }
                }
 
index e31326a7556c907bb6ec0a25494d1415d297346e..b3dfb741398653a076acc6c70a039c261c7e4203 100644 (file)
@@ -1,5 +1,5 @@
-{{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>
+{{if $image->preview}}
+<a data-fancybox="{{$image->uriId}}" href="{{$image->url}}"><img src="{{$image->preview}}" alt="{{$image->description}}" title="{{$image->description}}" loading="lazy"></a>
 {{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}}