Skip to content

Instantly share code, notes, and snippets.

@xtpor
Created May 8, 2024 05:49
Show Gist options
  • Save xtpor/aa13070375aec41a851bf5427ed0888a to your computer and use it in GitHub Desktop.
Save xtpor/aa13070375aec41a851bf5427ed0888a to your computer and use it in GitHub Desktop.
<!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