High performance Node.js image processing library for resizing JPEG, PNG, WebP, GIF, AVIF and TIFF images
80
Sharp instances are created using the main constructor function which accepts various input types and configuration options.
Creates a Sharp instance from various input sources.
/**
* Creates a Sharp instance from various input sources
* @param input - Image data source (optional)
* @param options - Configuration options (optional)
* @returns Sharp instance for method chaining
*/
function sharp(input?: SharpInput | SharpInput[], options?: SharpOptions): Sharp;
type SharpInput = Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray |
Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array |
Float32Array | Float64Array | string;Usage Examples:
// From file path
const image1 = sharp('input.jpg');
// From Buffer
const buffer = fs.readFileSync('input.png');
const image2 = sharp(buffer);
// Create empty instance for streaming
const transformer = sharp();
// With options
const image3 = sharp('input.tiff', {
density: 300,
pages: 5,
failOn: 'error'
});Configuration options for Sharp instances.
interface SharpOptions {
/** Auto-orient based on EXIF Orientation tag */
autoOrient?: boolean;
/** When to abort processing of invalid pixel data */
failOn?: 'none' | 'truncated' | 'error' | 'warning';
/** Pixel limit to prevent memory exhaustion */
limitInputPixels?: number | boolean;
/** Enable unlimited processing (removes safety features) */
unlimited?: boolean;
/** Use sequential rather than random access reading */
sequentialRead?: boolean;
/** DPI for vector images (1-100000) */
density?: number;
/** Ignore embedded ICC profile */
ignoreIcc?: boolean;
/** Number of pages to extract for multi-page input (-1 for all) */
pages?: number;
/** Page number to start extracting from (zero-based) */
page?: number;
/** Read all frames/pages of animated image */
animated?: boolean;
/** Raw pixel input options */
raw?: CreateRaw;
/** Create new image options */
create?: Create;
/** Create text image options */
text?: CreateText;
/** Join array of images options */
join?: Join;
}Generate new images from scratch with specified dimensions and properties.
interface Create {
/** Image width in pixels */
width: number;
/** Image height in pixels */
height: number;
/** Number of channels (3 for RGB, 4 for RGBA) */
channels: 3 | 4;
/** Background color */
background: string | RGBA;
/** Noise generation options */
noise?: Noise;
/** Page height for animated images */
pageHeight?: number;
}
interface Noise {
/** Type of noise (currently only 'gaussian') */
type: 'gaussian';
/** Mean of pixels in generated noise */
mean?: number;
/** Standard deviation of pixels in generated noise */
sigma?: number;
}Usage Examples:
// Create solid color image
const redSquare = sharp({
create: {
width: 200,
height: 200,
channels: 4,
background: { r: 255, g: 0, b: 0, alpha: 1 }
}
});
// Create with noise
const noisyImage = sharp({
create: {
width: 100,
height: 100,
channels: 3,
background: 'black',
noise: {
type: 'gaussian',
mean: 128,
sigma: 30
}
}
});Create images from text using Pango markup.
interface CreateText {
/** Text to render as UTF-8 string (supports Pango markup) */
text: string;
/** Font name to render with */
font?: string;
/** Path to font file */
fontfile?: string;
/** Word-wrap width in pixels */
width?: number;
/** Target height in pixels (auto-fits text) */
height?: number;
/** Text alignment */
align?: 'left' | 'centre' | 'center' | 'right';
/** Apply text justification */
justify?: boolean;
/** Resolution for rendering (ignored if height specified) */
dpi?: number;
/** Enable RGBA output for color emoji */
rgba?: boolean;
/** Line height in points */
spacing?: number;
/** Word wrapping style */
wrap?: 'word' | 'char' | 'word-char' | 'none';
}Usage Examples:
// Basic text image
const textImage = sharp({
text: {
text: 'Hello World',
font: 'Arial',
width: 400,
height: 100,
align: 'center',
dpi: 150
}
});
// With Pango markup
const styledText = sharp({
text: {
text: '<span foreground="red" size="x-large">Styled</span> Text',
font: 'Sans',
rgba: true,
justify: true
}
});Work with raw pixel data directly.
interface CreateRaw {
/** Image width in pixels */
width: number;
/** Image height in pixels */
height: number;
/** Number of channels */
channels: 1 | 2 | 3 | 4;
/** Raw data is already premultiplied */
premultiplied?: boolean;
/** Page height for animated images */
pageHeight?: number;
}Combine array of images into single image.
interface Join {
/** Number of images per row */
across?: number;
/** Treat input as animated frames */
animated?: boolean;
/** Space between images in pixels */
shim?: number;
/** Background color */
background?: string | RGBA;
/** Horizontal alignment */
halign?: 'left' | 'centre' | 'center' | 'right';
/** Vertical alignment */
valign?: 'top' | 'centre' | 'center' | 'bottom';
}Usage Examples:
// Join images in grid
const joined = sharp([buffer1, buffer2, buffer3, buffer4], {
join: {
across: 2,
shim: 10,
background: 'white',
halign: 'center',
valign: 'center'
}
});
// Create animated sequence
const animated = sharp([frame1, frame2, frame3], {
join: {
animated: true
}
});Core instance methods available on all Sharp objects.
/**
* Create a new Sharp instance sharing the same input
* @returns New Sharp instance
*/
clone(): Sharp;Usage Example:
const original = sharp('input.jpg');
const copy1 = original.clone().resize(200);
const copy2 = original.clone().resize(400);
// Both copies share the same input but have independent processing pipelinesinterface RGBA {
r?: number;
g?: number;
b?: number;
alpha?: number;
}
type Channels = 1 | 2 | 3 | 4;
interface Sharp {
// Sharp extends Node.js Duplex stream
clone(): Sharp;
}Install with Tessl CLI
npx tessl i tessl/npm-sharpdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10