]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Photo.php
Merge pull request #10551 from annando/profiler
[friendica.git] / src / Model / Photo.php
index f79409e3f5c332f1115002d9b265f0388fbd2813..30e666898777c5b4a9c245d5a31b611b9373b932 100644 (file)
@@ -27,6 +27,7 @@ use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
+use Friendica\Model\Storage\ExternalResource;
 use Friendica\Model\Storage\SystemResource;
 use Friendica\Object\Image;
 use Friendica\Util\DateTimeFormat;
@@ -244,13 +245,17 @@ class Photo
         * Construct a photo array for a system resource image
         *
         * @param string $filename Image file name relative to code root
-        * @param string $mimetype Image mime type. Defaults to "image/jpeg"
+        * @param string $mimetype Image mime type. Is guessed by file name when empty.
         *
         * @return array
         * @throws \Exception
         */
-       public static function createPhotoForSystemResource($filename, $mimetype = "image/jpeg")
+       public static function createPhotoForSystemResource($filename, $mimetype = '')
        {
+               if (empty($mimetype)) {
+                       $mimetype = Images::guessTypeByExtension($filename);
+               }
+
                $fields = self::getFields();
                $values = array_fill(0, count($fields), "");
 
@@ -263,6 +268,33 @@ class Photo
                return $photo;
        }
 
+       /**
+        * Construct a photo array for an external resource image
+        *
+        * @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.
+        *
+        * @return array
+        * @throws \Exception
+        */
+       public static function createPhotoForExternalResource($url, $uid = 0, $mimetype = '')
+       {
+               if (empty($mimetype)) {
+                       $mimetype = Images::guessTypeByExtension($url);
+               }
+
+               $fields = self::getFields();
+               $values = array_fill(0, count($fields), "");
+
+               $photo                  = array_combine($fields, $values);
+               $photo['backend-class'] = ExternalResource::NAME;
+               $photo['backend-ref']   = json_encode(['url' => $url, 'uid' => $uid]);
+               $photo['type']          = $mimetype;
+               $photo['cacheable']     = true;
+
+               return $photo;
+       }
 
        /**
         * store photo metadata in db and binary in default backend
@@ -772,30 +804,33 @@ class Photo
        }
 
        /**
-        * Returns the GUID from picture links
+        * Fetch the guid and scale from picture links
         *
         * @param string $name Picture link
-        * @return string GUID
-        * @throws \Exception
+        * @return array
         */
-       public static function getGUID($name)
+       public static function getResourceData(string $name):array
        {
                $base = DI::baseUrl()->get();
 
                $guid = str_replace([Strings::normaliseLink($base), '/photo/'], '', Strings::normaliseLink($name));
 
+               if (parse_url($guid, PHP_URL_SCHEME)) {
+                       return [];
+               }
+
                $guid = self::stripExtension($guid);
                if (substr($guid, -2, 1) != "-") {
-                       return '';
+                       return [];
                }
 
                $scale = intval(substr($guid, -1, 1));
                if (!is_numeric($scale)) {
-                       return '';
+                       return [];
                }
 
                $guid = substr($guid, 0, -2);
-               return $guid;
+               return ['guid' => $guid, 'scale' => $scale];
        }
 
        /**
@@ -807,13 +842,12 @@ class Photo
         */
        public static function isLocal($name)
        {
-               $guid = self::getGUID($name);
-
-               if (empty($guid)) {
+               $data = self::getResourceData($name);
+               if (empty($data)) {
                        return false;
                }
 
-               return DBA::exists('photo', ['resource-id' => $guid]);
+               return DBA::exists('photo', ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
        }
 
        /**