9.6 KiB
toFile
Write output image data to a file.
If an explicit output format is not selected, it will be inferred from the extension, with JPEG, PNG, WebP, TIFF, DZI, and libvips' V format supported. Note that raw pixel data is only supported for buffer output.
A Promises/A+ promise is returned when callback is not provided.
Parameters
-
fileOutString the path to write the image data to. -
callbackFunction? called on completion with two arguments(err, info).infocontains the output imageformat,size(bytes),width,heightandchannels. -
Throws Error Invalid parameters
Returns Promise<Object> when no callback is provided
toBuffer
Write output to a Buffer.
By default, the format will match the input image. JPEG, PNG, WebP, and RAW are supported.
callback, if present, gets three arguments (err, buffer, info) where:
erris an error message, if any.bufferis the output image data.infocontains the output imageformat,size(bytes),width,heightandchannels. A Promises/A+ promise is returned whencallbackis not provided.
Parameters
callbackFunction?
Returns Promise<Buffer> when no callback is provided
withMetadata
Include all metadata (EXIF, XMP, IPTC) from the input image in the output image.
The default behaviour, when withMetadata is not used, is to strip all metadata and convert to the device-independent sRGB colour space.
This will also convert to and add a web-friendly sRGB ICC profile.
Parameters
-
withMetadataObject?withMetadata.orientationNumber? value between 1 and 8, used to update the EXIFOrientationtag.
-
Throws Error Invalid parameters
Returns Sharp
jpeg
Use these JPEG options for output image.
Parameters
-
optionsObject? output optionsoptions.qualityNumber? quality, integer 1-100 (optional, default80)options.progressiveBoolean? use progressive (interlace) scan (optional, defaultfalse)options.chromaSubsamplingString? set to '4:4:4' to prevent chroma subsampling when quality <= 90 (optional, default'4:2:0')options.trellisQuantisationBoolean? apply trellis quantisation, requires mozjpeg (optional, defaultfalse)options.overshootDeringingBoolean? apply overshoot deringing, requires mozjpeg (optional, defaultfalse)options.optimiseScansBoolean? optimise progressive scans, forces progressive, requires mozjpeg (optional, defaultfalse)options.optimizeScansBoolean? alternative spelling of optimiseScans (optional, defaultfalse)options.forceBoolean? force JPEG output, otherwise attempt to use input format (optional, defaulttrue)
-
Throws Error Invalid options
Returns Sharp
png
Use these PNG options for output image.
Parameters
-
optionsObject?options.progressiveBoolean? use progressive (interlace) scan (optional, defaultfalse)options.compressionLevelNumber? zlib compression level (optional, default6)options.adaptiveFilteringBoolean? use adaptive row filtering (optional, defaulttrue)options.forceBoolean? force PNG output, otherwise attempt to use input format (optional, defaulttrue)
-
Throws Error Invalid options
Returns Sharp
webp
Use these WebP options for output image.
Parameters
Returns Sharp
tiff
Use these TIFF options for output image.
Parameters
Returns Sharp
raw
Force output to be raw, uncompressed uint8 pixel data.
Returns Sharp
toFormat
Force output to a given format.
Parameters
-
format(String | Object) as a String or an Object with an 'id' attribute -
optionsObject output options -
Throws Error unsupported format or options
Returns Sharp
tile
Use tile-based deep zoom (image pyramid) output.
Set the format and options for tile images via the toFormat, jpeg, png or webp functions.
Use a .zip or .szi file extension with toFile to write to a compressed archive file format.
Parameters
tileObject?tile.sizeNumber? tile size in pixels, a value between 1 and 8192. (optional, default256)tile.overlapNumber? tile overlap in pixels, a value between 0 and 8192. (optional, default0)tile.containerString? tile container, with valuefs(filesystem) orzip(compressed file). (optional, default'fs')tile.layoutString? filesystem layout, possible values aredz,zoomifyorgoogle. (optional, default'dz')
Examples
sharp('input.tiff')
.png()
.tile({
size: 512
})
.toFile('output.dz', function(err, info) {
// output.dzi is the Deep Zoom XML definition
// output_files contains 512x512 tiles grouped by zoom level
});
- Throws Error Invalid parameters
Returns Sharp