Created
May 8, 2024 05:49
-
-
Save xtpor/aa13070375aec41a851bf5427ed0888a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<input id="file-upload" type="file"> | |
<script> | |
function getImageDimension(file, fn) { | |
const url = URL.createObjectURL(file) | |
const elem = document.createElement("img") | |
elem.src = url | |
elem.onload = () => { | |
const dimension = { w: elem.width, h: elem.height } | |
elem.src = null | |
URL.revokeObjectURL(url) | |
fn(dimension) | |
} | |
} | |
const input = document.querySelector("#file-upload") | |
input.onchange = (e) => { | |
const imageFile = e.target.files[0] | |
getImageDimension(imageFile, (wh) => { | |
console.log("image dimension", wh) | |
}) | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment