]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Images.php
Changes:
[friendica.git] / src / Util / Images.php
index eb1e8c4375a491ca9f2ca7107082e52194cd5199..e5b8afbb545cb24fda69f574468d07ffb75dda80 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2024, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Util;
 
+use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\DI;
 use Friendica\Model\Photo;
 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
+use Friendica\Object\Image;
 
 /**
  * Image utilities
@@ -180,10 +182,11 @@ class Images
         * Gets info array from given URL, cached data has priority
         *
         * @param string $url
+        * @param bool   $ocr
         * @return array Info
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function getInfoFromURLCached(string $url): array
+       public static function getInfoFromURLCached(string $url, bool $ocr = false): array
        {
                $data = [];
 
@@ -191,12 +194,12 @@ class Images
                        return $data;
                }
 
-               $cacheKey = 'getInfoFromURL:' . sha1($url);
+               $cacheKey = 'getInfoFromURL:' . sha1($url . $ocr);
 
                $data = DI::cache()->get($cacheKey);
 
                if (empty($data) || !is_array($data)) {
-                       $data = self::getInfoFromURL($url);
+                       $data = self::getInfoFromURL($url, $ocr);
 
                        DI::cache()->set($cacheKey, $data);
                }
@@ -208,10 +211,11 @@ class Images
         * Gets info from URL uncached
         *
         * @param string $url
+        * @param bool   $ocr
         * @return array Info array
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function getInfoFromURL(string $url): array
+       public static function getInfoFromURL(string $url, bool $ocr = false): array
        {
                $data = [];
 
@@ -228,7 +232,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) {
@@ -243,11 +252,27 @@ class Images
                        return [];
                }
 
-               if ($data) {
-                       $data['size'] = $filesize;
+               if (!$data) {
+                       return [];
+               }
+
+               $image = new Image($img_str);
+
+               if ($image->isValid()) {
+                       $data['blurhash'] = $image->getBlurHash();
+                       
+                       if ($ocr) {
+                               $media = ['img_str' => $img_str];
+                               Hook::callAll('ocr-detection', $media);
+                               if (!empty($media['description'])) {
+                                       $data['description'] = $media['description'];
+                               }
+                       }
                }
 
-               return is_array($data) ? $data : [];
+               $data['size'] = $filesize;
+
+               return $data;
        }
 
        /**
@@ -304,4 +329,51 @@ 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]';
+       }
+
+       /**
+        * Get the maximum possible upload size in bytes
+        *
+        * @return integer
+        */
+       public static function getMaxUploadBytes(): int
+       {
+               $upload_size = ini_get('upload_max_filesize') ?: DI::config()->get('system', 'maximagesize');
+               return Strings::getBytesFromShorthand($upload_size);
+       }
 }