Add toArrayBuffer to generate transferable output #4355

This commit is contained in:
Lovell Fuller
2025-12-22 15:05:33 +00:00
parent e1bad5470e
commit 6176f53f83
11 changed files with 142 additions and 7 deletions

View File

@@ -117,6 +117,42 @@ await sharp(pixelArray, { raw: { width, height, channels } })
```
## toArrayBuffer
> toArrayBuffer() ⇒ <code>Promise.&lt;{data: ArrayBuffer, info: Object}&gt;</code>
Write output to a transferable `ArrayBuffer`.
JPEG, PNG, WebP, AVIF, TIFF, GIF and raw pixel data output are supported.
Use [toFormat](#toformat) or one of the format-specific functions such as [jpeg](#jpeg), [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 [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 [toBuffer](#tobuffer) instead.
**Since**: v0.35.0
**Example**
```js
const { data, info } = await sharp(input).toArrayBuffer();
```
**Example**
```js
const { data } = await sharp(input)
.avif()
.toArrayBuffer();
const uint8Array = new Uint8Array(data);
```
## keepExif
> keepExif() ⇒ <code>Sharp</code>

View File

@@ -12,3 +12,6 @@ slug: changelog/v0.35.0
* Add `withGainMap` to process HDR JPEG images with embedded gain maps.
[#4314](https://github.com/lovell/sharp/issues/4314)
* Add `toArrayBuffer` to generate output image as a transferable `ArrayBuffer`.
[#4355](https://github.com/lovell/sharp/issues/4355)