]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Images.php
Fix: Pagination in search result works again
[friendica.git] / src / Util / Images.php
index 4fa2eda04ad525f8a13600af30d7ddc3db04435e..aed72f065b7c8c0810efa22c0aaa938ca6c7c46c 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -25,6 +25,7 @@ use Friendica\Core\Logger;
 use Friendica\DI;
 use Friendica\Model\Photo;
 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
+use Friendica\Object\Image;
 
 /**
  * Image utilities
@@ -179,7 +180,7 @@ class Images
        /**
         * Gets info array from given URL, cached data has priority
         *
-        * @param string $url URL
+        * @param string $url
         * @return array Info
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
@@ -207,7 +208,7 @@ class Images
        /**
         * Gets info from URL uncached
         *
-        * @param string $url URL
+        * @param string $url
         * @return array Info array
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
@@ -228,7 +229,12 @@ class Images
                }
 
                if (empty($img_str)) {
-                       $img_str = DI::httpClient()->fetch($url, HttpClientAccept::IMAGE, 4);
+                       try {
+                               $img_str = DI::httpClient()->fetch($url, HttpClientAccept::IMAGE, 4);
+                       } catch (\Exception $exception) {
+                               Logger::notice('Image is invalid', ['url' => $url, 'exception' => $exception]);
+                               return [];
+                       }
                }
 
                if (!$img_str) {
@@ -244,6 +250,12 @@ class Images
                }
 
                if ($data) {
+                       $image = new Image($img_str);
+
+                       if ($image->isValid()) {
+                               $data['blurhash'] = $image->getBlurHash();
+                       }
+
                        $data['size'] = $filesize;
                }
 
@@ -304,4 +316,40 @@ class Images
 
                return ['width' => $dest_width, 'height' => $dest_height];
        }
+
+       /**
+        * Get a BBCode tag for an local photo page URL with a preview thumbnail and an image description
+        *
+        * @param string $resource_id
+        * @param string $nickname The local user owner of the resource
+        * @param int    $preview Preview image size identifier, either 0, 1 or 2 in decreasing order of size
+        * @param string $ext Image file extension
+        * @param string $description
+        * @return string
+        */
+       public static function getBBCodeByResource(string $resource_id, string $nickname, int $preview, string $ext, string $description = ''): string
+       {
+               return self::getBBCodeByUrl(
+                       DI::baseUrl() . '/photos/' . $nickname . '/image/' . $resource_id,
+                       DI::baseUrl() . '/photo/' . $resource_id . '-' . $preview. '.' . $ext,
+                       $description
+               );
+       }
+
+       /**
+        * Get a BBCode tag for an image URL with a preview thumbnail and an image description
+        *
+        * @param string $photo Full image URL
+        * @param string $preview Preview image URL
+        * @param string $description
+        * @return string
+        */
+       public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = ''): string
+       {
+               if (!empty($preview)) {
+                       return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]';
+               }
+
+               return '[img=' . $photo . ']' . $description . '[/img]';
+       }
 }