Skip to content

Instantly share code, notes, and snippets.

@zmts
Created June 6, 2020 16:48
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save zmts/3388e16fe6b2983a541e91c40fcc2eb3 to your computer and use it in GitHub Desktop.
Save zmts/3388e16fe6b2983a541e91c40fcc2eb3 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)
    }
  })
}
@bart-antczak
Copy link

Works perfect!

@gatochuqui
Copy link

please put this example in context(in an html page). showing <input..multiple..image only etc. that would be very helpful, thank you

@akshayrajp
Copy link

Thanks a lot!

@Pnlvfx
Copy link

Pnlvfx commented Jun 5, 2022

I always receive that image is undefined when I add readAsDataUrl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment