module:image
- Description:
Scaling and composing images with Sharp.js
- Source:
Methods
(async, static) image.clip(item, bgitem) → {Promise}
- Description:
Clip (crop) an item's image buffer so it fits within the background item's bounds.
If the item is completely outside the background, it marks it as skipped.
Side effects:
- May set
item._skip = 1when the item is fully outside the background. - Replaces
item._bufferwith the cropped image buffer. - Replaces
item._metawith metadata of the cropped image.
- May set
- Source:
Parameters:
| Name | Type | Description | |||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
item |
Object | item to be clipped. Properties
|
|||||||||||||||||||||||||||||||||
bgitem |
Object | Background item that defines the clipping bounds. Properties
|
Returns:
| Type | Description |
|---|---|
| Promise |
Resolves when clipping is complete. Returns early if no clipping is needed. |
(async, static) image.composite(items, defaultsopt) → {Array.<object>}
- Description:
Blend multiple images together, first image is used as the background for remaning items
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
items |
Array.<object> | list of objects representing an image or text, properties are given to module:image.create,
additional string property |
|
defaults |
object |
<optional> |
defaults global for all items, same parameters |
Returns:
| Type | Description |
|---|---|
| Array.<object> |
|
(static) image.convert(input, item) → {object}
- Description:
Apply Sharp image filters and conversions
- Source:
Parameters:
| Name | Type | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
input |
object | sharp image |
||||||||
item |
object |
Properties
|
Returns:
| Type | Description |
|---|---|
| object |
transformed image |
(async, static) image.create(item, bgitemopt) → {Promise.<object>}
- Description:
Create an image from a photo or text, supports raunded corners, borders
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
item |
object |
Properties
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bgitem |
object |
<optional> |
background item |
Returns:
| Type | Description |
|---|---|
| Promise.<object> |
|
(async, static) image.createBorder(item, width, height) → {Promise.<Buffer>}
- Description:
Create a border image buffer with optional rounded corners.
Builds an RGBA image of the given size filled with
item.border_color(anditem.border_alpha), falling back to semi-transparent white when no color is provided. If a radius is specified (item.border_radiusoritem.radius), it applies an SVG rounded-rectangle mask using a composite operation (defaultdest-inoritem.border_blend).
- Source:
Parameters:
| Name | Type | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
item |
Object | Border configuration. Properties
|
||||||||||||||||||||||||||||||
width |
number | Output image width in pixels. |
||||||||||||||||||||||||||||||
height |
number | Output image height in pixels. |
Returns:
| Type | Description |
|---|---|
| Promise.<Buffer> |
PNG-encoded image buffer. |
(async, static) image.createGradient(item) → {Promise.<object>}
- Description:
Generate an SVG radial-gradient ellipse overlay for an image item and return it as a Buffer.
Picks a gradient color (
gcolor) based onitem.coloranditem.gradient_type, adjusting for luminance/contrast, then builds an SVG sized toitem.meta.width/item.meta.height.
- Source:
Parameters:
| Name | Type | Description | |||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
item |
Object |
Properties
|
Returns:
| Type | Description |
|---|---|
| Promise.<object> |
Sharp instance with rendered image |
(async, static) image.createImage(item, bgitemopt) → {Promise.<object>}
- Description:
Create a Sharp image instance from different input sources and optionally resize it.
Accepts an image from a file path/URL (
item.file), an existing Sharp instance (item.data), a Buffer (item.data), or a base64 data URL (item.datastarting with"data:image/"). Ifwidth/heightare provided (or derived frombgitem._meta), the image is resized and a new Sharp instance is returned.
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
item |
Object |
Properties
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
bgitem |
Object |
<optional> |
Optional background item used only as a source of fallback dimensions. Properties
|
Returns:
| Type | Description |
|---|---|
| Promise.<object> |
A Sharp instance for the loaded (and possibly resized) image. |
(async, static) image.createOutline(item, bgitemopt) → {Promise.<Sharp>}
- Description:
Render an SVG text block (optionally with stroke + outline dilation or drop shadow) into an image buffer, trimming each line and stitching multiple lines together when needed.
The function builds an SVG based on
itemproperties, renders it withsharp, then:- if
item.textcontains multiple lines (\n), renders each line separately and stitches results - otherwise returns a single rendered image
Supported effects (mutually exclusive; dilation takes precedence over shadow):
- Dilation/outline via SVG filter when
item.dilate_radius|dilate_alpha|dilate_coloris set - Drop shadow via SVG filter when
item.shadow_width|shadow_alpha|shadow_coloris set
Notes:
- Width/height and padding may be derived from
bgitem._meta.width/height. item.textsupports simple Pango-like tags:<b>,<i>,<small>,<big>, and closing tags</...>which are mapped to<tspan>elements.item.sizecan be a fraction (0..1) meaning a percentage of background height.
- if
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
item |
Object | Text/rendering configuration. Properties
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bgitem |
Object |
<optional> |
Background item used for sizing/padding. Properties
|
Returns:
| Type | Description |
|---|---|
| Promise.<Sharp> |
A Sharp instance for the rendered outline/shadow text image. |
(static) image.createText(item, bgitemopt) → {object}
- Description:
Create a Sharp image instance that renders a text block using Pango rendering.
Builds an optional
<span ...>...</span>wrapper around the provided text using supported attributes (color, size, weight, underline, etc.), normalizes newline sequences ("\\n"->"\n"), and passes sizing/font/layout options tosharp({ text: ... }).
- Source:
Parameters:
| Name | Type | Attributes | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
item |
Object | Text render options. Properties
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bgitem |
Object |
<optional> |
Optional background item used for default sizing. Properties
|
Returns:
| Type | Description |
|---|---|
| object |
A Sharp instance configured with a text input. |
(async, static) image.detect(item, bgitem)
- Description:
Detect a text color to place on top of the area inside given background
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
item |
object | input item |
bgitem |
object | background item generated |
(static) image.mergeDefaults(item, defaultsopt) → {object}
- Description:
Merge defaults into an item, supports format
id.nameortype.namewhere id must match item.id and type item.type
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
item |
object | ||
defaults |
object |
<optional> |
Returns:
| Type | Description |
|---|---|
| object |
(async, static) image.metadata(input) → {object}
- Description:
Return metadata about the input
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
input |
object | Buffer | buffer or Sharp instance |
Returns:
| Type | Description |
|---|---|
| object |
|
(static) image.padding(itemopt, widthopt, heightopt) → {Object}
- Description:
Normalize padding-related item into a rectangle object.
Accepts either side-specific padding values or axis-wide or single padding:
padding_top,padding_right,padding_bottom,padding_left(highest priority)padding_yapplies to top/bottom,padding_xapplies to left/rightpaddingapplies to all sides
Values >= 1 are in pixels, > 0 and < 1 in percentages relative to the background dimenstions
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
item |
object |
<optional> |
Input item object. Properties
|
||||||||||||||||||||||||||||||||
width |
number |
<optional> |
max width to be used for fractions |
||||||||||||||||||||||||||||||||
height |
number |
<optional> |
max height to be used for fractions |
Returns:
| Type | Description |
|---|---|
| Object |
Padding rectangle. |
(static) image.region(item, width, height) → {object}
- Description:
Return region corresponding to the item gravity or absolute coordinates (top, left ,width, height)
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
item |
object | object |
width |
int | background image width |
height |
int | background image height |
Returns:
| Type | Description |
|---|---|
| object |
|
(static) image.stats(input, regionopt) → {object}
- Description:
Return the stats about given region with inverted dominant color, if no region provided stats for the whole image
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
input |
Buffer | object | buffer or sharp object |
|
region |
object |
<optional> |
region to extract and return stats for |
Returns:
| Type | Description |
|---|---|
| object |
region stats with key properties:
|
(async, static) image.stitch(images, optionsopt) → {Promise.<Sharp>}
- Description:
Stitch multiple images into a single vertically stacked image.
Each input image is loaded and normalized via module:image/metadata, then composited onto a transparent RGBA background. Images are placed in the order provided, starting at the top (0), with each subsequent image positioned directly below the previous one.
The resulting canvas size is:
- width: max width of all successfully processed images
- height: sum of heights of all successfully processed images
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
images |
Array.<(Buffer|string|Object)> | List of images to stitch. Each item must be accepted by image.metadata (commonly a Buffer, file path, URL, or an object supported by that helper). |
|||||||||
options |
object |
<optional> |
Properties
|
Throws:
-
May throw if creating/compositing/encoding fails, or if the input set produces an invalid canvas size (e.g., no valid images).
- Type
- Error
Returns:
| Type | Description |
|---|---|
| Promise.<Sharp> |
A Sharp instance containing the stitched PNG image. |