resizeImage: skip canvas reliability check for now (it's unreliable)

This commit is contained in:
Alex Gleason 2021-11-15 13:41:33 -06:00
parent 9a42ebb41f
commit 30a9de817a
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -43,41 +43,41 @@ const dropOrientationIfNeeded = (orientation) => new Promise(resolve => {
// Some browsers don't allow reading from a canvas and instead return all-white // 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 // or randomized data. Use a pre-defined image to check if reading the canvas
// works. // works.
const checkCanvasReliability = () => new Promise((resolve, reject) => { // const checkCanvasReliability = () => new Promise((resolve, reject) => {
switch(_browser_quirks['canvas-read-unreliable']) { // switch(_browser_quirks['canvas-read-unreliable']) {
case true: // case true:
reject('Canvas reading unreliable'); // reject('Canvas reading unreliable');
break; // break;
case false: // case false:
resolve(); // resolve();
break; // break;
default: // default:
// 2×2 GIF with white, red, green and blue pixels // // 2×2 GIF with white, red, green and blue pixels
const testImageURL = // const testImageURL =
'data:image/gif;base64,R0lGODdhAgACAKEDAAAA//8AAAD/AP///ywAAAAAAgACAAACA1wEBQA7'; // 'data:image/gif;base64,R0lGODdhAgACAKEDAAAA//8AAAD/AP///ywAAAAAAgACAAACA1wEBQA7';
const refData = // const refData =
[255, 255, 255, 255, 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255]; // [255, 255, 255, 255, 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255];
const img = new Image(); // const img = new Image();
img.onload = () => { // img.onload = () => {
const canvas = document.createElement('canvas'); // const canvas = document.createElement('canvas');
const context = canvas.getContext('2d'); // const context = canvas.getContext('2d');
context.drawImage(img, 0, 0, 2, 2); // context.drawImage(img, 0, 0, 2, 2);
const imageData = context.getImageData(0, 0, 2, 2); // const imageData = context.getImageData(0, 0, 2, 2);
if (imageData.data.every((x, i) => refData[i] === x)) { // if (imageData.data.every((x, i) => refData[i] === x)) {
_browser_quirks['canvas-read-unreliable'] = false; // _browser_quirks['canvas-read-unreliable'] = false;
resolve(); // resolve();
} else { // } else {
_browser_quirks['canvas-read-unreliable'] = true; // _browser_quirks['canvas-read-unreliable'] = true;
reject('Canvas reading unreliable'); // reject('Canvas reading unreliable');
} // }
}; // };
img.onerror = () => { // img.onerror = () => {
_browser_quirks['canvas-read-unreliable'] = true; // _browser_quirks['canvas-read-unreliable'] = true;
reject('Failed to load test image'); // reject('Failed to load test image');
}; // };
img.src = testImageURL; // img.src = testImageURL;
} // }
}); // });
const getImageUrl = inputFile => new Promise((resolve, reject) => { const getImageUrl = inputFile => new Promise((resolve, reject) => {
if (window.URL && URL.createObjectURL) { if (window.URL && URL.createObjectURL) {
@ -162,8 +162,10 @@ const resizeImage = (img, inputFile, maxPixels) => new Promise((resolve, reject)
const newWidth = Math.round(Math.sqrt(maxPixels * (width / height))); const newWidth = Math.round(Math.sqrt(maxPixels * (width / height)));
const newHeight = Math.round(Math.sqrt(maxPixels * (height / width))); const newHeight = Math.round(Math.sqrt(maxPixels * (height / width)));
checkCanvasReliability() // Skip canvas reliability check for now (it's unreliable)
.then(getOrientation(img, type)) // checkCanvasReliability()
// .then(getOrientation(img, type))
getOrientation(img, type)
.then(orientation => processImage(img, { .then(orientation => processImage(img, {
width: newWidth, width: newWidth,
height: newHeight, height: newHeight,