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

Error: Could not find MIME for Buffer <null> #775

Closed
codan84 opened this issue Aug 16, 2019 · 33 comments · Fixed by #789
Closed

Error: Could not find MIME for Buffer <null> #775

codan84 opened this issue Aug 16, 2019 · 33 comments · Fixed by #789
Labels
bug there is a bug in the way jimp behaves released This issue/pull request has been released.

Comments

@codan84
Copy link

codan84 commented Aug 16, 2019

This is the same as issue 643. However, the latter is closed and seems like it is dead despite people experiencing this issue still.

Expected Behavior

Jimp.read loads an image from a URL correctly.

Current Behavior

Trying to read some images from certain URLs fails, throwing Error: Could not find MIME for Buffer <null>

Failure Information (for bugs)

Whilst certain images load correctly, other consistently fail (a failing url will always fail, without exception).
Example failing URL: https://s-media-cache-ak0.pinimg.com/736x/c9/8f/e1/c98fe17dc7de72bb29c34a0c79ef5762.jpg

Trying to Jimp.read it 1000 times gives:

Called jimp.read() 1000 times.
  Failed: 1000.
  Succeeded: 0.

Steps to Reproduce

Repo with a simple script to reproduce the error: https://github.com/codan84/jimp-bug

Context

  • Jimp Version: 0.6.4
  • Operating System: OSX 10.14.3
  • Node version: v10.16.2

Failure Logs

{ Error: Could not find MIME for Buffer <null>
    at Jimp.parseBitmap (/Users/gruszd01/workspace/jimp-bug/node_modules/@jimp/core/dist/utils/image-bitmap.js:108:15)
    at Jimp.parseBitmap (/Users/gruszd01/workspace/jimp-bug/node_modules/@jimp/core/dist/index.js:498:32)
    at /Users/gruszd01/workspace/jimp-bug/node_modules/@jimp/core/dist/index.js:440:15
    at /Users/gruszd01/workspace/jimp-bug/node_modules/@jimp/core/dist/index.js:168:14
    at /Users/gruszd01/workspace/jimp-bug/node_modules/@jimp/core/dist/request.js:56:9
    at IncomingMessage.<anonymous> (/Users/gruszd01/workspace/jimp-bug/node_modules/phin/lib/phin.compiled.js:1:2100)
    at IncomingMessage.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19) methodName: 'constructor' }
@codan84
Copy link
Author

codan84 commented Aug 16, 2019

It's also worth mentioning that reading the same image using an external request library (I used axios) into a buffer and then loading that same image to jimp from a buffer works.
An example of that is in use-axios branch of the repo I linked above.
This would only mean that there's an issue with whatever is used for http requests in jimp...

Not working:

jimp.read(failingImageUrl)

Working:

axios({
    method: 'get',
    url: failingImageUrl,
    responseType: 'arraybuffer'
  })
  .then(function ({data: imageBuffer}) {
    return jimp.read(imageBuffer)
  })

@FridayJew
Copy link

FridayJew commented Aug 16, 2019

Have same problem. Problematic images downloaded on local disk with requst module make this error in jimp too. But are not have visible promplems.

@FridayJew
Copy link

I try use GM for edited this images. It don't read them too. Show this error:
Error: Command failed: gm composite: Improper image header
If i give created by me 100% good images , he dont worked.
Maybe i did something wrong , but if i use good images with JIMP, i have wanting results.

How i can get images from internet and use them with JIMP without errors ?

@hipstersmoothie hipstersmoothie added the bug there is a bug in the way jimp behaves label Sep 3, 2019
@lucafaggianelli
Copy link

I'm facing the same issue using node-vibrant, a library that uses Jimp and the axios trick is working like a charm!

@SaWey
Copy link
Contributor

SaWey commented Sep 4, 2019

This is happening when the image requested has a redirect response.
Your sample image is returning this response:
https://s-media-cache-ak0.pinimg.com/736x/c9/8f/e1/c98fe17dc7de72bb29c34a0c79ef5762.jpg

accept-ranges: bytes
content-length: 0
date: Wed, 04 Sep 2019 10:02:45 GMT
location: https://i.pinimg.com/736x/c9/8f/e1/c98fe17dc7de72bb29c34a0c79ef5762.jpg
retry-after: 0
status: 301
vary: Origin
x-cdn: fastly

jimp does not handle this, neither does phin <3.1.0
Either upgrading the phin library ( #ethan7g/phin#20) or handling this in the jimp core 'loadFromURL' should do the trick.

For now, I updated this code in 'core/dist/index.js to solve my problem:

function loadFromURL(options, cb) {
  (0, _request.default)(options, function (err, response, data) {
    if (err) {
      return cb(err);
    }

    if(response.headers.hasOwnProperty('location') ){
      options.url = response.headers['location'];
      return loadFromURL(options, cb);
    }

    if (_typeof(data) === 'object' && Buffer.isBuffer(data)) {
      return cb(null, data);
    }

    var msg = 'Could not load Buffer from <' + options.url + '> ' + '(HTTP: ' + response.statusCode + ')';
    return new Error(msg);
  });
}

@hipstersmoothie
Copy link
Collaborator

hipstersmoothie commented Sep 6, 2019

Please make a PR so we can all benefit from your code :) A comment helps very few but a PR can help many

