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.

Error trying to get the dimensions of an .jpeg image from a buffer

See original GitHub issue

https://github.com/image-size/image-size/blob/175bca5d57ceb024ecd307f7a1d70e03fafec205/lib/detector.ts#L5-L17

This constant doesn’t include .jpeg (0x41), and doesn’t want to work with a buffer from a downloaded .jpeg image… Gives the error: unsupported file type: undefined (file: undefined)(...). The buffer was a buffer, and not undefined.

I also see it is missing some other file types.

Edit: I thought all JPEG types was supported. Maybe this isn’t a bug. I read in your code that the file has to be 0xff, if not the table was corrupt… But I don’t think that is true. The picture I am trying to read is fine.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ravo10commented, Nov 16, 2019

@netroy I want to report back. I tried again with a URL (didn’t need a buffer…) from local tmp. storage, and that worked for me atleast it seems !

0reactions
ravo10commented, Nov 5, 2019

@ravo10 i don’t get any errors on any of the 4 images from this zip file. can you send me a file that most definitely is throwing this error?

Sorry for wasting your time. I am not sure why I am getting errors… Those .jpeg-pictures are the only one I’ve tried, and won’t work for me. As said, I am using a buffer recived from a download-URL. I am using Google Cloud Storage. I can give you the snippet of code I am using server side with Node.JS 8 (Google Firebase Functions) to try and get the dimensions. (I also get error when trying to use exif-parser (Invalid JPEG section offset at Object.parseSections (…))).

I might for now just get the dimensions locally with JS and storing the original dimensions in the pictures custom metadata on upload, for later auto. resizing and compression on serverside.

TypeScript:

async hentUtFilPrewKjelde(fil): Promise<string> {
    const LESER = new FileReader();

    const KJELDE = await new Promise<string>(resolve => {
        LESER.addEventListener('load', (evt: any) => {
            resolve(evt.target.result);
        });

        // Start avlesning
        LESER.readAsDataURL(fil);
    });

    return KJELDE;
}

The way I am using (that works) on client side:

// Hent metadata frå fil...
mData = Object.assign(mData, await (async () => {
  return await new Promise<{
    ['original-breidde']: string,
    ['original-høgde']: string
  }>(async (resolve) => {
    const bileteMetadata = new Image();
    bileteMetadata.addEventListener('load', () => {
      // Ferdig
      resolve({
        ['original-breidde']: bileteMetadata.width + 'px',
        ['original-høgde']: bileteMetadata.height + 'px'
      });
    });

    // Sett
    const adresse = await this.asyncFilerPrewPipe.hentUtFilPrewKjelde(fil); // Returns a buffer
    bileteMetadata.src = adresse;
  });
})());

The logic I try to use on server:

const bileteDimensions = await new Promise<{
    h: number,
    w: number
} | null>(resolve => {
    if (downloadURL) {
        https.get(downloadURL, response => {
            const chunks: Buffer[] = [];

            response
                .on('data', (chunk: Buffer) => {
                    chunks.push(chunk);
                })
                .on('end', () => {
                    const buffer = Buffer.concat(chunks);
                    // Outputs a buffer
                    console.log(buffer);

                    setTimeout(() => {
                        const dimensions: {
                            width: number,
                            height: number
                        } = sizeOf(buffer);
                        console.log(dimensions);

                        setTimeout(() => {
                            // Done
                            resolve({
                                h: dimensions.width,
                                w: dimensions.height
                            })
                        }, 700);
                    }, 300);
                })
                .on('error', (e) => {
                    console.error(e);
                });
        });
    } else {
        // Done (no URL)
        resolve(null);
    }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Ideal image sizes and formats for your posts
In this guide, we'll share basic information about the recommended sizes and formats you should use for your images when crafting your posts ......
Read more >
Sharp unable to read file buffer - node.js
1 Answer 1 · Maybe check the type of your provided input req.files.image.data one more time. Is it really a not corrupted image...
Read more >
Buffer size is not sufficient - MSDN - Microsoft
In my application. From one place I am reading the file (image) and trying to get it displayed in wpf screen as WriteableBitmap....
Read more >
image - Rust
Read a tuple containing the (width, height) of the image located at the specified path. This is faster than fully loading the image...
Read more >
OpenCV: Image file reading and writing
The function imdecode reads an image from the specified buffer in the memory. ... when the function is called repeatedly for images of...
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