Unifiy storing of photos with previews

This commit is contained in:
Michael 2023-05-09 05:29:05 +00:00
parent 1789266859
commit d83073f2a2
3 changed files with 82 additions and 95 deletions

View file

@ -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
*