No matching constructor overloading was found. Please see the docs for how to call the Jimp constructor.
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
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({ ... })
Same here. Documentation is wrong.
@jardicc can you give me a link to that documentation
@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 I'll look into the source and figure out the issue
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?
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.
note: Documentation shows object as first argument.
@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 would you mind making a pr to make the error better?
@hipstersmoothie No, as I personally believe that custom constructors are fundamentally flawed. Best I would do is send a deprecation warning
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 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()
I had same message with electron vite in production.
I was able to downgrade to "jimp": "^0.3.10" and it worked after that