]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Images.php
Fixed max value check, improved request value fetching
[friendica.git] / src / Util / Images.php
index f0aefb9f2c298822a315555e2d4b76a6d8fddbba..077509d3cb5e6e1c64105e2a7886bd131cf31be5 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -22,8 +22,8 @@
 namespace Friendica\Util;
 
 use Friendica\Core\Logger;
-use Friendica\Core\System;
 use Friendica\DI;
+use Friendica\Model\Photo;
 
 /**
  * Image utilities
@@ -46,6 +46,30 @@ class Images
                return $m;
        }
 
+       /**
+        * Return file extension for mime type
+        * @param string $mimetype
+        * @return string
+        */
+       public static function getExtensionByMimeType(string $mimetype): string
+       {
+               switch ($mimetype) {
+                       case 'image/png':
+                               $imagetype = IMAGETYPE_PNG;
+                               break;
+
+                       case 'image/gif':
+                               $imagetype = IMAGETYPE_GIF;
+                               break;
+
+                       default:
+                               $imagetype = IMAGETYPE_JPEG;
+                               break;
+               }
+
+               return image_type_to_extension($imagetype);
+       }
+
        /**
         * Returns supported image mimetypes and corresponding file extensions
         *
@@ -75,25 +99,23 @@ class Images
        /**
         * Fetch image mimetype from the image data or guessing from the file name
         *
-        * @param string   $image_data Image data
-        * @param string   $filename   File name (for guessing the type via the extension)
-        * @param string[] $mimeTypes  possible mime types
+        * @param string $image_data Image data
+        * @param string $filename   File name (for guessing the type via the extension)
+        * @param string $mime       default mime type
         *
         * @return string
         * @throws \Exception
         */
-       public static function getMimeTypeByData(string $image_data, string $filename = '', array $mimeTypes = [])
+       public static function getMimeTypeByData(string $image_data, string $filename = '', string $mime = '')
        {
-               foreach ($mimeTypes as $mimeType) {
-                       if (substr($mimeType, 0, 6) == 'image/') {
-                               Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mimeTypes]);
-                               return $mimeType;
-                       }
+               if (substr($mime, 0, 6) == 'image/') {
+                       Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
+                       return $mime;
                }
 
                $image = @getimagesizefromstring($image_data);
                if (!empty($image['mime'])) {
-                       Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mimeTypes, 'mime' => $image['mime']]);
+                       Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mime, 'mime' => $image['mime']]);
                        return $image['mime'];
                }
 
@@ -186,7 +208,17 @@ class Images
                        return $data;
                }
 
-               $img_str = DI::httpRequest()->fetch($url, true, 4);
+               if (Network::isLocalLink($url) && ($data = Photo::getResourceData($url))) {
+                       $photo = Photo::selectFirst([], ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
+                       if (!empty($photo)) {
+                               $img_str = Photo::getImageDataForPhoto($photo);
+                       }
+                       // @todo Possibly add a check for locally stored files
+               }
+
+               if (empty($img_str)) {
+                       $img_str = DI::httpClient()->fetch($url, 4);
+               }
 
                if (!$img_str) {
                        return [];
@@ -195,18 +227,7 @@ class Images
                $filesize = strlen($img_str);
 
                try {
-                       if (function_exists("getimagesizefromstring")) {
-                               $data = @getimagesizefromstring($img_str);
-                       } else {
-                               $tempfile = tempnam(get_temppath(), "cache");
-
-                               $stamp1 = microtime(true);
-                               file_put_contents($tempfile, $img_str);
-                               DI::profiler()->saveTimestamp($stamp1, "file");
-
-                               $data = getimagesize($tempfile);
-                               unlink($tempfile);
-                       }
+                       $data = @getimagesizefromstring($img_str);
                } catch (\Exception $e) {
                        return [];
                }