]> git.mxchange.org Git - friendica.git/commitdiff
Fix PR 14591 - improve blurhash creation
authorMichael <heluecht@pirati.ca>
Mon, 9 Dec 2024 21:24:28 +0000 (21:24 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 9 Dec 2024 22:23:01 +0000 (22:23 +0000)
src/Contact/Avatar.php
src/Model/Contact.php
src/Model/Photo.php
src/Model/Post/Link.php
src/Object/Image.php
src/Util/Images.php

index 66930119e891223a58be0551427e1a8f3e910769..922b84ea6f72c8e70d8eb5545b728e0888251e7b 100644 (file)
@@ -83,7 +83,7 @@ class Avatar
                $filename  = self::getFilename($contact['url']);
                $timestamp = time();
 
-               $fields['blurhash'] = $image->getBlurHash();
+               $fields['blurhash'] = $image->getBlurHash($img_str);
 
                $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL, $timestamp);
                $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB, $timestamp);
index 4f449a1fa345eda1e306c5bf2da3f3bb36571e17..122422a82022b67ff18881b5470d7b5cbc75e8ae 100644 (file)
@@ -2320,7 +2320,7 @@ class Contact
                                                if ($fetchResult->isSuccess() && !empty($img_str)) {
                                                        $image = new Image($img_str, $fetchResult->getContentType(), $avatar);
                                                        if ($image->isValid()) {
-                                                               $update_fields['blurhash'] = $image->getBlurHash();
+                                                               $update_fields['blurhash'] = $image->getBlurHash($img_str);
                                                        } else {
                                                                return;
                                                        }
index e2d38241d88abbcd97e9f0027b36db876b9830f7..3a37bc50d1ddb97c41ef0378fec48a6a76553989 100644 (file)
@@ -434,6 +434,7 @@ class Photo
                $data        = '';
                $backend_ref = '';
                $storage     = '';
+               $img_str     = $image->asString();
 
                try {
                        if (DBA::isResult($existing_photo)) {
@@ -442,9 +443,9 @@ class Photo
                        } else {
                                $storage = DI::storage();
                        }
-                       $backend_ref = $storage->put($image->asString(), $backend_ref);
+                       $backend_ref = $storage->put($img_str, $backend_ref);
                } catch (InvalidClassStorageException $storageException) {
-                       $data = $image->asString();
+                       $data = $img_str;
                }
 
                $fields = [
@@ -452,7 +453,7 @@ class Photo
                        'contact-id' => $cid,
                        'guid' => $guid,
                        'resource-id' => $rid,
-                       'hash' => md5($image->asString()),
+                       'hash' => md5($img_str),
                        'created' => $created,
                        'edited' => DateTimeFormat::utcNow(),
                        'filename' => basename($filename),
@@ -460,8 +461,8 @@ class Photo
                        'album' => $album,
                        'height' => $image->getHeight(),
                        'width' => $image->getWidth(),
-                       'datasize' => strlen($image->asString()),
-                       'blurhash' => $image->getBlurHash(),
+                       'datasize' => strlen($img_str),
+                       'blurhash' => $image->getBlurHash($img_str),
                        'data' => $data,
                        'scale' => $scale,
                        'photo-type' => $type,
index 021e8a84b71a60260fb6d642bed78a4eda1b738f..93a53c85451f79704682667c59b88314db4266aa 100644 (file)
@@ -130,12 +130,12 @@ class Link
 
                if (Images::isSupportedMimeType($fields['mimetype'])) {
                        $img_str = $curlResult->getBodyString();
-                       $image = new Image($img_str, $fields['mimetype'], $url);
+                       $image = new Image($img_str, $fields['mimetype'], $url, false);
                        if ($image->isValid()) {
                                $fields['mimetype'] = $image->getType();
                                $fields['width']    = $image->getWidth();
                                $fields['height']   = $image->getHeight();
-                               $fields['blurhash'] = $image->getBlurHash();
+                               $fields['blurhash'] = $image->getBlurHash($img_str);
                        }
                }
 
index 26fc5bac6f6492927ae14f780ed08da6ccc97a52..08ebe4f458d7e91236a085b1943e52b4f4965fcc 100644 (file)
@@ -105,7 +105,8 @@ class Image
         * @param string $data
         * @return boolean
         */
-       private function isAnimatedWebP(string $data) {
+       private function isAnimatedWebP(string $data)
+       {
                $header_format = 'A4Riff/I1Filesize/A4Webp/A4Vp/A74Chunk';
                $header = @unpack($header_format, $data);
 
@@ -356,7 +357,6 @@ class Image
                } else {
                        return false;
                }
-
        }
 
        /**
@@ -526,7 +526,7 @@ class Image
                $width = $this->getWidth();
                $height = $this->getHeight();
 
-               if ((!$width)|| (!$height)) {
+               if ((!$width) || (!$height)) {
                        return false;
                }
 
@@ -733,7 +733,7 @@ class Image
                        }
                }
 
-               $stream = fopen('php://memory','r+');
+               $stream = fopen('php://memory', 'r+');
 
                switch ($this->getImageType()) {
                        case IMAGETYPE_PNG:
@@ -768,9 +768,9 @@ class Image
         * @param string $img_str
         * @return string
         */
-       public function getBlurHash(): string
+       public function getBlurHash(string $img_str = ''): string
        {
-               $image = clone($this);
+               $image = new Image($img_str ?: $this->asString(), $this->getType(), $this->filename, false);
                if (empty($image) || !$this->isValid()) {
                        return '';
                }
index 675f034b9c07f2dbb6f1201c8276826a3f19f19c..75d5d4d19e710f67bbaff9505fe096ea11ce989c 100644 (file)
@@ -366,11 +366,11 @@ class Images
                        return [];
                }
 
-               $image = new Image($img_str, '', $url);
+               $image = new Image($img_str, '', $url, false);
 
                if ($image->isValid()) {
-                       $data['blurhash'] = $image->getBlurHash();
-                       
+                       $data['blurhash'] = $image->getBlurHash($img_str);
+
                        if ($ocr) {
                                $media = ['img_str' => $img_str];
                                Hook::callAll('ocr-detection', $media);
@@ -454,7 +454,7 @@ class Images
        {
                return self::getBBCodeByUrl(
                        DI::baseUrl() . '/photos/' . $nickname . '/image/' . $resource_id,
-                       DI::baseUrl() . '/photo/' . $resource_id . '-' . $preview. $ext,
+                       DI::baseUrl() . '/photo/' . $resource_id . '-' . $preview . $ext,
                        $description
                );
        }