Skip to content

Instantly share code, notes, and snippets.

@Jeket
Forked from zmts/imageSize.md
Created October 25, 2021 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeket/fdb60adef5ea551706edb25961fb1fd3 to your computer and use it in GitHub Desktop.
Save Jeket/fdb60adef5ea551706edb25961fb1fd3 to your computer and use it in GitHub Desktop.
Get image width and height with JavaScript

Get image width and height with JavaScript

function imageSize (image) {
  return new Promise((resolve, reject) => {
    try {
      const fileReader = new FileReader()

      fileReader.onload = () => {
        const img = new Image()

        img.onload = () => {
          resolve({ width: img.width, height: img.height })
        }

        img.src = fileReader.result
      }

      fileReader.readAsDataURL(image)
    } catch (e) {
      reject(e)
    }
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment