High performance Node.js image processing library for resizing JPEG, PNG, WebP, GIF, AVIF and TIFF images
npx @tessl/cli install tessl/npm-sharp@0.34.0Sharp is a high-performance Node.js image processing library that provides fast image transformations including resizing, format conversion, rotation, extraction, compositing, and color space management. Built on the libvips image processing library, it delivers 4x-5x faster performance than ImageMagick while maintaining quality through Lanczos resampling and proper handling of color spaces, ICC profiles, and alpha channels.
npm install sharpconst sharp = require('sharp');For TypeScript:
import sharp from 'sharp';const sharp = require('sharp');
// Basic resize operation
await sharp('input.jpg')
.resize(300, 200)
.toFile('output.jpg');
// Pipeline with multiple operations
const result = await sharp('input.png')
.rotate(90)
.resize({ width: 800, fit: 'contain' })
.jpeg({ quality: 90 })
.toBuffer();
// Stream processing
const transformer = sharp()
.resize(400)
.webp({ quality: 80 });
readableStream
.pipe(transformer)
.pipe(writableStream);Sharp is built around several key components:
Create Sharp instances from various input sources including files, buffers, streams, and generated content.
function sharp(input?: SharpInput | SharpInput[], options?: SharpOptions): Sharp;
type SharpInput = Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray |
Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array |
Float32Array | Float64Array | string;
interface SharpOptions {
autoOrient?: boolean;
failOn?: 'none' | 'truncated' | 'error' | 'warning';
limitInputPixels?: number | boolean;
density?: number;
pages?: number;
page?: number;
animated?: boolean;
raw?: CreateRaw;
create?: Create;
text?: CreateText;
join?: Join;
}Resize, crop, extend, and extract regions from images with multiple fit strategies and interpolation methods.
resize(width?: number | ResizeOptions, height?: number, options?: ResizeOptions): Sharp;
extract(region: Region): Sharp;
extend(extend: number | ExtendOptions): Sharp;
trim(options?: TrimOptions): Sharp;
interface ResizeOptions {
width?: number;
height?: number;
fit?: 'cover' | 'contain' | 'fill' | 'inside' | 'outside';
position?: string | number;
background?: string | object;
kernel?: 'nearest' | 'cubic' | 'mitchell' | 'lanczos2' | 'lanczos3';
withoutEnlargement?: boolean;
withoutReduction?: boolean;
}Apply filters, effects, and transformations including rotation, sharpening, blurring, and color adjustments.
rotate(angle?: number, options?: RotateOptions): Sharp;
sharpen(options?: SharpenOptions): Sharp;
blur(sigma?: number | boolean | BlurOptions): Sharp;
gamma(gamma?: number, gammaOut?: number): Sharp;
negate(negate?: boolean | NegateOptions): Sharp;
normalise(options?: NormaliseOptions): Sharp;
modulate(options?: ModulateOptions): Sharp;
interface SharpenOptions {
sigma: number;
m1?: number;
m2?: number;
x1?: number;
y2?: number;
y3?: number;
}Control color spaces, manipulate channels, and apply color transformations.
tint(tint: string | object): Sharp;
greyscale(greyscale?: boolean): Sharp;
toColourspace(colourspace?: string): Sharp;
extractChannel(channel: 0 | 1 | 2 | 3 | 'red' | 'green' | 'blue' | 'alpha'): Sharp;
removeAlpha(): Sharp;
ensureAlpha(alpha?: number): Sharp;
interface RGBA {
r?: number;
g?: number;
b?: number;
alpha?: number;
}Composite multiple images with various blend modes and positioning options.
composite(images: OverlayOptions[]): Sharp;
interface OverlayOptions {
input: string | Buffer | object;
blend?: 'clear' | 'source' | 'over' | 'multiply' | 'screen' | 'overlay' |
'darken' | 'lighten' | 'difference' | 'exclusion';
gravity?: 'north' | 'northeast' | 'east' | 'southeast' | 'south' |
'southwest' | 'west' | 'northwest' | 'center' | 'centre';
top?: number;
left?: number;
tile?: boolean;
}Write images to files or buffers with format-specific options and metadata control.
toFile(fileOut: string, callback?: (err: Error, info: OutputInfo) => void): Promise<OutputInfo>;
toBuffer(options?: BufferOptions, callback?: (err: Error, buffer: Buffer, info: OutputInfo) => void): Promise<Buffer>;
jpeg(options?: JpegOptions): Sharp;
png(options?: PngOptions): Sharp;
webp(options?: WebpOptions): Sharp;
avif(options?: AvifOptions): Sharp;
tiff(options?: TiffOptions): Sharp;
interface OutputInfo {
format: string;
size: number;
width: number;
height: number;
channels: number;
premultiplied: boolean;
}Access comprehensive image metadata and pixel-level statistics without decoding image data.
metadata(): Promise<Metadata>;
stats(): Promise<Stats>;
interface Metadata {
format: string;
width: number;
height: number;
channels: number;
depth: string;
space: string;
hasProfile: boolean;
hasAlpha: boolean;
orientation?: number;
density?: number;
pages?: number;
}
interface Stats {
channels: ChannelStats[];
isOpaque: boolean;
entropy: number;
sharpness: number;
dominant: { r: number; g: number; b: number };
}Control library behavior, performance settings, and access version information.
// Static utility functions
sharp.cache(options?: boolean | CacheOptions): CacheResult;
sharp.concurrency(concurrency?: number): number;
sharp.counters(): SharpCounters;
sharp.simd(enable?: boolean): boolean;
// Library information
sharp.format: FormatEnum;
sharp.versions: VersionInfo;
sharp.interpolators: Interpolators;interface Sharp {
// Sharp extends Node.js Duplex stream
clone(): Sharp;
}
interface Region {
left: number;
top: number;
width: number;
height: number;
}
interface Create {
width: number;
height: number;
channels: 3 | 4;
background: string | RGBA;
}
interface CreateText {
text: string;
font?: string;
fontfile?: string;
width?: number;
height?: number;
align?: 'left' | 'centre' | 'center' | 'right';
justify?: boolean;
dpi?: number;
rgba?: boolean;
spacing?: number;
wrap?: 'word' | 'char' | 'word-char' | 'none';
}