jimp icon indicating copy to clipboard operation
jimp copied to clipboard

No matching constructor overloading was found. Please see the docs for how to call the Jimp constructor.

Open lsq4590578 opened this issue 4 years ago • 9 comments

Jimp version :0.16.0 is wrong version :0.9.6 is ok

how to resolve this problem? i has the same question /** * * @param width 图片宽度 * @param height 图片高度 * @param data 图片的流数据 * @returns void 无返回值,但会生成一个图片 */ saveImage(width: number, height: number, data: any, dest: string, flip: boolean) { return new Promise((resolve, reject) => { new Jimp({ width: width, height: height, data: data }, (error: any, image: any) => { console.log(error); console.log(image); if (error) { reject(error) return; } else { image.flip(false, flip) //是否翻转 horz, vert,传过来的是horz还是vert为true //@ts-ignore image.write(dest) //文件名以及存放路径 resolve(); } }) }) }

lsq4590578 avatar Jan 13 '22 02:01 lsq4590578

According to the source code https://github.com/oliver-moran/jimp/blob/53ff9d1266207f7f674233f465ec358274510511/packages/core/src/index.js#L181 This constructor is a pseudo constructor that allows the user to define their own constructors

Since you don't register any custom constructors it won't work.

You probably need to use Jimp.<method>({ ... }) instead of calling new Jimp({ ... })

nopeless avatar Jan 28 '22 03:01 nopeless

Same here. Documentation is wrong.

jardicc avatar Feb 28 '22 12:02 jardicc

@jardicc can you give me a link to that documentation

nopeless avatar Mar 01 '22 14:03 nopeless

@jardicc can you give me a link to that documentation

Sure: https://www.npmjs.com/package/jimp#creating-new-images

This is false

new Jimp({ data: buffer, width: 1280, height: 768 }, (err, image) => {
  // this image is 1280 x 768, pixels are loaded from the given buffer.
});

jardicc avatar Mar 01 '22 15:03 jardicc

@jardicc I'll look into the source and figure out the issue

nopeless avatar Mar 01 '22 16:03 nopeless

https://github.com/oliver-moran/jimp/blob/53ff9d1266207f7f674233f465ec358274510511/packages/core/src/index.js#L309

Is responsible for checking whether the object passed in has valid data, width and height

I'm starting to think that one of these fields is invalid in the original post. Could you give a minimal reproducible example?

nopeless avatar Mar 01 '22 16:03 nopeless

I Already workarounded it by making empty image using different overload. And then once it is created... setting bitmap values directly. So I don't have that code now and I don't have time to write it. But I think anything you would try should fail if you do that according documentation.

jardicc avatar Mar 01 '22 16:03 jardicc

note: Documentation shows object as first argument.

jardicc avatar Mar 01 '22 16:03 jardicc

@jardicc sorry for the late reply. I totally forgot about this issue.

You just used the data parameter wrong. After a quick debugging, I found out that it was expecting a data buffer with no metadata. The code below will show you the difference.

Retrieved from

https://github.com/oliver-moran/jimp/blob/53ff9d1266207f7f674233f465ec358274510511/packages/core/src/index.js#L100-L113

function isRawRGBAData(obj) {
  // This part will log `false` when you pass in raw .png buffer
  console.log((
    obj.data.length === obj.width * obj.height * 4
    || obj.data.length === obj.width * obj.height * 3
   ));
  return obj &&
   (0, _typeof2["default"])(obj) === 'object'
   && typeof obj.width === 'number'
   && typeof obj.height === 'number'
   && (
     Buffer.isBuffer(obj.data)
     || obj.data instanceof Uint8Array
     || typeof Uint8ClampedArray === 'function'
     && obj.data instanceof Uint8ClampedArray)
   && (
     obj.data.length === obj.width * obj.height * 4
     || obj.data.length === obj.width * obj.height * 3
    );
}

So here is a working code. Obviously there can be many other ways to get raw image data

const Jimp = require("jimp");
const fs = require("fs");
const PNG = require("pngjs").PNG;


// Does not work
// const img = new Jimp({
//     data: fs.readFileSync("img.png"),
//     width: 688,
//     height: 860
// });

fs.createReadStream("img.png")
  .pipe(
    new PNG({
      filterType: 4,
    })
  )
  .on("parsed", function () {
    // for (var y = 0; y < this.height; y++) {
    //   for (var x = 0; x < this.width; x++) {
    //     var idx = (this.width * y + x) << 2;

    //     // invert color
    //     this.data[idx] = 255 - this.data[idx];
    //     this.data[idx + 1] = 255 - this.data[idx + 1];
    //     this.data[idx + 2] = 255 - this.data[idx + 2];

    //     // and reduce opacity
    //     this.data[idx + 3] = this.data[idx + 3] >> 1;
    //   }
    // }

    // this.pack().pipe(fs.createWriteStream("out.png"));



    const img = new Jimp({
        data: this.data,
        width: 688,
        height: 860
    });
});

I do think that the error message should change though.

nopeless avatar Mar 16 '22 09:03 nopeless

@nopeless would you mind making a pr to make the error better?

hipstersmoothie avatar Feb 04 '23 18:02 hipstersmoothie

@hipstersmoothie No, as I personally believe that custom constructors are fundamentally flawed. Best I would do is send a deprecation warning

nopeless avatar Feb 04 '23 19:02 nopeless

Well if the documentation is in fact wrong we should update it until that fix happens. If anyone wants to make the pr I'll be quick to review and merge!

hipstersmoothie avatar Feb 04 '23 23:02 hipstersmoothie

@hipstersmoothie Actually I read the issue wrong. The documentation definitely needs some clarity. But if we use the same entry point for different types of constructors (constructor overloading) then error messages will not be reliable. We should support a different scheme where the user should explicitly provide some sort of format ex) Jimp.png()

nopeless avatar Feb 05 '23 01:02 nopeless

I had same message with electron vite in production.

I was able to downgrade to "jimp": "^0.3.10" and it worked after that

gitTerebi avatar Jun 03 '24 23:06 gitTerebi