]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Photo.php
Replace reference to post plink by author base URL
[friendica.git] / src / Model / Photo.php
index 44ee6a6706e242c245fce15a9aeca9ea0c49aead..f393211548dc0eed7e8f63706dc503c10104943a 100644 (file)
@@ -830,7 +830,7 @@ class Photo
         * Changes photo permissions that had been embedded in a post
         *
         * @todo This function currently does have some flaws:
-        * - Sharing a post with a forum will create a photo that only the forum can see.
+        * - Sharing a post with a group will create a photo that only the group can see.
         * - Sharing a photo again that been shared non public before doesn't alter the permissions.
         *
         * @return string
@@ -877,7 +877,7 @@ class Photo
                        /**
                         * @todo Existing permissions need to be mixed with the new ones.
                         * Otherwise this creates problems with sharing the same picture multiple times
-                        * Also check if $str_contact_allow does contain a public forum.
+                        * Also check if $str_contact_allow does contain a public group.
                         * Then set the permissions to public.
                         */
 
@@ -990,28 +990,21 @@ class Photo
        }
 
        /**
-        * Tries to resize image to wanted maximum size
+        * Resize to a given maximum file size
         *
-        * @param Image $image Image instance
-        * @return Image|null Image instance on success, null on error
+        * @param Image $image
+        * @param integer $maximagesize
+        * @return Image
         */
-       private static function fitImageSize(Image $image)
+       public static function resizeToFileSize(Image $image, int $maximagesize): Image
        {
-               $max_length = DI::config()->get('system', 'max_image_length');
-               if ($max_length > 0) {
-                       $image->scaleDown($max_length);
-                       Logger::info('File upload: Scaling picture to new size', ['max-length' => $max_length]);
-               }
-
                $filesize = strlen($image->asString());
                $width    = $image->getWidth();
                $height   = $image->getHeight();
-
-               $maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', '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) {
+                       foreach ([5120, 2560, 1280, 640, 320] as $pixels) {
                                if (($filesize > $maximagesize) && (max($width, $height) > $pixels)) {
                                        Logger::info('Resize', ['size' => $filesize, 'width' => $width, 'height' => $height, 'max' => $maximagesize, 'pixels' => $pixels]);
                                        $image->scaleDown($pixels);
@@ -1020,13 +1013,26 @@ class Photo
                                        $height = $image->getHeight();
                                }
                        }
-                       if ($filesize > $maximagesize) {
-                               Logger::notice('Image size is too big', ['size' => $filesize, 'max' => $maximagesize]);
-                               return null;
-                       }
                }
+       
+               return $image;  
+       }
 
-               return $image;
+       /**
+        * Tries to resize image to wanted maximum size
+        *
+        * @param Image $image Image instance
+        * @return Image|null Image instance on success, null on error
+        */
+       private static function fitImageSize(Image $image)
+       {
+               $max_length = DI::config()->get('system', 'max_image_length');
+               if ($max_length > 0) {
+                       $image->scaleDown($max_length);
+                       Logger::info('File upload: Scaling picture to new size', ['max-length' => $max_length]);
+               }
+               
+               return self::resizeToFileSize($image, Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize')));
        }
 
        /**
@@ -1237,32 +1243,7 @@ class Photo
         */
        public static function storeWithPreview(Image $image, int $uid, string $resource_id, string $filename, int $filesize, string $album, string $description, string $allow_cid, string $allow_gid, string $deny_cid, string $deny_gid): int
        {
-               if ($filesize == 0) {
-                       $filesize = strlen($image->asString());
-               }
-
-               $width  = $image->getWidth();
-               $height = $image->getHeight();
-
-               $maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize'));
-
-               if ($maximagesize && $filesize > $maximagesize) {
-                       // Scale down to multiples of 640 until the maximum size isn't exceeded anymore
-                       foreach ([5120, 2560, 1280, 640, 320] as $pixels) {
-                               if ($filesize > $maximagesize && max($width, $height) > $pixels) {
-                                       DI::logger()->info('Resize', ['size' => $filesize, 'width' => $width, 'height' => $height, 'max' => $maximagesize, 'pixels' => $pixels]);
-                                       $image->scaleDown($pixels);
-                                       $filesize = strlen($image->asString());
-                                       $width    = $image->getWidth();
-                                       $height   = $image->getHeight();
-                               }
-                       }
-
-                       if ($filesize > $maximagesize) {
-                               DI::logger()->notice('Image size is too big', ['size' => $filesize, 'max' => $maximagesize]);
-                               return -1;
-                       }
-               }
+               $image = self::resizeToFileSize($image, Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize')));
 
                $width   = $image->getWidth();
                $height  = $image->getHeight();