New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why is it ok to stop downloading JPEG image only after a few KB? #258
Comments
They're both right. The answer doesn't say that you have to parse the entire file, just until you find the segment. Anyway, here's a full demo code that's working for me, Node 10+. You only need const url = require('url')
const http = require('https')
const sizeOf = require('image-size')
const imgUrl = 'https://i.imgur.com/O2HfwuZ.jpg'
const options = url.parse(imgUrl)
async function getStreamImageSize(stream) {
const chunks = []
for await (const chunk of stream) {
chunks.push(chunk)
try {
return sizeOf(Buffer.concat(chunks));
} catch (error) {/* Not ready yet */}
}
return sizeOf(Buffer.concat(chunks));
}
http.get(options, async stream => {
console.log(await getStreamImageSize(stream));
}); This is so fast that in my tests it works with the very first chunk. |
I was wondering if we need to buffer the image at all... Couldn't we get the image dimensions by using its metadata? On exif, for instance... |
Exif is often much further into the image data than the dimensions info block; so you would end up downloading more of the image buffer than you need to |
Co-authored-by: Federico Brigante <me@fregante.com> image-size/image-size#258 (comment)
According to the example it's ok to download only a few KB in order to get image size/dimensions. But according to this stackoverflow answer in order to get JPEG width/height:
Am I missing something or your library has a workaround?
The text was updated successfully, but these errors were encountered: