+ /**
+ * 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 circles
+ * @param string $deny_cid Permissions, denied contacts
+ * @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;
+ }
+ }
+
+ $width = $image->getWidth();
+ $height = $image->getHeight();
+ $preview = 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) {
+ $preview = 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 && ($preview == 0)) {
+ $preview = 2;
+ }
+ }
+ return $preview;
+ }
+