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
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
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
// 800 x 600 becomes 840 x 640image.border({ width: 20, color: "white" });
await image.write("photo-framed.jpg");Rounded corners
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:
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.
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.
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 * widthbyimage.height + 2 * width. To keep the original size, scale the image down first, for example withcontainorresize. - 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
coloris respected, so0x00000080draws 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
borderinstead ofroundCorners+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. Passradiustoborderand 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.
widthmust be a whole number:2.5fails withInvalid input: expected int, received number, and negative values withToo small: expected number to be >=0. - Added in 2.0.