]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Photo.php
Merge pull request #12674 from nupplaphil/bug/config_typesafe
[friendica.git] / src / Model / Photo.php
index 550d802e8124b81badc3f372279b79f4b9d2c3fc..9623f5622eabf849292bd45f25bae99c82136306 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
  *
@@ -173,6 +173,64 @@ class Photo
                return $photo;
        }
 
+       /**
+        * Returns all browsable albums for a given user
+        *
+        * @param int $uid The given user
+        *
+        * @return array An array of albums
+        * @throws \Exception
+        */
+       public static function getBrowsableAlbumsForUser(int $uid): array
+       {
+               $photos = DBA::toArray(
+                       DBA::p(
+                               "SELECT DISTINCT(`album`) AS `album` FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?)",
+                               $uid,
+                               static::CONTACT_AVATAR,
+                               static::CONTACT_BANNER
+                       )
+               );
+
+               return array_column($photos, 'album');
+       }
+
+       /**
+        * Returns browsable photos for a given user (optional and a given album)
+        *
+        * @param int         $uid   The given user id
+        * @param string|null $album (optional) The given album
+        *
+        * @return array All photos of the user/album
+        * @throws \Exception
+        */
+       public static function getBrowsablePhotosForUser(int $uid, string $album = null): array
+       {
+               $values = [
+                       $uid,
+                       Photo::CONTACT_AVATAR,
+                       Photo::CONTACT_BANNER
+               ];
+
+               if (!empty($album)) {
+                       $sqlExtra  = "AND `album` = ? ";
+                       $values[] = $album;
+                       $sqlExtra2 = "";
+               } else {
+                       $sqlExtra  = '';
+                       $sqlExtra2 = ' ORDER BY created DESC LIMIT 0, 10';
+               }
+
+               return DBA::toArray(
+                       DBA::p(
+                               "SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
+                                       min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created`
+                                       FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?) $sqlExtra 
+                                       GROUP BY `resource-id` $sqlExtra2",
+                               $values
+                       ));
+       }
+
        /**
         * Check if photo with given conditions exists
         *
@@ -255,6 +313,28 @@ class Photo
                return $fields;
        }
 
+       /**
+        * Construct a photo array for a given image data string
+        *
+        * @param string $image_data Image data
+        * @param string $mimetype   Image mime type. Is guessed by file name when empty.
+        *
+        * @return array
+        * @throws \Exception
+        */
+       public static function createPhotoForImageData(string $image_data, string $mimetype = ''): array
+       {
+               $fields = self::getFields();
+               $values = array_fill(0, count($fields), '');
+
+               $photo                  = array_combine($fields, $values);
+               $photo['data']          = $image_data;
+               $photo['type']          = $mimetype ?: Images::getMimeTypeByData($image_data);
+               $photo['cacheable']     = false;
+
+               return $photo;
+       }
+
        /**
         * Construct a photo array for a system resource image
         *
@@ -288,11 +368,14 @@ class Photo
         * @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.
+        * @param string $blurhash The blurhash that will be used to generate a picture when the original picture can't be fetched
+        * @param int    $width    Image width
+        * @param int    $height   Image height
         *
         * @return array
         * @throws \Exception
         */
-       public static function createPhotoForExternalResource(string $url, int $uid = 0, string $mimetype = ''): array
+       public static function createPhotoForExternalResource(string $url, int $uid = 0, string $mimetype = '', string $blurhash = null, int $width = null, int $height = null): array
        {
                if (empty($mimetype)) {
                        $mimetype = Images::guessTypeByExtension($url);
@@ -306,6 +389,9 @@ class Photo
                $photo['backend-ref']   = json_encode(['url' => $url, 'uid' => $uid]);
                $photo['type']          = $mimetype;
                $photo['cacheable']     = true;
+               $photo['blurhash']      = $blurhash;
+               $photo['width']         = $width;
+               $photo['height']        = $height;
 
                return $photo;
        }
@@ -378,6 +464,7 @@ class Photo
                        'height' => $image->getHeight(),
                        'width' => $image->getWidth(),
                        'datasize' => strlen($image->asString()),
+                       'blurhash' => $image->getBlurHash(),
                        'data' => $data,
                        'scale' => $scale,
                        'photo-type' => $type,
@@ -517,8 +604,9 @@ class Photo
                        $image->scaleToSquare(300);
 
                        $filesize = strlen($image->asString());
-                       $maximagesize = DI::config()->get('system', 'maximagesize');
-                       if (!empty($maximagesize) && ($filesize > $maximagesize)) {
+                       $maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize'));
+
+                       if ($maximagesize && ($filesize > $maximagesize)) {
                                Logger::info('Avatar exceeds image limit', ['uid' => $uid, 'cid' => $cid, 'maximagesize' => $maximagesize, 'size' => $filesize, 'type' => $image->getType()]);
                                if ($image->getType() == 'image/gif') {
                                        $image->toStatic();
@@ -581,7 +669,11 @@ class Photo
                        $micro = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
                }
 
-               return [$image_url, $thumb, $micro];
+               $photo = DBA::selectFirst(
+                       'photo', ['blurhash'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'photo-type' => self::CONTACT_AVATAR]
+               );
+
+               return [$image_url, $thumb, $micro, $photo['blurhash']];
        }
 
        /**
@@ -908,9 +1000,9 @@ class Photo
                $width    = $image->getWidth();
                $height   = $image->getHeight();
 
-               $maximagesize = DI::config()->get('system', 'maximagesize');
+               $maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize'));
 
-               if (!empty($maximagesize) && ($filesize > $maximagesize)) {
+               if ($maximagesize && ($filesize > $maximagesize)) {
                        // Scale down to multiples of 640 until the maximum size isn't exceeded anymore
                        foreach ([5120, 2560, 1280, 640] as $pixels) {
                                if (($filesize > $maximagesize) && (max($width, $height) > $pixels)) {
@@ -1130,8 +1222,8 @@ class Photo
                $picture['height']      = $photo['height'];
                $picture['type']        = $photo['type'];
                $picture['albumpage']   = DI::baseUrl() . '/photos/' . $user['nickname'] . '/image/' . $resource_id;
-               $picture['picture']     = DI::baseUrl() . '/photo/{$resource_id}-0.' . $image->getExt();
-               $picture['preview']     = DI::baseUrl() . '/photo/{$resource_id}-{$smallest}.' . $image->getExt();
+               $picture['picture']     = DI::baseUrl() . '/photo/' . $resource_id . '-0.' . $image->getExt();
+               $picture['preview']     = DI::baseUrl() . '/photo/' . $resource_id . '-' . $smallest . '.' . $image->getExt();
 
                Logger::info('upload done', ['picture' => $picture]);
                return $picture;
@@ -1259,7 +1351,7 @@ class Photo
                        logger::warning('profile banner upload with scale 3 (960) failed');
                }
 
-               logger::info('new profile banner upload ended');
+               logger::info('new profile banner upload ended', ['uid' => $uid, 'resource_id' => $resource_id, 'filename' => $filename]);
 
                $condition = ["`photo-type` = ? AND `resource-id` != ? AND `uid` = ?", self::USER_BANNER, $resource_id, $uid];
                self::update(['photo-type' => self::DEFAULT], $condition);
@@ -1272,4 +1364,3 @@ class Photo
                return $resource_id;
        }
 }
-