question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Read image dimensions

See original GitHub issue

Hello, and thanks for the great lib!

Is there a way to access the image dimensions within Uppy without need to go through a Blob -> FileReader -> Image conversion? I was unable to find a width/height metadata fields on the objects returned by the upload-success and complete events.

I’m currently doing this like so:

// convert the blob URL to an object
let objFile = await fetch(elem.preview)
let objBlob = await objFile.blob()

// convert it to a data URI
let reader = new FileReader()
reader.readAsDataURL(objBlob)

reader.onload = e => {
  // retrieve the image dimensions
  let image = new Image()
  image.src = e.target.result

  image.onload = (loadEvent) => {
    // all done
    resolve({
      thumbnail: e.target.result,
      url: elem.uploadURL,
      width: loadEvent.path[0].width,
      height: loadEvent.path[0].height,
      aspectRatio: loadEvent.path[0].width / loadEvent.path[0].height
    })
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
goto-bus-stopcommented, Feb 28, 2019

Indeed uppy doesn’t have these built in. It would be possible to do with a plugin.

For the Blob/FileReader dance, the easier thing to do is to use URL.createObjectURL, which saves a few steps:

uppy.on('file-added', (file) => {
  const data = file.data // is a Blob instance
  const url = URL.createObjectURL(data)
  const image = new Image()
  image.src = url
  image.onload = () => {
    uppy.setFileMeta(file.id, { width: image.width, height: image.height })
    URL.revokeObjectURL(url)
  }
})

As for the .preview property, it’s already a blob URL, which can be assigned directly to an Image’s .src:

const image = new Image()
image.src = file.preview
image.onload = () => { ... }

Then you do not need the URL.createObjectURL and URL.revokeObjectURL calls.

4reactions
HeiVaskcommented, Feb 26, 2019

Related: It would be also nice to add the image dimensions as a restriction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image Size Finder | Free Tool - PosterBurner
Find the size and pixel depth of your image. This free tool shows you the height, width, and pixel per inch of your...
Read more >
How to find image size & dimensions info on your computer
On a PC (Windows 10):​​ Open Windows Explorer and find the image you want to check. The dimensions and file size appear in...
Read more >
Determine an image's file size and dimensions
Click Finder on your Dock. Image of the Finder icon. · Find the image you want to check. · Control+click (ctrl+click) your image....
Read more >
How to get image size (height & width) using JavaScript?
You can programmatically get the image and check the dimensions using Javascript... const img = new Image(); img.onload = function() { alert(this.width + ......
Read more >
Get image size (width, height) with Python, OpenCV, Pillow (PIL)
This article describes how to get the image size (width, height) in Python with OpenCV and Pillow (PIL).The image size can be obtained...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found