Migrating to v1
The goals of v1 were to:
- Make jimp easier to use in any environment
- Make jimp’s API more consistent and easier to use
- Many constants have been removed and are string value powered by TS
Async/Sync
In v0 of jimp there were a mix of async and sync methods for export. In v1 all “export” methods are async.
They have also been renamed:
getBufferAsync
->getBuffer
getBase64Async
->getBase64
writeAsync
->write
Importing
Jimp
no longer uses a default export. Instead it uses named exports.
Positional Arguments to Options Objects
In jimp v0 there were many ways to provide arguments to a method. Most methods used positional arguments, which leads to code thats harder to read and extend.
For example the resize method used to look like this:
Now it looks like this:
Jimp
Constructor
The constructor for Jimp
has changed.
Much in the same vein as above, the constructor now takes an options object.
To create and empty jimp image:
Even give it a background color:
Jimp.read
In v0 of jimp the constructor was async! This is a huge anit-pattern so it had to go.
Now you should instead use the Jimp.read
method.
In node environments it will read a file from disk.
In the browser it fetch the file from the url.
It will also read from a Buffer
or ArrayBuffer
.
Jimp.fromBuffer
You can load an image from a buffer.
In v0 this was done through the constructor.
In v1 it is done through the Jimp.fromBuffer
method.
Jimp.fromBitmap
You can load an image (or canvas data) from a bitmap.
In v0 this was done through the constructor.
In v1 it is done through the Jimp.fromBitmap
method.
Encoding and Decoding Options
Another area where the API has changed is the way that encodings and decoding are handled.
Previously the options were global and it was confusing where they might be applied (unless you have experience with the underlying image codecs).
For example in v0 if you wanted to export a JPEG with the quality set to 80% you would do this:
In v1 the options are passed when you get the encoded image:
Removed Constants
Most constants have been moved to named exports.
Other changes:
Jimp.AUTO
- This constant was only needed for positional arguments. It is no longer needed with the new API.
Jimp.MIME_*
These have moved to a named export JimpMime
.
Moved Functions
Jimp.intToRGBA
was movedimport { intToRGBA } from "jimp";
image.getHeight()
was movedimage.height
image.getWidth()
was movedimage.width
image.getMIME()
was movedimage.mime