Skip to content
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

[TypeError: Corrupt JPG, exceeded buffer limits] #37

Open
ktiniatros opened this issue Jan 13, 2015 · 16 comments
Open

[TypeError: Corrupt JPG, exceeded buffer limits] #37

ktiniatros opened this issue Jan 13, 2015 · 16 comments
Milestone

Comments

@ktiniatros
Copy link

Trying to calculate the following image throws an exception:

var imagesize = require("image-size");
var url = "http://www.niemanlab.org/images/renewalinvoice.jpg";
var needle = require('needle');

var stream = needle.get(url);
stream.on('readable', function( ) {
    var chunk;
    while( chunk = this.read() ) {
        info = imagesize(chunk);
        if( info ) {
            stream.end();
            console.log('streaming picture', info);
        }
    }
});
stream.on('end', function(){
    console.log('Stream ended');
});
@netroy
Copy link
Member

netroy commented Jan 13, 2015

@ktiniatros the 2 problems with this code are

  1. imagesize(chunk) throws for bad data, you need to catch that error.
  2. this.read() from needle would give you a chunk read from the stream, not all of the data in the stream since the beginning, so you are only passing in parts of the file, while what you really need to pass is a buffer containing everything from the beginning.

@ktiniatros
Copy link
Author

OK, got it now..

following your feedback, I fixed my code:

var totalChunk;
var length = 0;
stream.on('data', function( c ) {
    length += c.length;
var newBuffer = new Buffer(length);
    if(!totalChunk){
        totalChunk = new Buffer(c.length);
        c.copy(totalChunk, 0);
    }
    totalChunk.copy(newBuffer, 0);
    totalChunk = newBuffer;
    try {
        info = imagesize(totalChunk);
        if( info ) {
            stream.end();
            console.log('streaming picture', info);
        }
    } catch(e) {
        console.log(e);
    }

});
stream.on('end', function( ) {
    console.log('Stream ended');
});

My goal was actually to get the image dimensions without download the whole image. It works now. IF you have any better suggestion for getting dimensions without having to download the whole file, you are more than welcome :)

@techbirds
Copy link

10

var sizeOf = require('./lib/index');

var dimensions = sizeOf('xxx.JPG');
console.log(dimensions.width, dimensions.height);

error:
TypeError: Corrupt JPG, exceeded buffer limits

@paldepind
Copy link

I encountered this error as well on a quite small jpeg that seemed perfectly fine. I tested out GraphicsMagick as recommended in this SO answer. It returned the size correctly without errors for the given file.

@kbradl16
Copy link

FYI I had this error as well, but I found out it was because the images I was using were saved as CMYK file type instead of RGB. Once I changed the images to RGB the script worked.

@yangsibai
Copy link

Have the same error, I think we should use gm before it's fixed.

@holm
Copy link

holm commented Apr 4, 2016

We ran into the same problem with a CMYK JPG file. Having support for CMYK files would be great (or at least throw an error saying CMYK is not supported).

@apocist
Copy link

apocist commented Apr 7, 2016

I'm also getting the 'Corrupt JPG, exceeded buffer limits' error on CMYK JPG files.

However, if the file is resized to be small enough(<150px) the error no longer appears.

@ChenJiaH
Copy link

ChenJiaH commented Oct 17, 2016

I'm also getting the 'Corrupt JPG, exceeded buffer limits' error and the file is saved as RGB type.
But it's worked when I take it out alone.


It has been solved by changed the saved type of the image to decrease the size of the image.

@puzrin
Copy link

puzrin commented Dec 1, 2016

try https://github.com/nodeca/probe-image-size, it works with examples from this thread

@jgilligan113
Copy link

@kbradl16 This 100% was the case for me too
THANK YOU!

@fajardm
Copy link

fajardm commented Sep 11, 2017

me too

@Gatunox
Copy link

Gatunox commented Aug 10, 2018

i have the same issue, any solution?

@Anaphase
Copy link

@Gatunox use https://github.com/nodeca/probe-image-size instead

@terminatorheart
Copy link

i have the same problem too, if jpg is CMYK mode(which first four bytes are ffd8 ffe1), then image-size won't get right size

@AndrewKvalheim
Copy link

Is this the same as #34?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests