Image handling reworked, new image formats added (#13900)

* Image handling reworked, new image formats added

* Updated messages.po

* The dot is now part of the file extension

* Added WebP in install documentation

* Handle unhandled mime types

* Fixed animated picture detected
This commit is contained in:
Michael Vogel 2024-02-17 07:45:41 +01:00 committed by GitHub
parent 1ea8a4042d
commit 14e5b06029
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 541 additions and 386 deletions

View file

@ -842,7 +842,6 @@ class Contact
return false;
}
$file_suffix = 'jpg';
$url = DI::baseUrl() . '/profile/' . $user['nickname'];
$fields = [
@ -875,17 +874,11 @@ class Contact
$fields['avatar-date'] = DateTimeFormat::utcNow();
}
// Creating the path to the avatar, beginning with the file suffix
$types = Images::supportedTypes();
if (isset($types[$avatar['type']])) {
$file_suffix = $types[$avatar['type']];
}
// We are adding a timestamp value so that other systems won't use cached content
$timestamp = strtotime($fields['avatar-date']);
$prefix = DI::baseUrl() . '/photo/' . $avatar['resource-id'] . '-';
$suffix = '.' . $file_suffix . '?ts=' . $timestamp;
$suffix = Images::getExtensionByMimeType($avatar['type']) . '?ts=' . $timestamp;
$fields['photo'] = $prefix . '4' . $suffix;
$fields['thumb'] = $prefix . '5' . $suffix;
@ -2313,8 +2306,8 @@ class Contact
$fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
$img_str = $fetchResult->getBodyString();
if (!empty($img_str)) {
$image = new Image($img_str, Images::getMimeTypeByData($img_str));
if ($fetchResult->isSuccess() && !empty($img_str)) {
$image = new Image($img_str, $fetchResult->getContentType(), $avatar);
if ($image->isValid()) {
$update_fields['blurhash'] = $image->getBlurHash();
} else {

View file

@ -363,6 +363,7 @@ class Photo
$photo['backend-class'] = SystemResource::NAME;
$photo['backend-ref'] = $filename;
$photo['type'] = $mimetype;
$photo['filename'] = basename($filename);
$photo['cacheable'] = false;
return $photo;
@ -394,6 +395,7 @@ class Photo
$photo['backend-class'] = ExternalResource::NAME;
$photo['backend-ref'] = json_encode(['url' => $url, 'uid' => $uid]);
$photo['type'] = $mimetype;
$photo['filename'] = basename(parse_url($url, PHP_URL_PATH));
$photo['cacheable'] = true;
$photo['blurhash'] = $blurhash;
$photo['width'] = $width;
@ -608,9 +610,7 @@ class Photo
return false;
}
$type = Images::getMimeTypeByData($img_str, $image_url, $type);
$image = new Image($img_str, $type);
$image = new Image($img_str, $type, $image_url);
if ($image->isValid()) {
$image->scaleToSquare(300);
@ -619,9 +619,9 @@ class Photo
if ($maximagesize && ($filesize > $maximagesize)) {
Logger::info('Avatar exceeds image limit', ['uid' => $uid, 'cid' => $cid, 'maximagesize' => $maximagesize, 'size' => $filesize, 'type' => $image->getType()]);
if ($image->getType() == 'image/gif') {
if ($image->getImageType() == IMAGETYPE_GIF) {
$image->toStatic();
$image = new Image($image->asString(), 'image/png');
$image = new Image($image->asString(), image_type_to_mime_type(IMAGETYPE_PNG));
$filesize = strlen($image->asString());
Logger::info('Converted gif to a static png', ['uid' => $uid, 'cid' => $cid, 'size' => $filesize, 'type' => $image->getType()]);
@ -662,9 +662,9 @@ class Photo
$suffix = '?ts=' . time();
$image_url = DI::baseUrl() . '/photo/' . $resource_id . '-4.' . $image->getExt() . $suffix;
$thumb = DI::baseUrl() . '/photo/' . $resource_id . '-5.' . $image->getExt() . $suffix;
$micro = DI::baseUrl() . '/photo/' . $resource_id . '-6.' . $image->getExt() . $suffix;
$image_url = DI::baseUrl() . '/photo/' . $resource_id . '-4' . $image->getExt() . $suffix;
$thumb = DI::baseUrl() . '/photo/' . $resource_id . '-5' . $image->getExt() . $suffix;
$micro = DI::baseUrl() . '/photo/' . $resource_id . '-6' . $image->getExt() . $suffix;
} else {
$photo_failure = true;
}
@ -1060,9 +1060,7 @@ class Photo
return [];
}
$type = Images::getMimeTypeByData($img_str, $image_url, $type);
$image = new Image($img_str, $type);
$image = new Image($img_str, $type, $image_url);
$image = self::fitImageSize($image);
if (empty($image)) {
@ -1132,12 +1130,10 @@ class Photo
return [];
}
$filetype = Images::getMimeTypeBySource($src, $filename, $filetype);
Logger::info('File upload', ['src' => $src, 'filename' => $filename, 'size' => $filesize, 'type' => $filetype]);
$imagedata = @file_get_contents($src);
$image = new Image($imagedata, $filetype);
$image = new Image($imagedata, $filetype, $filename);
if (!$image->isValid()) {
Logger::notice('Image is unvalid', ['files' => $files]);
return [];

View file

@ -134,15 +134,23 @@ class Link
Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
return [];
}
$fields = ['mimetype' => $curlResult->getHeader('Content-Type')[0]];
$img_str = $curlResult->getBodyString();
$image = new Image($img_str, Images::getMimeTypeByData($img_str));
if ($image->isValid()) {
$fields['mimetype'] = $image->getType();
$fields['width'] = $image->getWidth();
$fields['height'] = $image->getHeight();
$fields['blurhash'] = $image->getBlurHash();
if (!$curlResult->isSuccess()) {
Logger::notice('Fetching unsuccessful', ['url' => $url]);
return [];
}
$fields = ['mimetype' => $curlResult->getContentType()];
if (Images::isSupportedMimeType($fields['mimetype'])) {
$img_str = $curlResult->getBodyString();
$image = new Image($img_str, $fields['mimetype'], $url);
if ($image->isValid()) {
$fields['mimetype'] = $image->getType();
$fields['width'] = $image->getWidth();
$fields['height'] = $image->getHeight();
$fields['blurhash'] = $image->getBlurHash();
}
}
return $fields;

View file

@ -196,7 +196,7 @@ class Media
if ($curlResult->isSuccess()) {
if (empty($media['mimetype'])) {
$media['mimetype'] = $curlResult->getHeader('Content-Type')[0] ?? '';
$media['mimetype'] = $curlResult->getContentType() ?? '';
}
if (empty($media['size'])) {
$media['size'] = (int)($curlResult->getHeader('Content-Length')[0] ?? 0);

View file

@ -1403,9 +1403,7 @@ class User
$type = '';
}
$type = Images::getMimeTypeByData($img_str, $photo, $type);
$image = new Image($img_str, $type);
$image = new Image($img_str, $type, $photo);
if ($image->isValid()) {
$image->scaleToSquare(300);