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

Commit

Permalink
Allow sending and thumbnailing AVIF images (#8172)
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Mar 29, 2022
1 parent 4e665de commit cd15e08
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
23 changes: 14 additions & 9 deletions src/ContentMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,20 @@ async function infoForImageFile(matrixClient: MatrixClient, roomId: string, imag
const result = await createThumbnail(imageElement.img, imageElement.width, imageElement.height, thumbnailType);
const imageInfo = result.info;

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

const uploadResult = await uploadFile(matrixClient, roomId, result.thumbnail);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import { arrayHasDiff } from "./arrays";

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

function arrayBufferRead(arr: ArrayBuffer, start: number, len: number): Uint8Array {
Expand Down
1 change: 1 addition & 0 deletions src/utils/blobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const ALLOWED_BLOB_MIMETYPES = [
'image/png',
'image/apng',
'image/webp',
'image/avif',

'video/mp4',
'video/webm',
Expand Down

0 comments on commit cd15e08

Please sign in to comment.