fix: make sure we scale image before trying to compress it

pull/2887/head
Audric Ackermann 2 years ago
parent 70b220400c
commit 1b73112294

@ -76,7 +76,7 @@
"auto-bind": "^4.0.0", "auto-bind": "^4.0.0",
"backbone": "1.3.3", "backbone": "1.3.3",
"blob-util": "2.0.2", "blob-util": "2.0.2",
"blueimp-load-image": "5.14.0", "blueimp-load-image": "^5.16.0",
"buffer-crc32": "0.2.13", "buffer-crc32": "0.2.13",
"bunyan": "^1.8.15", "bunyan": "^1.8.15",
"bytebuffer": "^5.0.1", "bytebuffer": "^5.0.1",
@ -134,7 +134,7 @@
"devDependencies": { "devDependencies": {
"@electron/notarize": "^2.1.0", "@electron/notarize": "^2.1.0",
"@types/backbone": "1.4.2", "@types/backbone": "1.4.2",
"@types/blueimp-load-image": "5.14.4", "@types/blueimp-load-image": "^5.16.2",
"@types/buffer-crc32": "^0.2.0", "@types/buffer-crc32": "^0.2.0",
"@types/bunyan": "^1.8.8", "@types/bunyan": "^1.8.8",
"@types/bytebuffer": "^5.0.41", "@types/bytebuffer": "^5.0.41",

@ -2,7 +2,7 @@
import imageType from 'image-type'; import imageType from 'image-type';
import { arrayBufferToBlob } from 'blob-util'; import { arrayBufferToBlob } from 'blob-util';
import loadImage, { LoadImageOptions } from 'blueimp-load-image'; import loadImage from 'blueimp-load-image';
import { StagedAttachmentType } from '../components/conversation/composition/CompositionBox'; import { StagedAttachmentType } from '../components/conversation/composition/CompositionBox';
import { SignalService } from '../protobuf'; import { SignalService } from '../protobuf';
import { getDecryptedMediaUrl } from '../session/crypto/DecryptedAttachmentsManager'; import { getDecryptedMediaUrl } from '../session/crypto/DecryptedAttachmentsManager';
@ -189,28 +189,32 @@ export async function autoScale<T extends { contentType: string; blob: Blob }>(
throw new Error(`GIF is too large, required size is ${maxSize}`); throw new Error(`GIF is too large, required size is ${maxSize}`);
} }
const loadImgOpts: LoadImageOptions = {
maxWidth: makeSquare ? maxMeasurements?.maxSide : maxWidth,
maxHeight: makeSquare ? maxMeasurements?.maxSide : maxHeight,
crop: !!makeSquare,
orientation: 1,
aspectRatio: makeSquare ? 1 : undefined,
canvas: true,
imageSmoothingQuality: 'medium',
};
perfStart(`loadimage-*${blob.size}`); perfStart(`loadimage-*${blob.size}`);
const canvas = await loadImage(blob, loadImgOpts); const canvasLoad = await loadImage(blob, {});
const canvasScaled = loadImage.scale(
canvasLoad.image, // img or canvas element
{
maxWidth: makeSquare ? maxMeasurements?.maxSide : maxWidth,
maxHeight: makeSquare ? maxMeasurements?.maxSide : maxHeight,
crop: !!makeSquare,
cover: !!makeSquare,
orientation: 1,
// aspectRatio: makeSquare ? 1 : undefined,
canvas: true,
imageSmoothingQuality: 'medium',
meta: false,
}
);
perfEnd(`loadimage-*${blob.size}`, `loadimage-*${blob.size}`); perfEnd(`loadimage-*${blob.size}`, `loadimage-*${blob.size}`);
if (!canvas || !canvas.originalWidth || !canvas.originalHeight) { if (!canvasScaled || !canvasScaled.width || !canvasScaled.height) {
throw new Error('failed to scale image'); throw new Error('failed to scale image');
} }
let readAndResizedBlob = blob; let readAndResizedBlob = blob;
if ( if (
canvas.originalWidth <= maxWidth && canvasScaled.width <= maxWidth &&
canvas.originalHeight <= maxHeight && canvasScaled.height <= maxHeight &&
blob.size <= maxSize && blob.size <= maxSize &&
!makeSquare !makeSquare
) { ) {
@ -218,15 +222,15 @@ export async function autoScale<T extends { contentType: string; blob: Blob }>(
// so we have to return those measures as the loaded file has now those measures. // so we have to return those measures as the loaded file has now those measures.
return { return {
...attachment, ...attachment,
width: canvas.image.width, width: canvasScaled.width,
height: canvas.image.height, height: canvasScaled.height,
blob, blob,
}; };
} }
if (DEBUG_ATTACHMENTS_SCALE) { if (DEBUG_ATTACHMENTS_SCALE) {
window.log.debug('canvas.originalWidth', { window.log.info('canvasOri.originalWidth', {
canvasOriginalWidth: canvas.originalWidth, canvasOriginalWidth: canvasScaled.width,
canvasOriginalHeight: canvas.originalHeight, canvasOriginalHeight: canvasScaled.height,
maxWidth, maxWidth,
maxHeight, maxHeight,
blobsize: blob.size, blobsize: blob.size,
@ -240,10 +244,10 @@ export async function autoScale<T extends { contentType: string; blob: Blob }>(
do { do {
i -= 1; i -= 1;
if (DEBUG_ATTACHMENTS_SCALE) { if (DEBUG_ATTACHMENTS_SCALE) {
// window.log.info(`autoscale iteration: [${i}] for:`, attachment); window.log.info(`autoscale iteration: [${i}] for:`, JSON.stringify(readAndResizedBlob.size));
} }
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const tempBlob = await canvasToBlob(canvas.image as HTMLCanvasElement, 'image/jpeg', quality); const tempBlob = await canvasToBlob(canvasScaled, 'image/jpeg', quality);
if (!tempBlob) { if (!tempBlob) {
throw new Error('Failed to get blob during canvasToBlob.'); throw new Error('Failed to get blob during canvasToBlob.');
@ -265,8 +269,8 @@ export async function autoScale<T extends { contentType: string; blob: Blob }>(
contentType: attachment.contentType, contentType: attachment.contentType,
blob: readAndResizedBlob, blob: readAndResizedBlob,
width: canvas.image.width, width: canvasScaled.width,
height: canvas.image.height, height: canvasScaled.height,
}; };
} }

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save