2018-05-09 16:41:07 +10:00
|
|
|
|
import EXIF from 'exif-js';
|
|
|
|
|
|
2018-07-28 11:33:00 +10:00
|
|
|
|
const MAX_IMAGE_PIXELS = 1638400; // 1280x1280px
|
2018-04-23 17:15:51 +10:00
|
|
|
|
|
2020-04-18 23:37:03 +10:00
|
|
|
|
const _browser_quirks = {};
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
const dropOrientationIfNeeded = (orientation) => new Promise(resolve => {
|
|
|
|
|
switch (_browser_quirks['image-orientation-automatic']) {
|
|
|
|
|
case true:
|
|
|
|
|
resolve(1);
|
|
|
|
|
break;
|
|
|
|
|
case false:
|
|
|
|
|
resolve(orientation);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// black 2x1 JPEG, with the following meta information set:
|
|
|
|
|
// - EXIF Orientation: 6 (Rotated 90° CCW)
|
|
|
|
|
const testImageURL =
|
|
|
|
|
'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' +
|
|
|
|
|
'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' +
|
|
|
|
|
'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' +
|
|
|
|
|
'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' +
|
|
|
|
|
'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' +
|
|
|
|
|
'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q==';
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-19 04:01:58 +11: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((resolve, reject) => {
|
|
|
|
|
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');
|
|
|
|
|
context.drawImage(img, 0, 0, 2, 2);
|
|
|
|
|
const imageData = context.getImageData(0, 0, 2, 2);
|
|
|
|
|
if (imageData.data.every((x, i) => refData[i] === x)) {
|
|
|
|
|
_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;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-04-23 17:15:51 +10:00
|
|
|
|
const getImageUrl = inputFile => new Promise((resolve, reject) => {
|
|
|
|
|
if (window.URL && URL.createObjectURL) {
|
|
|
|
|
try {
|
|
|
|
|
resolve(URL.createObjectURL(inputFile));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
reject(error);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onerror = (...args) => reject(...args);
|
|
|
|
|
reader.onload = ({ target }) => resolve(target.result);
|
|
|
|
|
|
|
|
|
|
reader.readAsDataURL(inputFile);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const loadImage = inputFile => new Promise((resolve, reject) => {
|
|
|
|
|
getImageUrl(inputFile).then(url => {
|
|
|
|
|
const img = new Image();
|
|
|
|
|
|
|
|
|
|
img.onerror = (...args) => reject(...args);
|
|
|
|
|
img.onload = () => resolve(img);
|
|
|
|
|
|
|
|
|
|
img.src = url;
|
|
|
|
|
}).catch(reject);
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-09 16:41:07 +10:00
|
|
|
|
const getOrientation = (img, type = 'image/png') => new Promise(resolve => {
|
2019-08-18 06:04:15 +10:00
|
|
|
|
if (type !== 'image/jpeg') {
|
2018-05-09 16:41:07 +10:00
|
|
|
|
resolve(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXIF.getData(img, () => {
|
|
|
|
|
const orientation = EXIF.getTag(img, 'Orientation');
|
2020-04-18 23:37:03 +10:00
|
|
|
|
if (orientation !== 1) {
|
|
|
|
|
dropOrientationIfNeeded(orientation).then(resolve).catch(() => resolve(orientation));
|
|
|
|
|
} else {
|
|
|
|
|
resolve(orientation);
|
|
|
|
|
}
|
2018-05-09 16:41:07 +10:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const processImage = (img, { width, height, orientation, type = 'image/png' }) => new Promise(resolve => {
|
|
|
|
|
const canvas = document.createElement('canvas');
|
2018-05-17 00:24:16 +10:00
|
|
|
|
|
|
|
|
|
if (4 < orientation && orientation < 9) {
|
|
|
|
|
canvas.width = height;
|
|
|
|
|
canvas.height = width;
|
|
|
|
|
} else {
|
|
|
|
|
canvas.width = width;
|
|
|
|
|
canvas.height = height;
|
|
|
|
|
}
|
2018-05-09 16:41:07 +10:00
|
|
|
|
|
|
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
|
|
|
|
|
|
switch (orientation) {
|
2018-05-17 00:24:16 +10: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;
|
2018-05-09 16:41:07 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.drawImage(img, 0, 0, width, height);
|
|
|
|
|
|
|
|
|
|
canvas.toBlob(resolve, type);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const resizeImage = (img, type = 'image/png') => new Promise((resolve, reject) => {
|
|
|
|
|
const { width, height } = img;
|
|
|
|
|
|
2018-07-28 11:33:00 +10:00
|
|
|
|
const newWidth = Math.round(Math.sqrt(MAX_IMAGE_PIXELS * (width / height)));
|
|
|
|
|
const newHeight = Math.round(Math.sqrt(MAX_IMAGE_PIXELS * (height / width)));
|
2018-05-09 16:41:07 +10:00
|
|
|
|
|
2020-11-19 04:01:58 +11:00
|
|
|
|
checkCanvasReliability()
|
|
|
|
|
.then(getOrientation(img, type))
|
2018-05-09 16:41:07 +10:00
|
|
|
|
.then(orientation => processImage(img, {
|
|
|
|
|
width: newWidth,
|
|
|
|
|
height: newHeight,
|
|
|
|
|
orientation,
|
|
|
|
|
type,
|
|
|
|
|
}))
|
|
|
|
|
.then(resolve)
|
|
|
|
|
.catch(reject);
|
|
|
|
|
});
|
|
|
|
|
|
2020-04-25 20:27:29 +10:00
|
|
|
|
export default inputFile => new Promise((resolve) => {
|
2018-04-23 17:15:51 +10:00
|
|
|
|
if (!inputFile.type.match(/image.*/) || inputFile.type === 'image/gif') {
|
|
|
|
|
resolve(inputFile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadImage(inputFile).then(img => {
|
2018-07-28 11:33:00 +10:00
|
|
|
|
if (img.width * img.height < MAX_IMAGE_PIXELS) {
|
2018-04-23 17:15:51 +10:00
|
|
|
|
resolve(inputFile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 16:41:07 +10:00
|
|
|
|
resizeImage(img, inputFile.type)
|
|
|
|
|
.then(resolve)
|
|
|
|
|
.catch(() => resolve(inputFile));
|
2020-04-25 20:27:29 +10:00
|
|
|
|
}).catch(() => resolve(inputFile));
|
2018-04-23 17:15:51 +10:00
|
|
|
|
});
|