Browse Source

Fix lint and test error

pull/482/head
Jerome Wu 4 years ago
parent
commit
6481256f5e
  1. 28
      src/worker/browser/loadImage.js
  2. 5
      src/worker/node/loadImage.js

28
src/worker/browser/loadImage.js

@ -1,5 +1,5 @@
const resolveURL = require('resolve-url'); const resolveURL = require('resolve-url');
const blueimp = require('blueimp-load-image'); const blueimpLoadImage = require('blueimp-load-image');
/** /**
* readFromBlobOrFile * readFromBlobOrFile
@ -23,9 +23,16 @@ const readFromBlobOrFile = blob => (
const fixOrientationFromUrlOrBlobOrFile = blob => ( const fixOrientationFromUrlOrBlobOrFile = blob => (
new Promise((resolve) => { new Promise((resolve) => {
blueimp(blob, (img) => img.toBlob(resolve, 'image/jpeg'), {orientation: true}) blueimpLoadImage(
blob,
img => img.toBlob(resolve),
{
orientation: true,
canvas: true,
},
);
}) })
) );
/** /**
* loadImage * loadImage
@ -46,9 +53,13 @@ const loadImage = async (image) => {
data = atob(image.split(',')[1]) data = atob(image.split(',')[1])
.split('') .split('')
.map(c => c.charCodeAt(0)); .map(c => c.charCodeAt(0));
} else if (image.endsWith('.pbm')) {
const resp = await fetch(resolveURL(image));
data = await resp.arrayBuffer();
} else { } else {
image = await fixOrientationFromUrlOrBlobOrFile(resolveURL(image)); data = await readFromBlobOrFile(
data = await readFromBlobOrFile(image); await fixOrientationFromUrlOrBlobOrFile(resolveURL(image)),
);
} }
} else if (image instanceof HTMLElement) { } else if (image instanceof HTMLElement) {
if (image.tagName === 'IMG') { if (image.tagName === 'IMG') {
@ -66,8 +77,11 @@ const loadImage = async (image) => {
}); });
} }
} else if (image instanceof File || image instanceof Blob) { } else if (image instanceof File || image instanceof Blob) {
image = await fixOrientationFromUrlOrBlobOrFile(image); let img = image;
data = await readFromBlobOrFile(image); if (!image.name.endsWith('.pbm')) {
img = await fixOrientationFromUrlOrBlobOrFile(img);
}
data = await readFromBlobOrFile(img);
} }
return new Uint8Array(data); return new Uint8Array(data);

5
src/worker/node/loadImage.js

@ -33,9 +33,8 @@ module.exports = async (image) => {
} }
try { try {
data = (await jo.rotate(data, {quality: 100})).buffer data = (await jo.rotate(data, { quality: 100 })).buffer;
} catch (_) { } catch (_) {} /* eslint-disable-line */
}
return new Uint8Array(data); return new Uint8Array(data);
}; };

Loading…
Cancel
Save