@SaWey

@FridayJew
Copy link

jimp does not handle this, neither does phin <3.1.0
Either upgrading the phin library ( #ethanent/phin#20) or handling this in the jimp core 'loadFromURL' should do the trick.

But what i can't use downloaded images from this urls. Are not looking broken or something. Opened with any program for images viewer.

SaWey added a commit to SaWey/jimp that referenced this issue Sep 10, 2019
@SaWey SaWey mentioned this issue Sep 10, 2019
4 tasks
@SaWey
Copy link
Contributor

SaWey commented Sep 10, 2019

Please make a PR so we can all benefit from your code :) A comment helps very few but a PR can help many

@SaWey

Since there were 2 options I didn't know what way to go, but I have created a pull request which allows following redirects without upgrading phin.

@hipstersmoothie
Copy link
Collaborator

If phin supports this in a later version it think it makes sense to upgrade

@SaWey
Copy link
Contributor

SaWey commented Sep 13, 2019

It probably does make sense, but it also makes sense to always follow redirects by default.
Phin >=3.1.0 requires the flag followRedirects to be set in order to follow redirects.

hipstersmoothie pushed a commit that referenced this issue Nov 26, 2019
* Follow redirects

Fixes #775

* Follow redirects

Fixing lint error

* Follow redirects

Fix tests

* Added redirect test

* Fixed lint errors

* Skip redirect test in browser as we cannot mock a server

* Different test for browser

* Fixed browser detection
@hipstersmoothie
Copy link
Collaborator

🚀 Issue was released in v0.9.3 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 26, 2019
@hipstersmoothie
Copy link
Collaborator

🚀 Issue was released in v0.9.3 🚀

@ajeetku
Copy link

ajeetku commented Feb 14, 2020

Getting same in still now

Node 13.8.0
NPM 6.13.6
jimp: ^0.93

