2021-08-03 12:29:36 -07:00
|
|
|
|
/* eslint-disable no-case-declarations */
|
2021-10-19 10:03:04 -07:00
|
|
|
|
const DEFAULT_MAX_PIXELS = 1920 * 1080;
|
2021-06-16 12:50:59 -07:00
|
|
|
|
|
2022-04-24 12:28:07 -07:00
|
|
|
|
interface BrowserCanvasQuirks {
|
|
|
|
|
'image-orientation-automatic'?: boolean,
|
|
|
|
|
'canvas-read-unreliable'?: boolean,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const _browser_quirks: BrowserCanvasQuirks = {};
|
2021-06-16 12:50:59 -07:00
|
|
|
|
|
|
|
|
|
// Some browsers will automatically draw images respecting their EXIF orientation
|
|
|
|
|
// while others won't, and the safest way to detect that is to examine how it
|
|
|
|
|
// is done on a known image.
|
|
|
|
|
// See https://github.com/w3c/csswg-drafts/issues/4666
|
|
|
|
|
// and https://github.com/blueimp/JavaScript-Load-Image/commit/1e4df707821a0afcc11ea0720ee403b8759f3881
|
2022-04-24 12:28:07 -07:00
|
|
|
|
const dropOrientationIfNeeded = (orientation: number) => new Promise<number>(resolve => {
|
2021-06-16 12:50:59 -07:00
|
|
|
|
switch (_browser_quirks['image-orientation-automatic']) {
|
2022-05-11 14:06:35 -07:00
|
|
|
|
case true:
|
|
|
|
|
resolve(1);
|
|
|
|
|
break;
|
|
|
|
|
case false:
|
|
|
|
|
resolve(orientation);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2021-06-16 12:50:59 -07:00
|
|
|
|
// black 2x1 JPEG, with the following meta information set:
|
|
|
|
|
// - EXIF Orientation: 6 (Rotated 90° CCW)
|
2022-05-11 14:06:35 -07:00
|
|
|
|
const testImageURL =
|
2021-06-16 12:50:59 -07:00
|
|
|
|
'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' +
|
|
|
|
|
'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' +
|
|
|
|
|
'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' +
|
|
|
|
|
'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' +
|
|
|
|
|
'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' +
|
|
|
|
|
'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q==';
|
2022-05-11 14:06:35 -07:00
|
|
|
|
const img = new Image();
|
|
|
|
|
img.onload = () => {
|
|
|
|
|
const automatic = (img.width === 1 && img.height === 2);
|
|
|
|
|
_browser_quirks['image-orientation-automatic'] = automatic;
|
|
|
|
|
resolve(automatic ? 1 : orientation);
|
|
|
|
|
};
|
|
|
|
|
img.onerror = () => {
|
|
|
|
|
_browser_quirks['image-orientation-automatic'] = false;
|
|
|
|
|
resolve(orientation);
|
|
|
|
|
};
|
|
|
|
|
img.src = testImageURL;
|
2021-06-16 12:50:59 -07:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-24 12:28:07 -07:00
|
|
|
|
// /**
|
|
|
|
|
// *Some browsers don't allow reading from a canvas and instead return all-white
|
|
|
|
|
// * or randomized data. Use a pre-defined image to check if reading the canvas
|
|
|
|
|
// * works.
|
|
|
|
|
// */
|
|
|
|
|
// const checkCanvasReliability = () => new Promise<void>((resolve, reject) => {
|
2021-11-15 11:41:33 -08:00
|
|
|
|
// switch(_browser_quirks['canvas-read-unreliable']) {
|
|
|
|
|
// case true:
|
|
|
|
|
// reject('Canvas reading unreliable');
|
|
|
|
|
// break;
|
|
|
|
|
// case false:
|
|
|
|
|
// resolve();
|
|
|
|
|
// break;
|
|
|
|
|
// default:
|
|
|
|
|
// // 2×2 GIF with white, red, green and blue pixels
|
|
|
|
|
// const testImageURL =
|
|
|
|
|
// 'data:image/gif;base64,R0lGODdhAgACAKEDAAAA//8AAAD/AP///ywAAAAAAgACAAACA1wEBQA7';
|
|
|
|
|
// const refData =
|
|
|
|
|
// [255, 255, 255, 255, 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255];
|
|
|
|
|
// const img = new Image();
|
|
|
|
|
// img.onload = () => {
|
|
|
|
|
// const canvas = document.createElement('canvas');
|
|
|
|
|
// const context = canvas.getContext('2d');
|
2022-04-24 12:28:07 -07:00
|
|
|
|
// context?.drawImage(img, 0, 0, 2, 2);
|
|
|
|
|
// const imageData = context?.getImageData(0, 0, 2, 2);
|
|
|
|
|
// if (imageData?.data.every((x, i) => refData[i] === x)) {
|
2021-11-15 11:41:33 -08:00
|
|
|
|
// _browser_quirks['canvas-read-unreliable'] = false;
|
|
|
|
|
// resolve();
|
|
|
|
|
// } else {
|
|
|
|
|
// _browser_quirks['canvas-read-unreliable'] = true;
|
|
|
|
|
// reject('Canvas reading unreliable');
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
// img.onerror = () => {
|
|
|
|
|
// _browser_quirks['canvas-read-unreliable'] = true;
|
|
|
|
|
// reject('Failed to load test image');
|
|
|
|
|
// };
|
|
|
|
|
// img.src = testImageURL;
|
|
|
|
|
// }
|
|
|
|
|
// });
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
2022-04-24 12:28:07 -07:00
|
|
|
|
/** Convert the file into a local blob URL. */
|
|
|
|
|
const getImageUrl = (inputFile: File) => new Promise<string>((resolve, reject) => {
|
|
|
|
|
// @ts-ignore: This is a browser capabilities check.
|
2022-02-18 18:04:03 -08:00
|
|
|
|
if (window.URL?.createObjectURL) {
|
2020-03-27 13:59:38 -07:00
|
|
|
|
try {
|
|
|
|
|
resolve(URL.createObjectURL(inputFile));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
reject(error);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onerror = (...args) => reject(...args);
|
2022-04-24 12:28:07 -07:00
|
|
|
|
reader.onload = ({ target }) => resolve((target?.result || '') as string);
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
|
|
reader.readAsDataURL(inputFile);
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-24 12:28:07 -07:00
|
|
|
|
/** Get an image element from a file. */
|
|
|
|
|
const loadImage = (inputFile: File) => new Promise<HTMLImageElement>((resolve, reject) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
|
getImageUrl(inputFile).then(url => {
|
|
|
|
|
const img = new Image();
|
|
|
|
|
|
2022-04-24 12:28:07 -07:00
|
|
|
|
img.onerror = (...args) => reject([...args]);
|
2020-03-27 13:59:38 -07:00
|
|
|
|
img.onload = () => resolve(img);
|
|
|
|
|
|
|
|
|
|
img.src = url;
|
|
|
|
|
}).catch(reject);
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-24 12:28:07 -07:00
|
|
|
|
/** Get the exif orientation for the image. */
|
|
|
|
|
const getOrientation = (img: HTMLImageElement, type = 'image/png') => new Promise<number>(resolve => {
|
2020-03-27 13:59:38 -07:00
|
|
|
|
if (!['image/jpeg', 'image/webp'].includes(type)) {
|
|
|
|
|
resolve(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 09:40:03 -07:00
|
|
|
|
import(/* webpackChunkName: "features/compose" */'exif-js').then(({ default: EXIF }) => {
|
2022-04-24 12:28:07 -07:00
|
|
|
|
// @ts-ignore: The TypeScript definition is wrong.
|
2021-09-12 09:40:03 -07:00
|
|
|
|
EXIF.getData(img, () => {
|
|
|
|
|
const orientation = EXIF.getTag(img, 'Orientation');
|
|
|
|
|
if (orientation !== 1) {
|
|
|
|
|
dropOrientationIfNeeded(orientation).then(resolve).catch(() => resolve(orientation));
|
|
|
|
|
} else {
|
|
|
|
|
resolve(orientation);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}).catch(() => {});
|
2020-03-27 13:59:38 -07:00
|
|
|
|
});
|
|
|
|
|
|
2022-04-24 12:28:07 -07:00
|
|
|
|
const processImage = (
|
|
|
|
|
img: HTMLImageElement,
|
|
|
|
|
{
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
orientation,
|
|
|
|
|
type = 'image/png',
|
|
|
|
|
name = 'resized.png',
|
|
|
|
|
} : {
|
|
|
|
|
width: number,
|
|
|
|
|
height: number,
|
|
|
|
|
orientation: number,
|
|
|
|
|
type?: string,
|
|
|
|
|
name?: string,
|
|
|
|
|
},
|
|
|
|
|
) => new Promise<File>((resolve, reject) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
|
|
|
|
|
|
if (4 < orientation && orientation < 9) {
|
|
|
|
|
canvas.width = height;
|
|
|
|
|
canvas.height = width;
|
|
|
|
|
} else {
|
|
|
|
|
canvas.width = width;
|
|
|
|
|
canvas.height = height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
|
|
2022-04-24 12:28:07 -07:00
|
|
|
|
if (!context) {
|
|
|
|
|
reject(context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
|
switch (orientation) {
|
2022-05-11 14:06:35 -07:00
|
|
|
|
case 2: context.transform(-1, 0, 0, 1, width, 0); break;
|
|
|
|
|
case 3: context.transform(-1, 0, 0, -1, width, height); break;
|
|
|
|
|
case 4: context.transform(1, 0, 0, -1, 0, height); break;
|
|
|
|
|
case 5: context.transform(0, 1, 1, 0, 0, 0); break;
|
|
|
|
|
case 6: context.transform(0, 1, -1, 0, height, 0); break;
|
|
|
|
|
case 7: context.transform(0, -1, -1, 0, height, width); break;
|
|
|
|
|
case 8: context.transform(0, -1, 1, 0, 0, width); break;
|
2020-03-27 13:59:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.drawImage(img, 0, 0, width, height);
|
|
|
|
|
|
2021-04-17 15:52:10 -07:00
|
|
|
|
canvas.toBlob((blob) => {
|
2022-04-24 12:28:07 -07:00
|
|
|
|
if (!blob) {
|
|
|
|
|
reject(blob);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-17 18:35:46 -07:00
|
|
|
|
resolve(new File([blob], name, { type, lastModified: new Date().getTime() }));
|
2021-04-17 15:52:10 -07:00
|
|
|
|
}, type);
|
2020-03-27 13:59:38 -07:00
|
|
|
|
});
|
|
|
|
|
|
2022-04-24 12:28:07 -07:00
|
|
|
|
const resizeImage = (
|
|
|
|
|
img: HTMLImageElement,
|
|
|
|
|
inputFile: File,
|
|
|
|
|
maxPixels: number,
|
|
|
|
|
) => new Promise<File>((resolve, reject) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
|
const { width, height } = img;
|
2021-04-17 15:52:10 -07:00
|
|
|
|
const type = inputFile.type || 'image/png';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
2021-10-19 10:03:04 -07:00
|
|
|
|
const newWidth = Math.round(Math.sqrt(maxPixels * (width / height)));
|
|
|
|
|
const newHeight = Math.round(Math.sqrt(maxPixels * (height / width)));
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
2021-11-15 11:41:33 -08:00
|
|
|
|
// Skip canvas reliability check for now (it's unreliable)
|
|
|
|
|
// checkCanvasReliability()
|
|
|
|
|
// .then(getOrientation(img, type))
|
|
|
|
|
getOrientation(img, type)
|
2020-03-27 13:59:38 -07:00
|
|
|
|
.then(orientation => processImage(img, {
|
|
|
|
|
width: newWidth,
|
|
|
|
|
height: newHeight,
|
2021-04-17 15:52:10 -07:00
|
|
|
|
name: inputFile.name,
|
2020-03-27 13:59:38 -07:00
|
|
|
|
orientation,
|
|
|
|
|
type,
|
|
|
|
|
}))
|
|
|
|
|
.then(resolve)
|
|
|
|
|
.catch(reject);
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-24 12:28:07 -07:00
|
|
|
|
/** Resize an image to the maximum number of pixels. */
|
|
|
|
|
export default (inputFile: File, maxPixels = DEFAULT_MAX_PIXELS) => new Promise<File>((resolve) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
|
if (!inputFile.type.match(/image.*/) || inputFile.type === 'image/gif') {
|
|
|
|
|
resolve(inputFile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadImage(inputFile).then(img => {
|
2021-10-19 10:03:04 -07:00
|
|
|
|
if (img.width * img.height < maxPixels) {
|
2020-03-27 13:59:38 -07:00
|
|
|
|
resolve(inputFile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 10:03:04 -07:00
|
|
|
|
resizeImage(img, inputFile, maxPixels)
|
2020-03-27 13:59:38 -07:00
|
|
|
|
.then(resolve)
|
2021-11-15 11:13:36 -08:00
|
|
|
|
.catch(error => {
|
|
|
|
|
console.error(error);
|
|
|
|
|
resolve(inputFile);
|
|
|
|
|
});
|
2021-06-16 12:50:59 -07:00
|
|
|
|
}).catch(() => resolve(inputFile));
|
2020-03-27 13:59:38 -07:00
|
|
|
|
});
|