Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Last active July 21, 2022 11:31
Show Gist options
  • Save tyler-austin/a1f609a8a5880cf06703edf1a06d0485 to your computer and use it in GitHub Desktop.
Save tyler-austin/a1f609a8a5880cf06703edf1a06d0485 to your computer and use it in GitHub Desktop.
JSZip Open image files
// https://github.com/Stuk/jszip/issues/399
JSZip.loadAsync(data).then(function (zip) {
var re = /(.jpg|.png|.gif|.ps|.jpeg)$/;
var promises = Object.keys(zip.files).filter(function (fileName) {
// don't consider non image files
return re.test(fileName.toLowerCase());
}).map(function (fileName) {
var file = zip.files[fileName];
return file.async("blob").then(function (blob) {
return [
fileName, // keep the link between the file name and the content
URL.createObjectURL(blob) // create an url. img.src = URL.createObjectURL(...) will work
];
});
});
// `promises` is an array of promises, `Promise.all` transforms it
// into a promise of arrays
return Promise.all(promises);
}).then(function (result) {
// we have here an array of [fileName, url]
// if you want the same result as imageSrc:
return result.reduce(function (acc, val) {
acc[val[0]] = val[1];
return acc;
}, {});
}).catch(function (e) {
console.error(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment