diff --git a/src/Model/Photo.php b/src/Model/Photo.php index d6039dae8..e07b6b6af 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -1148,7 +1148,7 @@ class Photo return []; } - return ['image' => $image, 'filename' => $filename]; + return ['image' => $image, 'filename' => $filename, 'size' => $filesize]; } /** @@ -1182,8 +1182,7 @@ class Photo $image = $data['image']; $filename = $data['filename']; - $width = $image->getWidth(); - $height = $image->getHeight(); + $filesize = $data['size']; $resource_id = $resource_id ?: self::newResource(); $album = $album ?: DI::l10n()->t('Wall Photos'); @@ -1193,30 +1192,12 @@ class Photo $allow_gid = ''; } - $smallest = 0; - - $result = self::store($image, $user['uid'], 0, $resource_id, $filename, $album, 0, self::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); - if (!$result) { - Logger::warning('Photo could not be stored', ['uid' => $user['uid'], 'resource_id' => $resource_id, 'filename' => $filename, 'album' => $album]); + $smallest = self::storeWithPreview($image, $user['uid'], $resource_id, $filename, $filesize, $album, $desc, $allow_cid, $allow_gid, $deny_cid, $deny_gid); + if ($smallest < 0) { + Logger::notice('Photo not stored', ['resource-id' => $resource_id]); return []; } - if ($width > 640 || $height > 640) { - $image->scaleDown(640); - } - - if ($width > 320 || $height > 320) { - $r = self::store($image, $user['uid'], 0, $resource_id, $filename, $album, 1, self::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); - if ($r) { - $smallest = 1; - } - $image->scaleDown(320); - $r = self::store($image, $user['uid'], 0, $resource_id, $filename, $album, 2, self::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc); - if ($r && ($smallest == 0)) { - $smallest = 2; - } - } - $condition = ['resource-id' => $resource_id]; $photo = self::selectFirst(['id', 'datasize', 'width', 'height', 'type'], $condition, ['order' => ['width' => true]]); if (empty($photo)) { @@ -1240,6 +1221,77 @@ class Photo return $picture; } + /** + * store photo metadata in db and binary with preview photos in default backend + * + * @param Image $image Image object with data + * @param integer $uid User ID + * @param string $resource_id Resource ID + * @param string $filename Filename + * @param integer $filesize Filesize + * @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 $deny_cid Permissions, denied contacts + * @param string $deny_gid Permissions, denied group + * + * @return boolean True on success + * @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 + { + $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; + } + } + + $width = $image->getWidth(); + $height = $image->getHeight(); + $smallest = 0; + + $result = self::store($image, $uid, 0, $resource_id, $filename, $album, 0, self::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $description); + if (!$result) { + Logger::warning('Photo could not be stored', ['uid' => $uid, 'resource_id' => $resource_id, 'filename' => $filename, 'album' => $album]); + return -1; + } + + if ($width > 640 || $height > 640) { + $image->scaleDown(640); + } + + if ($width > 320 || $height > 320) { + $result = self::store($image, $uid, 0, $resource_id, $filename, $album, 1, self::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $description); + if ($result) { + $smallest = 1; + } + $image->scaleDown(320); + $result = self::store($image, $uid, 0, $resource_id, $filename, $album, 2, self::DEFAULT, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $description); + if ($result && ($smallest == 0)) { + $smallest = 2; + } + } + return $smallest; + } + /** * Upload a user avatar * diff --git a/src/Module/Media/Photo/Upload.php b/src/Module/Media/Photo/Upload.php index 3b1b69ef4..d00093388 100644 --- a/src/Module/Media/Photo/Upload.php +++ b/src/Module/Media/Photo/Upload.php @@ -164,34 +164,8 @@ class Upload extends \Friendica\BaseModule $this->logger->info('File upload: Scaling picture to new size', ['max_length' => $max_length]); } - $width = $image->getWidth(); - $height = $image->getHeight(); - - $maximagesize = Strings::getBytesFromShorthand($this->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) { - if ($filesize > $maximagesize && max($width, $height) > $pixels) { - $this->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) { - @unlink($src); - $this->logger->notice('Image size is too big', ['size' => $filesize, 'max' => $maximagesize]); - $this->return(401, $this->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize))); - } - } - $resource_id = Photo::newResource(); - $smallest = 0; - // If we don't have an album name use the Wall Photos album if (!strlen($album)) { $album = $this->t('Wall Photos'); @@ -199,26 +173,10 @@ class Upload extends \Friendica\BaseModule $allow_cid = '<' . $owner['id'] . '>'; - $result = Photo::store($image, $owner['uid'], 0, $resource_id, $filename, $album, 0, Photo::DEFAULT, $allow_cid); - if (!$result) { - $this->logger->warning('Photo::store() failed', ['result' => $result]); - $this->return(401, $this->t('Image upload failed.')); - } - - if ($width > 640 || $height > 640) { - $image->scaleDown(640); - } - - if ($width > 320 || $height > 320) { - $result = Photo::store($image, $owner['uid'], 0, $resource_id, $filename, $album, 1, Photo::DEFAULT, $allow_cid); - if ($result) { - $smallest = 1; - } - $image->scaleDown(320); - $result = Photo::store($image, $owner['uid'], 0, $resource_id, $filename, $album, 2, Photo::DEFAULT, $allow_cid); - if ($result && ($smallest == 0)) { - $smallest = 2; - } + $smallest = Photo::storeWithPreview($image, $owner['uid'], $resource_id, $filename, $filesize, $album, '', $allow_cid, '', '', ''); + if ($smallest < 0) { + $this->return(401, $this->t('Image could not be uploaded')); + @unlink($src); } $this->logger->info('upload done'); diff --git a/src/Module/Profile/Photos.php b/src/Module/Profile/Photos.php index ebbba283b..6fa04d977 100644 --- a/src/Module/Profile/Photos.php +++ b/src/Module/Profile/Photos.php @@ -229,38 +229,15 @@ class Photos extends \Friendica\Module\BaseProfile $image->scaleDown($max_length); } - $width = $image->getWidth(); - $height = $image->getHeight(); - - $smallest = 0; - $resource_id = Photo::newResource(); - $r = Photo::store($image, $this->owner['uid'], 0, $resource_id, $filename, $album, 0 , Photo::DEFAULT, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); - - if (!$r) { + $smallest = Photo::storeWithPreview($image, $this->owner['uid'], $resource_id, $filename, $filesize, $album, '', $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); + if ($smallest < 0) { $this->logger->warning('image store failed'); $this->systemMessages->addNotice($this->t('Image upload failed.')); return; } - if ($width > 640 || $height > 640) { - $image->scaleDown(640); - } - - if ($width > 320 || $height > 320) { - $result = Photo::store($image, $this->owner['uid'], 0, $resource_id, $filename, $album, 1, Photo::DEFAULT, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); - if ($result) { - $smallest = 1; - } - - $image->scaleDown(320); - $result = Photo::store($image, $this->owner['uid'], 0, $resource_id, $filename, $album, 2, Photo::DEFAULT, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny); - if ($result && ($smallest == 0)) { - $smallest = 2; - } - } - $uri = Item::newURI(); // Create item container