forked from external-repos/squoosh
Caught a bit of repetition in our utils
This commit is contained in:
@@ -57,16 +57,32 @@ export async function canvasEncode(data: ImageData, type: string, quality?: numb
|
|||||||
return blob;
|
return blob;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function decodeImage(url: string): Promise<HTMLImageElement> {
|
||||||
|
const img = new Image();
|
||||||
|
img.decoding = 'async';
|
||||||
|
img.src = url;
|
||||||
|
const loaded = new Promise((resolve, reject) => {
|
||||||
|
img.onload = () => resolve();
|
||||||
|
img.onerror = () => reject(Error('Image loading error'));
|
||||||
|
});
|
||||||
|
|
||||||
|
if (img.decode) {
|
||||||
|
// Nice off-thread way supported in Safari/Chrome.
|
||||||
|
// Safari throws on decode if the source is SVG.
|
||||||
|
// https://bugs.webkit.org/show_bug.cgi?id=188347
|
||||||
|
await img.decode().catch(() => null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always await loaded, as we may have bailed due to the Safari bug above.
|
||||||
|
await loaded;
|
||||||
|
return img;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to load the given URL as an image.
|
* Attempts to load the given URL as an image.
|
||||||
*/
|
*/
|
||||||
export function canDecodeImage(data: string): Promise<boolean> {
|
export function canDecodeImage(url: string): Promise<boolean> {
|
||||||
return new Promise((resolve) => {
|
return decodeImage(url).then(() => true, () => false);
|
||||||
const img = document.createElement('img');
|
|
||||||
img.src = data;
|
|
||||||
img.onload = _ => resolve(true);
|
|
||||||
img.onerror = _ => resolve(false);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function blobToArrayBuffer(blob: Blob): Promise<ArrayBuffer> {
|
export function blobToArrayBuffer(blob: Blob): Promise<ArrayBuffer> {
|
||||||
@@ -108,24 +124,7 @@ export async function blobToImg(blob: Blob): Promise<HTMLImageElement> {
|
|||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const img = new Image();
|
return await decodeImage(url);
|
||||||
img.decoding = 'async';
|
|
||||||
img.src = url;
|
|
||||||
const loaded = new Promise((resolve, reject) => {
|
|
||||||
img.onload = () => resolve();
|
|
||||||
img.onerror = () => reject(Error('Image loading error'));
|
|
||||||
});
|
|
||||||
|
|
||||||
if (img.decode) {
|
|
||||||
// Nice off-thread way supported in Safari/Chrome.
|
|
||||||
// Safari throws on decode if the source is SVG.
|
|
||||||
// https://bugs.webkit.org/show_bug.cgi?id=188347
|
|
||||||
await img.decode().catch(() => null);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always await loaded, as we may have bailed due to the Safari bug above.
|
|
||||||
await loaded;
|
|
||||||
return img;
|
|
||||||
} finally {
|
} finally {
|
||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user