9.5 KiB
Sharp
Constructor factory to create an instance of sharp, to which further methods are chained.
JPEG, PNG, WebP, GIF, AVIF or TIFF format image data can be streamed out from this object.
When using Stream based output, derived attributes are available from the info event.
Non-critical problems encountered during processing are emitted as warning events.
Implements the stream.Duplex class.
Parameters
-
input(Buffer | Uint8Array | Uint8ClampedArray | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array | Float32Array | Float64Array | string)? if present, can be a Buffer / Uint8Array / Uint8ClampedArray containing JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image data, or a TypedArray containing raw pixel image data, or a String containing the filesystem path to an JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image file. JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present. -
optionsObject? if present, is an Object with optional attributes.-
options.failOnErrorboolean by default halt processing and raise an error when loading invalid images. Set this flag tofalseif you'd rather apply a "best effort" to decode images, even if the data is corrupt or invalid. (optional, defaulttrue) -
options.limitInputPixels(number | boolean) Do not process input images where the number of pixels (width x height) exceeds this limit. Assumes image dimensions contained in the input metadata can be trusted. An integral Number of pixels, zero or false to remove limit, true to use default limit of 268402689 (0x3FFF x 0x3FFF). (optional, default268402689) -
options.unlimitedboolean Set this totrueto remove safety features that help prevent memory exhaustion (SVG, PNG). (optional, defaultfalse) -
options.sequentialReadboolean Set this totrueto use sequential rather than random access where possible. This can reduce memory usage and might improve performance on some systems. (optional, defaultfalse) -
options.densitynumber number representing the DPI for vector images in the range 1 to 100000. (optional, default72) -
options.pagesnumber number of pages to extract for multi-page input (GIF, WebP, AVIF, TIFF, PDF), use -1 for all pages. (optional, default1) -
options.pagenumber page number to start extracting from for multi-page input (GIF, WebP, AVIF, TIFF, PDF), zero based. (optional, default0) -
options.subifdnumber subIFD (Sub Image File Directory) to extract for OME-TIFF, defaults to main image. (optional, default-1) -
options.levelnumber level to extract from a multi-level input (OpenSlide), zero based. (optional, default0) -
options.animatedboolean Set totrueto read all frames/pages of an animated image (equivalent of settingpagesto-1). (optional, defaultfalse) -
options.rawObject? describes raw pixel input image data. Seeraw()for pixel ordering.options.raw.widthnumber? integral number of pixels wide.options.raw.heightnumber? integral number of pixels high.options.raw.channelsnumber? integral number of channels, between 1 and 4.options.raw.premultipliedboolean? specifies that the raw input has already been premultiplied, set totrueto avoid sharp premultiplying the image. (optional, defaultfalse)
-
options.createObject? describes a new image to be created.-
options.create.widthnumber? integral number of pixels wide. -
options.create.heightnumber? integral number of pixels high. -
options.create.channelsnumber? integral number of channels, either 3 (RGB) or 4 (RGBA). -
options.create.background(string | Object)? parsed by the color module to extract values for red, green, blue and alpha. -
options.create.noiseObject? describes a noise to be created.
-
-
Examples
sharp('input.jpg')
.resize(300, 200)
.toFile('output.jpg', function(err) {
// output.jpg is a 300 pixels wide and 200 pixels high image
// containing a scaled and cropped version of input.jpg
});
// Read image data from readableStream,
// resize to 300 pixels wide,
// emit an 'info' event with calculated dimensions
// and finally write image data to writableStream
var transformer = sharp()
.resize(300)
.on('info', function(info) {
console.log('Image height is ' + info.height);
});
readableStream.pipe(transformer).pipe(writableStream);
// Create a blank 300x200 PNG image of semi-transluent red pixels
sharp({
create: {
width: 300,
height: 200,
channels: 4,
background: { r: 255, g: 0, b: 0, alpha: 0.5 }
}
})
.png()
.toBuffer()
.then( ... );
// Convert an animated GIF to an animated WebP
await sharp('in.gif', { animated: true }).toFile('out.webp');
// Read a raw array of pixels and save it to a png
const input = Uint8Array.from([255, 255, 255, 0, 0, 0]); // or Uint8ClampedArray
const image = sharp(input, {
// because the input does not contain its dimensions or how many channels it has
// we need to specify it in the constructor options
raw: {
width: 2,
height: 1,
channels: 3
}
});
await image.toFile('my-two-pixels.png');
// Generate RGB Gaussian noise
await sharp({
create: {
width: 300,
height: 200,
channels: 3,
noise: {
type: 'gaussian',
mean: 128,
sigma: 30
}
}
}).toFile('noise.png');
- Throws Error Invalid parameters
Returns Sharp
clone
Take a "snapshot" of the Sharp instance, returning a new instance. Cloned instances inherit the input of their parent instance. This allows multiple output Streams and therefore multiple processing pipelines to share a single input Stream.
Examples
const pipeline = sharp().rotate();
pipeline.clone().resize(800, 600).pipe(firstWritableStream);
pipeline.clone().extract({ left: 20, top: 20, width: 100, height: 100 }).pipe(secondWritableStream);
readableStream.pipe(pipeline);
// firstWritableStream receives auto-rotated, resized readableStream
// secondWritableStream receives auto-rotated, extracted region of readableStream
// Create a pipeline that will download an image, resize it and format it to different files
// Using Promises to know when the pipeline is complete
const fs = require("fs");
const got = require("got");
const sharpStream = sharp({
failOnError: false
});
const promises = [];
promises.push(
sharpStream
.clone()
.jpeg({ quality: 100 })
.toFile("originalFile.jpg")
);
promises.push(
sharpStream
.clone()
.resize({ width: 500 })
.jpeg({ quality: 80 })
.toFile("optimized-500.jpg")
);
promises.push(
sharpStream
.clone()
.resize({ width: 500 })
.webp({ quality: 80 })
.toFile("optimized-500.webp")
);
// https://github.com/sindresorhus/got#gotstreamurl-options
got.stream("https://www.example.com/some-file.jpg").pipe(sharpStream);
Promise.all(promises)
.then(res => { console.log("Done!", res); })
.catch(err => {
console.error("Error processing files, let's clean it up", err);
try {
fs.unlinkSync("originalFile.jpg");
fs.unlinkSync("optimized-500.jpg");
fs.unlinkSync("optimized-500.webp");
} catch (e) {}
});
Returns Sharp