]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Photo.php
New function to resize an image to not exceed a given file size
[friendica.git] / src / Model / Photo.php
index 74031a822e0341c8e83c31a7318f56aa6e406cf1..f393211548dc0eed7e8f63706dc503c10104943a 100644 (file)
@@ -414,9 +414,9 @@ class Photo
         * @param integer $scale     Scale
         * @param integer $type      Photo type, optional, default: Photo::DEFAULT
         * @param string  $allow_cid Permissions, allowed contacts. optional, default = ""
-        * @param string  $allow_gid Permissions, allowed groups. optional, default = ""
-        * @param string  $deny_cid  Permissions, denied contacts.optional, default = ""
-        * @param string  $deny_gid  Permissions, denied group.optional, default = ""
+        * @param string  $allow_gid Permissions, allowed circles. optional, default = ""
+        * @param string  $deny_cid  Permissions, denied contacts. optional, default = ""
+        * @param string  $deny_gid  Permissions, denied circle. optional, default = ""
         * @param string  $desc      Photo caption. optional, default = ""
         *
         * @return boolean True on success
@@ -830,13 +830,13 @@ 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
         * @throws \Exception
         */
-       public static function setPermissionFromBody($body, $uid, $original_contact_id, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny)
+       public static function setPermissionFromBody($body, $uid, $original_contact_id, $str_contact_allow, $str_circle_allow, $str_contact_deny, $str_circle_deny)
        {
                // Simplify image codes
                $img_body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
@@ -877,11 +877,11 @@ 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.
                         */
 
-                       self::setPermissionForResource($image_rid, $uid, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
+                       self::setPermissionForResource($image_rid, $uid, $str_contact_allow, $str_circle_allow, $str_contact_deny, $str_circle_deny);
                }
 
                return true;
@@ -894,15 +894,15 @@ class Photo
         * @param string $image_rid
         * @param integer $uid
         * @param string $str_contact_allow
-        * @param string $str_group_allow
+        * @param string $str_circle_allow
         * @param string $str_contact_deny
-        * @param string $str_group_deny
+        * @param string $str_circle_deny
         * @return void
         */
-       public static function setPermissionForResource(string $image_rid, int $uid, string $str_contact_allow, string $str_group_allow, string $str_contact_deny, string $str_group_deny)
+       public static function setPermissionForResource(string $image_rid, int $uid, string $str_contact_allow, string $str_circle_allow, string $str_contact_deny, string $str_circle_deny)
        {
-               $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
-               'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny,
+               $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_circle_allow,
+               'deny_cid' => $str_contact_deny, 'deny_gid' => $str_circle_deny,
                'accessible' => DI::pConfig()->get($uid, 'system', 'accessible-photos', false)];
 
                $condition = ['resource-id' => $image_rid, 'uid' => $uid];
@@ -918,9 +918,7 @@ class Photo
         */
        public static function getResourceData(string $name): array
        {
-               $base = DI::baseUrl();
-
-               $guid = str_replace([Strings::normaliseLink($base), '/photo/'], '', Strings::normaliseLink($name));
+               $guid = str_replace([Strings::normaliseLink((string)DI::baseUrl()), '/photo/'], '', Strings::normaliseLink($name));
 
                if (parse_url($guid, PHP_URL_SCHEME)) {
                        return [];
@@ -982,9 +980,7 @@ class Photo
         */
        public static function isLocalPage(string $name): bool
        {
-               $base = DI::baseUrl();
-
-               $guid = str_replace(Strings::normaliseLink($base), '', Strings::normaliseLink($name));
+               $guid = str_replace(Strings::normaliseLink((string)DI::baseUrl()), '', Strings::normaliseLink($name));
                $guid = preg_replace("=/photos/.*/image/(.*)=ism", '$1', $guid);
                if (empty($guid)) {
                        return false;
@@ -994,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);
@@ -1024,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')));
        }
 
        /**
@@ -1232,41 +1234,16 @@ class Photo
         * @param string  $album       Album name
         * @param string  $description Photo caption
         * @param string  $allow_cid   Permissions, allowed contacts
-        * @param string  $allow_gid   Permissions, allowed groups
+        * @param string  $allow_gid   Permissions, allowed circles
         * @param string  $deny_cid    Permissions, denied contacts
-        * @param string  $deny_gid    Permissions, denied group
+        * @param string  $deny_gid    Permissions, denied circles
         *
         * @return integer preview photo size
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        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();