jimp
jimp copied to clipboard
Fix new Jimp()
When creating a new Jimp()
asyncronous stuff shouldn't happen.
const image = new Jimp()
console.log(image);
// result is <Jimp pending> so user would have to use timeouts to guess when it is useable
it should be the folowing.
const image = new Jimp()
console.log(image);
// result is <Jimp 400x300>
image.resize(10, Jimp.auto).greyscale();
To achieve this all we have to do is use fs.readFileSync
rather than fs.readFile
and stop supporting the callback. The callback within the constructor is an anti-pattern anyway.
This will be a breaking change. But it really cleans up the API.
related to #303
Although we probably don't want to block on a sync readFile. it could be an option
const image = new Jimp({
filePath: 'path/to/file.png'
sync: true
})
Actually I think the better way might be to just depreacte new Jimp()
all together since we have the top level APIs of Jimp.read
and Jimp.create
. This could return the jimp instance if the operation was synchronous and a promise if it wasn't. The only thing that would be async is reading from a filepath.
The following code seems nice.
const image = Jimp.create({
height: 256
width: 256
background: 0xff0000ff
});
const mask = await Jimp.read(filePath)
Just bumped into this. Wow, looks like an old issue.
In my use case, I might be conditionally either loading an external base image, or creating a new blank one. The difference with Jimp.read()
and new Jimp()
is a bit unintuitive, but what's worse is that you have to handle them differently in the code. I'm using async/await with .read()
, but I having to support the callback pattern with new Jimp()
forces me to structure the entire file in an entirely different way (which also conflicts with the rest of the codebase, since everything is async/await now).
You can work around this, it's not like it breaks everything, but it's annoying and completely unnecessary. I think the right solution is Jimp.create()
or await Jimp.create()
.
Fixed in v1