]> git.mxchange.org Git - friendica.git/commitdiff
Use the blurhash when the remote picture doesn't load
authorMichael <heluecht@pirati.ca>
Sun, 4 Dec 2022 14:58:53 +0000 (14:58 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 4 Dec 2022 14:58:53 +0000 (14:58 +0000)
src/Model/Photo.php
src/Module/Photo.php
src/Object/Image.php

index 213551b1152c247567419a12d7d8834351a6af4e..6dd9212832fe9c830d406fc89b592a727846c9a6 100644 (file)
@@ -346,11 +346,14 @@ class Photo
         * @param string $url      Image URL
         * @param int    $uid      User ID of the requesting person
         * @param string $mimetype Image mime type. Is guessed by file name when empty.
+        * @param string $blurhash The blurhash that will be used to generate a picture when the original picture can't be fetched
+        * @param int    $width    Image width
+        * @param int    $height   Image height
         *
         * @return array
         * @throws \Exception
         */
-       public static function createPhotoForExternalResource(string $url, int $uid = 0, string $mimetype = ''): array
+       public static function createPhotoForExternalResource(string $url, int $uid = 0, string $mimetype = '', string $blurhash = '', int $width = 0, int $height = 0): array
        {
                if (empty($mimetype)) {
                        $mimetype = Images::guessTypeByExtension($url);
@@ -364,6 +367,9 @@ class Photo
                $photo['backend-ref']   = json_encode(['url' => $url, 'uid' => $uid]);
                $photo['type']          = $mimetype;
                $photo['cacheable']     = true;
+               $photo['blurhash']      = $blurhash;
+               $photo['width']         = $width;
+               $photo['height']        = $height;
 
                return $photo;
        }
index 1847b4f707e0ebe6432c62a5b134456409ca552b..a4ef6ab414e005ca3a0248544556a1eba8c28ca9 100644 (file)
@@ -153,8 +153,12 @@ class Photo extends BaseModule
                $stamp = microtime(true);
 
                $imgdata = MPhoto::getImageDataForPhoto($photo);
-               if (empty($imgdata)) {
+               if (empty($imgdata) && empty($photo['blurhash'])) {
                        throw new HTTPException\NotFoundException();
+               } elseif (empty($imgdata) && !empty($photo['blurhash'])) {
+                       $image = New Image('', 'image/png');
+                       $image->getFromBlurHash($photo['blurhash'], $photo['width'], $photo['height']);
+                       $imgdata = $image->asString();
                }
 
                // The mimetype for an external or system resource can only be known reliably after it had been fetched
@@ -240,14 +244,18 @@ class Photo extends BaseModule
        {
                switch($type) {
                        case 'preview':
-                               $media = DBA::selectFirst('post-media', ['preview', 'url', 'mimetype', 'type', 'uri-id'], ['id' => $id]);
+                               $media = DBA::selectFirst('post-media', ['preview', 'url', 'preview-height', 'preview-width', 'height', 'width', 'mimetype', 'type', 'uri-id', 'blurhash'], ['id' => $id]);
                                if (empty($media)) {
                                        return false;
                                }
-                               $url = $media['preview'];
+                               $url    = $media['preview'];
+                               $width  = $media['preview-width'];
+                               $height = $media['preview-height'];
 
                                if (empty($url) && ($media['type'] == Post\Media::IMAGE)) {
-                                       $url = $media['url'];
+                                       $url    = $media['url'];
+                                       $width  = $media['width'];
+                                       $height = $media['height'];
                                }
 
                                if (empty($url)) {
@@ -258,9 +266,9 @@ class Photo extends BaseModule
                                        return MPhoto::getPhoto($matches[1], $matches[2]);
                                }
 
-                               return MPhoto::createPhotoForExternalResource($url, (int)DI::userSession()->getLocalUserId(), $media['mimetype'] ?? '');
+                               return MPhoto::createPhotoForExternalResource($url, (int)DI::userSession()->getLocalUserId(), $media['mimetype'] ?? '', $media['blurhash'], $width, $height);
                        case 'media':
-                               $media = DBA::selectFirst('post-media', ['url', 'mimetype', 'uri-id'], ['id' => $id, 'type' => Post\Media::IMAGE]);
+                               $media = DBA::selectFirst('post-media', ['url', 'height', 'width', 'mimetype', 'uri-id', 'blurhash'], ['id' => $id, 'type' => Post\Media::IMAGE]);
                                if (empty($media)) {
                                        return false;
                                }
@@ -269,7 +277,7 @@ class Photo extends BaseModule
                                        return MPhoto::getPhoto($matches[1], $matches[2]);
                                }
 
-                               return MPhoto::createPhotoForExternalResource($media['url'], (int)DI::userSession()->getLocalUserId(), $media['mimetype']);
+                               return MPhoto::createPhotoForExternalResource($media['url'], (int)DI::userSession()->getLocalUserId(), $media['mimetype'], $media['blurhash'], $media['width'], $media['height']);
                        case 'link':
                                $link = DBA::selectFirst('post-link', ['url', 'mimetype'], ['id' => $id]);
                                if (empty($link)) {
index c2cfa9e2f1e8fd260edcbd565daad010b72bf7a3..87401304db8af895ee8d46b36a1096c5d1004b79 100644 (file)
@@ -780,13 +780,21 @@ class Image
                        return;
                }
 
-               $pixels = Blurhash::decode($blurhash, $width, $height);
-               $this->image  = imagecreatetruecolor($width, $height);
-               for ($y = 0; $y < $height; ++$y) {
-                       for ($x = 0; $x < $width; ++$x) {
+               $scaled = Images::getScalingDimensions($width, $height, 90);
+               $pixels = Blurhash::decode($blurhash, $scaled['width'], $scaled['height']);
+
+               $this->image = imagecreatetruecolor($scaled['width'], $scaled['height']);
+               for ($y = 0; $y < $scaled['height']; ++$y) {
+                       for ($x = 0; $x < $scaled['width']; ++$x) {
                                [$r, $g, $b] = $pixels[$y][$x];
                                imagesetpixel($this->image, $x, $y, imagecolorallocate($this->image, $r, $g, $b));
                        }
                }
+
+               $this->width  = imagesx($this->image);
+               $this->height = imagesy($this->image);
+               $this->valid  = true;
+
+               $this->scaleUp(min($width, $height));
        }
 }