{ Error: Could not find MIME for Buffer
at Jimp.parseBitmap (../node_modules/@jimp/core/dist/utils/image-bitmap.js:106:15)
at Jimp.parseBitmap (../node_modules/@jimp/core/dist/index.js:521:32)
at new Jimp (../node_modules/@jimp/core/dist/index.js:474:13)
at _construct (../node_modules/@babel/runtime/helpers/construct.js:30:21)
at ../node_modules/@jimp/core/dist/index.js:1016:32
at new Promise ()
at Function.Jimp.read (../node_modules/@jimp/core/dist/index.js:1015:10)
{
methodName: 'constructor'
}

@natuan62
Copy link

I still get this error

Node v13.5.0
NPM v6.13.7
jimp ^0.93

Add logo error Error: Could not find MIME for Buffer <null>
    at Jimp.parseBitmap (C:\Users\burakkujakku\Documents\GIT\conect\conect-api\node_modules\@jimp\core\dist\utils\image-bitmap.js:106:15)
    at Jimp.parseBitmap (C:\Users\burakkujakku\Documents\GIT\conect\conect-api\node_modules\@jimp\core\dist\index.js:521:32)
    at C:\Users\burakkujakku\Documents\GIT\conect\conect-api\node_modules\@jimp\core\dist\index.js:463:15
    at C:\Users\burakkujakku\Documents\GIT\conect\conect-api\node_modules\@jimp\core\dist\index.js:205:14
    at C:\Users\burakkujakku\Documents\GIT\conect\conect-api\node_modules\@jimp\core\dist\request.js:66:9
    at IncomingMessage.<anonymous> (C:\Users\burakkujakku\Documents\GIT\conect\conect-api\node_modules\phin\lib\phin.compiled.js:1:2100)
    at IncomingMessage.emit (events.js:317:22)
    at endReadableNT (_stream_readable.js:1215:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  methodName: 'constructor'
}

My codes use to add logo-img to picture

Jimp.read(imgRaw)
                .then(tpl =>
                    Jimp.read(imgLogo)
                    .then(logoTpl => {
                        let axios_x = tpl.bitmap.width;
                        let axios_y = tpl.bitmap.height;
                        logoTpl.opacity(0.2);
                        return tpl.composite(logoTpl, axios_x - 134, axios_y - 86, [Jimp.BLEND_DESTINATION_OVER, 0.2, 0.2]);
                    })
                )
                .then(tpl => {
                    tpl.quality(100).write(imgExported)
                })
                .then(tpl => {
                    console.log('exported file: ' + imgExported);
                })
                .catch(err => {
                    console.error('Add logo error', err);
                })

@verveguy
Copy link

I'm also seeing this "Error: Could not find MIME for Buffer " error sporadically one time in about 5 or so. In my case, the images are local files (no network access involved). I'm working to extract a small test case from the larger test suite to submit for repro. Current versions of jimp 0.10.2 and node 13.12.0

@weiyunpeng
Copy link

I'm also seeing this "Error: Could not find MIME for Buffer " error. the images are buffers.

@verveguy
Copy link

So, after much digging and experimentation I rewrote all of my code that creates the local files and all of the invocations of jimp to process them to absolutely guarantee there was no possibility of any async race conditions or any possibility of reusing intermediate files. That appears to have resolved my issues as far as I can tell under test.

@StevenVerheyen
Copy link

I also face this issue.. Any news on this old issue?

@garrettlove8
Copy link

Same here...
Jimp 0.16.1
Node 10.13.0

@verveguy
Copy link

After rewriting all of my code that calls jimp as noted above, I no longer have these problems. I believe I can now confirm that the issues I was having were race conditions between jimp code and the file system.

@marty1982
Copy link

marty1982 commented Mar 4, 2021

Hi all !

I'm facing the same problem with JIMP.
I have Node-red dockerized and this is what i got when i try to upload an image from URL.

image

Also i have this JIMP version inside my package.json file.
image

Any suggestions?.

Regards!

@anphetamina
Copy link

Facing the same issue trying to read from a local file.
JIMP version "jimp": "^0.16.1",

@marty1982
Copy link

Facing the same issue trying to read from a local file.
JIMP version "jimp": "^0.16.1",

Hi !

I could upgrade the JIMP version and now i can upload the image correctly.

@homerjam
Copy link

I'm also getting this issue, perhaps worth re-opening?

Error: Could not find MIME for Buffer <null>
    at Jimp.call (/Users/jameshomer/Projects/fokus/node_modules/@jimp/core/src/utils/image-bitmap.js:156:15)
    at Jimp.parseBitmap (/Users/jameshomer/Projects/fokus/node_modules/@jimp/core/src/index.js:400:17)
    at new Jimp (/Users/jameshomer/Projects/fokus/node_modules/@jimp/core/src/index.js:355:12)

@jakubmz
Copy link

jakubmz commented Apr 23, 2021

Maybe this is of help to others. I got this error and solved it in my script. In my case it was an async bug in my code. When doing batch processing I was trying to run jimp.read() on an image file that was not found, incomplete or empty at the run time.

@tbanj
Copy link

tbanj commented Oct 1, 2021

@codan84 your solution works for me thanks so much

@cdric
Copy link

cdric commented Nov 17, 2021

I'm also having this issue while fetching a bunch of file locally! The solution from @codan84 only work as HTTP request, the axios library doesn't help otherwise.

@jakubmz , would you mind providing a excerpt of your script that show how you have been solving the async issue?

@Oyinbayode
Copy link

It's also worth mentioning that reading the same image using an external request library (I used axios) into a buffer and then loading that same image to jimp from a buffer works. An example of that is in use-axios branch of the repo I linked above. This would only mean that there's an issue with whatever is used for http requests in jimp...

Not working:

jimp.read(failingImageUrl)

Working:

axios({
    method: 'get',
    url: failingImageUrl,
    responseType: 'arraybuffer'
  })
  .then(function ({data: imageBuffer}) {
    return jimp.read(imageBuffer)
  })

Thank You

This worked for me

@frank3stein
Copy link

frank3stein commented Mar 16, 2023

Instead of using axios, you can use node-fetch to turn the image into a buffer and then feed it to jimp

import fetch from "node-fetch";

      const res = await fetch(inputURL);
      // I turn the image to a buffer and then to a Jimp image, otherwise some images are not processed
      const data = await res.arrayBuffer();
      const photo = await Jimp.read(data);

This solved my issue.

@bruceceng
Copy link

I just had this error, and in my case the solution was simple. I was using a command line tool to generate a png from a PDF, and for some images it worked and others it broke, but it turned out I just needed to add a 1 second delay between generating the png and trying to load it. I guess the generation program finished before the png was entirely ready or something like that (after finding a fix, I did not follow up on the details).

@verveguy
Copy link

verveguy commented Apr 13, 2023 via email

@KernelDeimos
Copy link

I maintain a server that produced this issue in production 79 times within a few days on an endpoint handling file uploads (so the buffer is coming from an uploaded file). Unfortunately I can't provide any sample data since it's in production, but if there's anything I can do to help narrow down the issue please let me know. It doesn't seem to be causing us major issues (just sometimes a thumbnail doesn't show, I presume), so it's not our priority, but I'd be happy to help if I can.

@hipstersmoothie
Copy link
Collaborator

If you have a solution I have the ability to merge!

kathrynijones added a commit to kathrynijones/aws-image-processing-microservice that referenced this issue Jan 29, 2024
… retrieve the image from the given URL. This buffers the image data which is then given in the input argument to the Jimp.read() function. This fixes a common issue with this function when the URL is given as the input argument where it fails to retrieve the image, resulting in the error, "Could not find MIME for Buffer", see jimp-dev/jimp#775. In this link there is a suggestion to use axios to fix the issue, this suggestion has been implemented here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug there is a bug in the way jimp behaves released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging a pull request may close this issue.