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

Jpeg load error #966

Closed
qertis opened this issue Aug 18, 2017 · 6 comments
Closed

Jpeg load error #966

qertis opened this issue Aug 18, 2017 · 6 comments

Comments

@qertis
Copy link

qertis commented Aug 18, 2017

Issue or Feature

ctx.drawImage(img, 0, 0, img.width, img.height);
        ^

Error: Image given has not completed loading
    at Error (native)
    at C:\Users\iavto\WebstormProjects\CanvasLevels\index.js:22:9
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:445:3)

Steps to Reproduce

const Canvas = require('canvas');
const fs = require('fs')
var p = __dirname + '\\images\\noplan.jpg';
fs.readFile(p, function(err, data) {
    if (err) throw err;
    var img = new Canvas.Image;
    img.src = data;
    var canvas = new Canvas(img.width, img.height);
    var ctx = canvas.getContext('2d');
    ctx.drawImage(img, 0, 0, img.width, img.height);
})

Your Environment

  • Version of node-canvas (e.g. 1.4.0): 1.6.6
  • Environment (e.g. node 4.2.0 on Mac OS X 10.8): Windows 10
@qertis
Copy link
Author

qertis commented Aug 18, 2017

When I load *.png file all works.

@LinusU
Copy link
Collaborator

LinusU commented Aug 19, 2017

Can you attach an onerror handler to the image, and log any potential error?

img.onerror = (err) => console.error(err.stack)

@zbjornson
Copy link
Collaborator

zbjornson commented Sep 14, 2017

I think this is the same as #1001, and I just posted a solution there: The error doesn't happen (for me at least) if you wait for the img.onload event.

It's odd that it only happens for jpeg; it suggests there's something async but I'm not seeing it. Plus, apparently the onerror and onload listeners must be registered before the img.src = ... line, which also suggests that it's sync.

(Aside: that the order matters is sort of unusual for node.js. Usually events are triggered after a process.nextTick so that you can register them after the img.src = ..., but this is also a bit of an unusual scenario.)

Ignore that -- I was mistaken.

@cvalente56
Copy link

I was having the same issue, and spent the last couple of days trying find out why.

For me the issue was due the this change to the /util/has_lib script.

I’m running my application from within a custom docker container with all of the native decencies compiled to /my-libs (in which node-canvas v1.6.9 runs just fine)

The new javascript based script fails the hasLdconfig() call and then fails to manually find the presence of libjpeg.so in the "common library locations”

ldconfig would have found the dependency just fine.

root@b775ecf2e41d:/my-libs/lib/pkgconfig# ldconfig -p 2>/dev/null | grep -E "jpeg" libjpeg.so.9 (libc6,x86-64) => /my-libs/lib/libjpeg.so.9 libjpeg.so (libc6,x86-64) => /my-libs/lib/libjpeg.so

So, a simple fix for me (in the meantime) was to just add the following symbolic links.

ln -s /my-libs/lib/libjpeg.so /usr/lib/libjpeg.so ln -s /my-libs/lib/libgif.so /usr/lib/libgif.so

@zbjornson
Copy link
Collaborator

@qertis does this error still happen if you wait for the onload event?

const Canvas = require('canvas');
const fs = require('fs')
var p = __dirname + '\\images\\noplan.jpg';
fs.readFile(p, function(err, data) {
    if (err) throw err;
    var img = new Canvas.Image;
    img.onload = () => {
        var canvas = new Canvas(img.width, img.height);
        var ctx = canvas.getContext('2d');
        ctx.drawImage(img, 0, 0, img.width, img.height);
    };
    img.onerror = console.log;
    img.src = data;
})

@zbjornson
Copy link
Collaborator

Closing since there's not enough info to do anything, and was possibly needing to wait for onload.

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

No branches or pull requests

4 participants