Avoid recreating small images where possible + bump libheif-js

This commit is contained in:
Jamie Kyle 2023-03-03 12:08:24 -08:00 committed by GitHub
parent fa68964510
commit 03697f66e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View file

@ -1,6 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { LoadImageResult } from 'blueimp-load-image';
import loadImage from 'blueimp-load-image';
import type { MIMEType } from '../types/MIME';
@ -115,16 +116,16 @@ export async function scaleImageToLevel(
blob: Blob;
contentType: MIMEType;
}> {
let image: HTMLCanvasElement;
let data: LoadImageResult;
try {
const data = await loadImage(fileOrBlobOrURL, {
data = await loadImage(fileOrBlobOrURL, {
canvas: true,
orientation: true,
meta: true, // Check if we need to strip EXIF data
});
if (!(data.image instanceof HTMLCanvasElement)) {
throw new Error('image not a canvas');
}
({ image } = data);
} catch (cause) {
const error = new Error('scaleImageToLevel: Failed to process image', {
cause,
@ -139,7 +140,14 @@ export async function scaleImageToLevel(
MEDIA_QUALITY_LEVEL_DATA.get(level) || DEFAULT_LEVEL_DATA;
if (fileOrBlobOrURL.size <= thresholdSize) {
const blob = await canvasToBlob(image, contentType);
let blob: Blob;
if (data.exif != null) {
blob = await canvasToBlob(data.image, contentType);
} else {
// If we don't have EXIF data, we can just return the original blob
// to avoid occasionally making the image larger.
blob = fileOrBlobOrURL;
}
return {
blob,
contentType,
@ -154,7 +162,11 @@ export async function scaleImageToLevel(
// We need these operations to be in serial
// eslint-disable-next-line no-await-in-loop
const blob = await getCanvasBlobAsJPEG(image, scalableDimensions, quality);
const blob = await getCanvasBlobAsJPEG(
data.image,
scalableDimensions,
quality
);
if (blob.size <= size) {
return {
blob,
@ -163,7 +175,7 @@ export async function scaleImageToLevel(
}
}
const blob = await getCanvasBlobAsJPEG(image, MIN_DIMENSIONS, quality);
const blob = await getCanvasBlobAsJPEG(data.image, MIN_DIMENSIONS, quality);
return {
blob,
contentType: IMAGE_JPEG,

View file

@ -12081,9 +12081,9 @@ li@^1.3.0:
integrity sha512-z34TU6GlMram52Tss5mt1m//ifRIpKH5Dqm7yUVOdHI+BQCs9qGPHFaCUTIzsWX7edN30aa2WrPwR7IO10FHaw==
libheif-js@^1.10.0:
version "1.12.0"
resolved "https://registry.yarnpkg.com/libheif-js/-/libheif-js-1.12.0.tgz#9ad1ed16a8e6412b4d3d83565d285465a00e7305"
integrity sha512-hDs6xQ7028VOwAFwEtM0Q+B2x2NW69Jb2MhQFUbk3rUrHzz4qo5mqS8VrqNgYnSc8TiUGnR691LnO4uIfEE23w==
version "1.15.1"
resolved "https://registry.yarnpkg.com/libheif-js/-/libheif-js-1.15.1.tgz#4693d664c2c512c6d907153c7ec4e320625470ad"
integrity sha512-1nIVY1IFYLglxHPuLMqMBpjx4wigEEUVnSj2d3pRzeOjzKetwXlVejHJJgomZwEARu0PZ3HeGOW7ID/hZr13cg==
lines-and-columns@^1.1.6:
version "1.2.4"