Skip to content

Writing Plugins

There are two types of plugins you can write for Jimp:

  1. Image format plugins
  2. Image manipulation plugins

You can combine plugins to create a Custom Jimp.

Image format plugins

Image format plugins are responsible for loading and saving images in a specific format.

For example, the bmp package is responsible for loading and saving BMP images.

An image format plugins is composed of two functions: encode and decode.

import { Format } from "@jimp/types";
import WEBP, { WEBPOptions } from "webp-js";
export default function webp() {
return {
mime: "image/webp",
// Encode the bitmap as a WEBP image
encode: (bitmap, options: WEBPOptions = {}) =>
WEBP.encode(bitmap, quality).data,
// Decode a WEBP image into a bitmap
decode: (data) => WEBP.decode(data),
} satisfies Format<"image/webp">;
}

Image manipulation plugins

Image manipulation plugins are responsible for manipulating images.

For example, the blur package is responsible for applying a blur effect to an image.

An image manipulation method takes an image as its first argument, does something to it, and returns a new image.

import { JimpClass } from "@jimp/types";
import { BlurOptions } from "blur-ts";
export const methods = {
/**
* A blur effect
*
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.blur(5);
* ```
*/
blur<I extends JimpClass>(image: I, r: number) {
// Implementation
},
};

Generally our method APIs follow the following pattern:

  • They can take 1 primitive as an option (other than image)
  • Otherwise they take and options object defined and validated by zod.