mirror of
https://github.com/lovell/sharp.git
synced 2026-02-09 08:06:14 +01:00
Add toArrayBuffer to generate transferable output #4355
This commit is contained in:
@@ -306,6 +306,7 @@ const Sharp = function (input, options) {
|
||||
fileOut: '',
|
||||
formatOut: 'input',
|
||||
streamOut: false,
|
||||
sharedOut: true,
|
||||
keepMetadata: 0,
|
||||
withMetadataOrientation: -1,
|
||||
withMetadataDensity: 0,
|
||||
|
||||
7
lib/index.d.ts
vendored
7
lib/index.d.ts
vendored
@@ -693,6 +693,13 @@ declare namespace sharp {
|
||||
*/
|
||||
toBuffer(options: { resolveWithObject: true }): Promise<{ data: Buffer; info: OutputInfo }>;
|
||||
|
||||
/**
|
||||
* Write output to a transferable ArrayBuffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and RAW output are supported.
|
||||
* By default, the format will match the input image, except SVG input which becomes PNG output.
|
||||
* @returns A promise that resolves with an object containing the ArrayBuffer data and an info object containing the output image format, size (bytes), width, height and channels
|
||||
*/
|
||||
toArrayBuffer(): Promise<{ data: ArrayBuffer; info: OutputInfo }>;
|
||||
|
||||
/**
|
||||
* Keep all EXIF metadata from the input image in the output image.
|
||||
* EXIF metadata is unsupported for TIFF output.
|
||||
|
||||
@@ -164,6 +164,45 @@ function toBuffer (options, callback) {
|
||||
return this._pipeline(is.fn(options) ? options : callback, stack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write output to a transferable `ArrayBuffer`.
|
||||
* JPEG, PNG, WebP, AVIF, TIFF, GIF and raw pixel data output are supported.
|
||||
*
|
||||
* Use {@link #toformat toFormat} or one of the format-specific functions such as {@link #jpeg jpeg}, {@link #png png} etc. to set the output format.
|
||||
*
|
||||
* If no explicit format is set, the output format will match the input image, except SVG input which becomes PNG output.
|
||||
*
|
||||
* By default all metadata will be removed, which includes EXIF-based orientation.
|
||||
* See {@link #keepexif keepExif} and similar methods for control over this.
|
||||
*
|
||||
* Resolves with an `Object` containing:
|
||||
* - `data` is the output image as a transferable `ArrayBuffer`.
|
||||
* - `info` contains properties relating to the output image such as `width` and `height`.
|
||||
*
|
||||
* This method does not work with the Electron V8 memory cage
|
||||
* and will reject with an error if used in that environment.
|
||||
* Use {@link #tobuffer toBuffer} instead.
|
||||
*
|
||||
* @since v0.35.0
|
||||
*
|
||||
* @example
|
||||
* const { data, info } = await sharp(input).toArrayBuffer();
|
||||
*
|
||||
* @example
|
||||
* const { data } = await sharp(input)
|
||||
* .avif()
|
||||
* .toArrayBuffer();
|
||||
* const uint8Array = new Uint8Array(data);
|
||||
*
|
||||
* @returns {Promise<{ data: ArrayBuffer, info: Object }>}
|
||||
*/
|
||||
function toArrayBuffer () {
|
||||
this.options.resolveWithObject = true;
|
||||
this.options.sharedOut = true;
|
||||
const stack = Error();
|
||||
return this._pipeline(null, stack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep all EXIF metadata from the input image in the output image.
|
||||
*
|
||||
@@ -1659,6 +1698,7 @@ module.exports = (Sharp) => {
|
||||
// Public
|
||||
toFile,
|
||||
toBuffer,
|
||||
toArrayBuffer,
|
||||
keepExif,
|
||||
withExif,
|
||||
withExifMerge,
|
||||
|
||||
Reference in New Issue
Block a user