contain()
Scale the image to fit inside a box and pad the rest.
From @jimp/plugin-contain
contain scales the image to the largest size that fits inside a w × h box, keeping its aspect ratio, and fills the space around it with the image’s background color.
The result is always exactly w × h and nothing is cropped, like CSS object-fit: contain.
Signature
image.contain(options: { w: number; h: number; align?: number | { x?: number; y?: number }; mode?: ResizeStrategy; premultiplyAlpha?: boolean;}): thiscontain changes the image in place and returns it, so you can chain more calls after it.
Options
| Option | Type | Default | Description |
|---|---|---|---|
w |
number |
required | The output width in pixels. |
h |
number |
required | The output height in pixels. |
align |
number or { x?, y? } |
centered | Where to place the image in the box: HorizontalAlign and VerticalAlign flags, or { x, y } fractions from 0 to 1. See Alignment. |
mode |
ResizeStrategy |
built-in resizer | The resampling algorithm. See resize strategies. |
premultiplyAlpha |
boolean |
true |
Treat alpha as opacity. Pass false when alpha holds other data, to keep the color of transparent pixels. See Alpha that isn’t opacity. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
// 800 x 600 is scaled to 300 x 225 and centered in a 300 x 300 squareimage.contain({ w: 300, h: 300 });
await image.write("square.png");Choose the padding color
The padding uses image.background, which is transparent black (0x00000000) for images loaded with Jimp.read.
Set it before calling contain to pad with a color:
image.background = 0xffffffff; // opaque white, as 0xRRGGBBAAimage.contain({ w: 300, h: 300 });
await image.write("square.jpg");Alignment
align places the scaled image inside the box.
Only the direction with padding matters. A wide image in a square box fills the width, so only the vertical value has an effect.
With flags
Combine at most one HorizontalAlign value and one VerticalAlign value with |:
import { Jimp, HorizontalAlign, VerticalAlign } from "jimp";
const image = await Jimp.read("logo.png");
// Pin the logo to the bottom-right corner of the boximage.contain({ w: 400, h: 400, align: HorizontalAlign.RIGHT | VerticalAlign.BOTTOM,});| Value | Places the image |
|---|---|
HorizontalAlign.LEFT |
against the left edge |
HorizontalAlign.CENTER |
in the middle (default) |
HorizontalAlign.RIGHT |
against the right edge |
VerticalAlign.TOP |
against the top edge |
VerticalAlign.MIDDLE |
in the middle (default) |
VerticalAlign.BOTTOM |
against the bottom edge |
A direction you leave out is centered, so VerticalAlign.BOTTOM is the same as HorizontalAlign.CENTER | VerticalAlign.BOTTOM.
With fractions
Pass { x, y } to place the image anywhere between the edges, like the percentages of CSS object-position.
0 is against the left or top edge, 0.5 the middle and 1 the right or bottom edge:
// Like CSS object-position: 20% 80%image.contain({ w: 300, h: 300, align: { x: 0.2, y: 0.8 } });
// Against the bottom edge; x is left out, so it's centeredimage.contain({ w: 300, h: 300, align: { y: 1 } });A missing x or y is centered (0.5), and values outside 0 to 1 are clamped.
Try it
Set the box with w and h, change the alignment, and pick a background color for the padding.
Switch align with to { x, y } fractions to use sliders instead of flags, and turn off set background to see the default transparent padding.
image.contain({ w: 240, h: 240 })Scale the image to fit inside a w x h box and fill the rest with the background color.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- It enlarges too. An image smaller than the box is scaled up until it touches two of its sides.
- JPEG output. JPEG has no transparency, so the default transparent padding is written as black. Set
image.backgroundto the color you want first. - Translucent pixels. The scaled image is drawn over the padding color, so pixels that are partly transparent blend with
image.background. premultiplyAlpha: falsecopies the scaled image into the box instead of drawing it over the background, so its alpha and the color of its transparent pixels are kept exactly.- Invalid flags. Two flags for one direction (such as
VerticalAlign.TOP | VerticalAlign.BOTTOM), unknown bits or a fractional number throwalign must combine at most one HorizontalAlign and one VerticalAlign flag.align: 0has no flags, so it’s centered. - Added in 2.0:
{ x, y }fractions foralign, thepremultiplyAlphaoption, and theLANCZOS,LANCZOS2andMITCHELLstrategies. - Changed in 2.0: a direction left out of
alignis centered, and any invalid combination of flags throws. Before, a missing direction aligned to the left or top, and two flags in one direction were accepted unless both directions were invalid.