Skip to content
docs
GitHub 4M /week v2.2.1
Guide

Custom Jimp

Build a Jimp with only the plugins and formats you need, or add extra ones.

The Jimp class exported by jimp is built from separate packages: one per image format and one per group of methods. You can build your own Jimp class from those packages with createJimp. Do that to:

  • Add something jimp doesn’t include, such as WebP and AVIF, shadow, or your own plugins.
  • Remove what you don’t use, to make a browser bundle smaller.
  • Replace a built-in format, for example with a WASM codec or different defaults.

Creating a custom Jimp

createJimp from @jimp/core takes the method plugins and format plugins to include, and returns a Jimp class with the same API as the one from jimp.

  1. Install @jimp/core and the packages you want. Jimp needs Node.js 22 or later.

    Terminal window
    npm install @jimp/core @jimp/js-png @jimp/plugin-resize
  2. Create the class. Pass each plugin package’s methods export, and each format package’s default export without calling it:

    jimp.ts
    import { createJimp } from "@jimp/core";
    import png from "@jimp/js-png";
    import { methods as resize } from "@jimp/plugin-resize";
    export const Jimp = createJimp({
    plugins: [resize],
    formats: [png],
    });
  3. Use it like the regular Jimp:

    index.ts
    import { Jimp } from "./jimp.js";
    const image = await Jimp.read("photo.png");
    image.resize({ w: 100 });
    await image.write("thumbnail.png");

This Jimp can only read and write PNG, and its images only have the resize methods plus the built-in methods. Calling anything else fails:

You call Error
image.blur(5) TypeError: image.blur is not a function
Jimp.read("photo.jpg") Mime type image/jpeg does not support decoding
image.write("out.jpg") Unsupported MIME type: image/jpeg

In TypeScript these are caught at compile time: the class’s types only include the methods and MIME types you passed in.

Starting from the defaults

jimp exports the plugins and formats it uses as defaultPlugins and defaultFormats. Spread them to add to the regular Jimp instead of starting from nothing:

jimp.ts
import { createJimp } from "@jimp/core";
import { defaultFormats, defaultPlugins } from "jimp";
import { methods as shadow } from "@jimp/plugin-shadow";
import webp from "@jimp/wasm-webp";
export const Jimp = createJimp({
formats: [...defaultFormats, webp],
plugins: [...defaultPlugins, shadow],
});

When two formats have the same MIME type, Jimp uses the first one in the list. To replace a built-in format, put the new one first: formats: [wasmPng, ...defaultFormats]. When two plugins have a method with the same name, the later plugin’s method wins.

defaultFormats contains @jimp/js-bmp (as image/bmp and image/x-ms-bmp), @jimp/js-gif, @jimp/js-ico (as image/x-icon and image/vnd.microsoft.icon), @jimp/js-jpeg, @jimp/js-png and @jimp/js-tiff. ICO was added to the defaults in 2.0.

Which package has which method

These methods are part of @jimp/core, so every Jimp class has them: Jimp.read, Jimp.fromBuffer, Jimp.fromBitmap, Jimp.readFrames, Jimp.fromBufferFrames, Jimp.encodeFrames, Jimp.writeFrames, getBuffer, getBase64, write, clone, toColorSpace, composite, getPixelColor, setPixelColor, getPixelIndex, hasAlpha, scan and scanIterator.

Everything else comes from a plugin:

Package Methods
@jimp/plugin-blit blit
@jimp/plugin-blur blur, gaussian, sharpen, median, bilateral
@jimp/plugin-border border, roundCorners, boxShadow
@jimp/plugin-circle circle
@jimp/plugin-color color, brightness, contrast, exposure, vibrance, greyscale, invert, sepia, gradientMap, posterize, grain, normalize, opacity, fade, opaque, pixelate, convolute, convolution
@jimp/plugin-contain contain
@jimp/plugin-cover cover
@jimp/plugin-crop crop, autocrop, autocropRect
@jimp/plugin-displace displace
@jimp/plugin-dither dither
@jimp/plugin-draw drawLine, drawRect, drawEllipse, drawPolygon, drawPath, floodFill
@jimp/plugin-fisheye fisheye
@jimp/plugin-flip flip
@jimp/plugin-hash hash, pHash, distanceFromHash, and the functions distance and compareHashes
@jimp/plugin-lut colorGrade, and the function parseCubeLut
@jimp/plugin-mask mask
@jimp/plugin-morphology erode, dilate, open, close
@jimp/plugin-print print, and the functions measureText and measureTextHeight. loadFont is exported from @jimp/plugin-print/load-font, loadTTF from @jimp/plugin-print/load-ttf and the fonts from @jimp/plugin-print/fonts.
@jimp/plugin-quantize quantize
@jimp/plugin-resize resize, scale, scaleToFit
@jimp/plugin-rotate rotate, skew, affine
@jimp/plugin-threshold threshold, and the function otsuThreshold
@jimp/plugin-shadow shadow. Not included in jimp.

diff is a function from @jimp/diff, and the color helpers such as intToRGBA come from @jimp/utils. Neither needs to be registered with createJimp.

Types

createJimp infers the class’s types from what you pass it, so methods and MIME types you didn’t include are type errors. To type an image from your class, use InstanceType:

jimp.ts
export const Jimp = createJimp({ plugins: [resize], formats: [png] });
export type JimpImage = InstanceType<typeof Jimp>;
function thumbnail(image: JimpImage) {
return image.resize({ w: 100 });
}

The jimp package exports the same type for its own class as JimpInstance. See TypeScript for more.

Using a custom Jimp outside of Node.js

Jimp’s packages import Buffer from the buffer package instead of relying on the Node.js global. In Node.js and Bun this resolves to the built-in module, and bundlers targeting the browser use the npm package. So a custom Jimp works in browsers and in Cloudflare Workers without a Buffer polyfill.

Browsers

The jimp package ships a pre-bundled browser build with everything it needs (see Using in Browser). A custom Jimp is bundled by your own bundler instead, so there are a few things to know.

@jimp/js-png uses @jimp/pngjs, which has a browser export condition that compresses with pako instead of Node’s zlib. Bundlers that target the browser (Vite, webpack, esbuild, Rollup with @rollup/plugin-node-resolve’s browser: true) pick it up automatically, so you don’t need to polyfill stream, zlib or util.

The WASM format plugins need extra config in Vite 7 and earlier; see WEBP / WASM.

Older versions of @jimp/core and the format plugins use the global Buffer, which browsers don’t provide. If you see ReferenceError: Buffer is not defined, update your Jimp packages. If you can’t update, set globalThis.Buffer from the buffer package before you import Jimp.

Older versions of @jimp/js-gif depend on gifwrap, which imports fs for file helpers that Jimp doesn’t use. Vite logs Module "fs" has been externalized for browser compatibility for it, and that warning is safe to ignore.

Cloudflare Workers

A custom Jimp works in Workers without any extra configuration. The WASM format plugins need their .wasm files passed in; see Cloudflare Workers in the WASM guide. If other dependencies in your worker need Node.js APIs, enable the nodejs_compat compatibility flag:

wrangler.toml
compatibility_flags = ["nodejs_compat"]

See also