Skip to content
docs
GitHub 4.5M /week v2.2.0
Size & shape

border()

Add a solid border around the image, optionally with rounded corners.

From @jimp/plugin-border

border draws a solid border around the outside of the image, like a CSS border. The image grows by the border width on every side, so none of it is covered. Add a radius for rounded corners.

Signature

border.d.ts
image.border(options: {
width: number;
color?: number | string;
radius?: CornerRadius;
}): this
type CornerRadius =
| number
| { topLeft?: number; topRight?: number; bottomRight?: number; bottomLeft?: number };

border changes the image in place and returns it, so you can chain more calls after it.

Options

Option Type Default Description
width number required The border width in pixels. A whole number, 0 or more.
color number or string 0x000000ff The border color: an RGBA number such as 0xff0000ff, or a CSS color such as "#ff0000", "rgb(255 0 0)" or "red".
radius number or object none The radius of the border’s outer corners. One number for every corner, or { topLeft, topRight, bottomRight, bottomLeft }.

Example

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
// 800 x 600 becomes 840 x 640
image.border({ width: 20, color: "white" });
await image.write("photo-framed.jpg");

Rounded corners

example.ts
image.border({ width: 8, color: "#ff5fb0", radius: 32 });
await image.write("photo-rounded.png");

The outer corners of the border get the radius you pass. The image’s own corners are rounded to fit inside, with a radius width smaller (here 24), just like CSS border-radius does for the padding box. The rounded-off corners outside the border are transparent, so save as PNG to keep them.

A double border

Call border twice to stack borders:

example.ts
image
.border({ width: 4, color: "#1b1840" })
.border({ width: 12, color: "#ffd83a" });

Try it

Change width, pick a color, and drag radius to round the corners.

Try border()
image.border({ width: 12, color: "#1b1840" })

Draw a border outside the image, so it grows by width on every side. A radius rounds the corners.

12px
#1b1840
0px
Loading Jimp…Drop an image to load it
PNG
index.js
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");

Notes

  • The image grows. The new size is image.width + 2 * width by image.height + 2 * width. To keep the original size, scale the image down first, for example with contain or resize.
  • Transparent areas stay transparent. The border is drawn around the image’s rectangle, not behind it. Transparent pixels inside the image aren’t filled with the border color.
  • Semi-transparent borders. The alpha of color is respected, so 0x00000080 draws a 50% black border.
  • Oversized radii are scaled like CSS. If two corners on one side add up to more than that side, all radii are scaled down by the same factor.
  • Use border instead of roundCorners + border. Rounding the image first and then adding a border leaves the border’s outer corners square, with transparent gaps between the border and the image’s rounded corners. Pass radius to border and both are rounded to match.
  • Invalid colors throw. A string that isn’t a CSS color throws Invalid color "nope" (with your string in the quotes).
  • Validation. width must be a whole number: 2.5 fails with Invalid input: expected int, received number, and negative values with Too small: expected number to be >=0.
  • Added in 2.0.