Skip to content

Instantly share code, notes, and snippets.

@materkel
Created February 7, 2022 22:18
Show Gist options
  • Save materkel/df9ba660d360f74800bca06213fe7f3c to your computer and use it in GitHub Desktop.
Save materkel/df9ba660d360f74800bca06213fe7f3c to your computer and use it in GitHub Desktop.
function fetchFileDimensionsAsBlob(file: File) {
const src = URL.createObjectURL(file);
const image = new Image();
image.onload = function() {
resolve({
src,
dimensions: {
width: image.width,
height: image.height,
},
});
};
image.src = src;
}
function fetchFileDimensionsAsBase64(file: File) {
return new Promise((resolve) => {
const fileReader = new FileReader();
fileReader.onload = (fileEvent) => {
const src = fileEvent.target.result as string;
const image = new Image();
image.onload = function() {
resolve({
src,
dimensions: {
width: image.width,
height: image.height,
},
});
};
image.src = src;
};
fileReader.readAsDataURL(file);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment