Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit cd15e08

Browse files
authored
Allow sending and thumbnailing AVIF images (#8172)
1 parent 4e665de commit cd15e08

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/ContentMessages.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,20 @@ async function infoForImageFile(matrixClient: MatrixClient, roomId: string, imag
249249
const result = await createThumbnail(imageElement.img, imageElement.width, imageElement.height, thumbnailType);
250250
const imageInfo = result.info;
251251

252-
// we do all sizing checks here because we still rely on thumbnail generation for making a blurhash from.
253-
const sizeDifference = imageFile.size - imageInfo.thumbnail_info.size;
254-
if (
255-
imageFile.size <= IMAGE_SIZE_THRESHOLD_THUMBNAIL || // image is small enough already
256-
(sizeDifference <= IMAGE_THUMBNAIL_MIN_REDUCTION_SIZE && // thumbnail is not sufficiently smaller than original
257-
sizeDifference <= (imageFile.size * IMAGE_THUMBNAIL_MIN_REDUCTION_PERCENT))
258-
) {
259-
delete imageInfo["thumbnail_info"];
260-
return imageInfo;
252+
// For lesser supported image types, always include the thumbnail even if it is larger
253+
if (!["image/avif", "image/webp"].includes(imageFile.type)) {
254+
// we do all sizing checks here because we still rely on thumbnail generation for making a blurhash from.
255+
const sizeDifference = imageFile.size - imageInfo.thumbnail_info.size;
256+
if (
257+
// image is small enough already
258+
imageFile.size <= IMAGE_SIZE_THRESHOLD_THUMBNAIL ||
259+
// thumbnail is not sufficiently smaller than original
260+
(sizeDifference <= IMAGE_THUMBNAIL_MIN_REDUCTION_SIZE &&
261+
sizeDifference <= (imageFile.size * IMAGE_THUMBNAIL_MIN_REDUCTION_PERCENT))
262+
) {
263+
delete imageInfo["thumbnail_info"];
264+
return imageInfo;
265+
}
261266
}
262267

263268
const uploadResult = await uploadFile(matrixClient, roomId, result.thumbnail);

src/utils/Image.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import { arrayHasDiff } from "./arrays";
1818

1919
export function mayBeAnimated(mimeType: string): boolean {
20-
return ["image/gif", "image/webp", "image/png", "image/apng"].includes(mimeType);
20+
// AVIF animation support at the time of writing is only available in Chrome hence not having `blobIsAnimated` check
21+
return ["image/gif", "image/webp", "image/png", "image/apng", "image/avif"].includes(mimeType);
2122
}
2223

2324
function arrayBufferRead(arr: ArrayBuffer, start: number, len: number): Uint8Array {

src/utils/blobs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const ALLOWED_BLOB_MIMETYPES = [
5454
'image/png',
5555
'image/apng',
5656
'image/webp',
57+
'image/avif',
5758

5859
'video/mp4',
5960
'video/webm',

0 commit comments

Comments
 (0)