Docs: ensure toBuffer pixel example works #2624

This commit is contained in:
Lovell Fuller 2021-03-21 20:54:09 +00:00
parent da43a3055f
commit ec26c8aa49
2 changed files with 10 additions and 6 deletions

View File

@ -87,10 +87,10 @@ sharp(input)
``` ```
```javascript ```javascript
const data = await sharp('my-image.jpg') const { data, info } = await sharp('my-image.jpg')
// output the raw pixels // output the raw pixels
.raw() .raw()
.toBuffer(); .toBuffer({ resolveWithObject: true });
// create a more type safe way to work with the raw pixel data // create a more type safe way to work with the raw pixel data
// this will not copy the data, instead it will change `data`s underlying ArrayBuffer // this will not copy the data, instead it will change `data`s underlying ArrayBuffer
@ -98,7 +98,9 @@ const data = await sharp('my-image.jpg')
const pixelArray = new Uint8ClampedArray(data.buffer); const pixelArray = new Uint8ClampedArray(data.buffer);
// When you are done changing the pixelArray, sharp takes the `pixelArray` as an input // When you are done changing the pixelArray, sharp takes the `pixelArray` as an input
await sharp(pixelArray).toFile('my-changed-image.jpg'); const { width, height, channels } = info;
await sharp(pixelArray, { raw: { width, height, channels } })
.toFile('my-changed-image.jpg');
``` ```
Returns **[Promise][5]<[Buffer][8]>** when no callback is provided Returns **[Promise][5]<[Buffer][8]>** when no callback is provided

View File

@ -105,10 +105,10 @@ function toFile (fileOut, callback) {
* .catch(err => { ... }); * .catch(err => { ... });
* *
* @example * @example
* const data = await sharp('my-image.jpg') * const { data, info } = await sharp('my-image.jpg')
* // output the raw pixels * // output the raw pixels
* .raw() * .raw()
* .toBuffer(); * .toBuffer({ resolveWithObject: true });
* *
* // create a more type safe way to work with the raw pixel data * // create a more type safe way to work with the raw pixel data
* // this will not copy the data, instead it will change `data`s underlying ArrayBuffer * // this will not copy the data, instead it will change `data`s underlying ArrayBuffer
@ -116,7 +116,9 @@ function toFile (fileOut, callback) {
* const pixelArray = new Uint8ClampedArray(data.buffer); * const pixelArray = new Uint8ClampedArray(data.buffer);
* *
* // When you are done changing the pixelArray, sharp takes the `pixelArray` as an input * // When you are done changing the pixelArray, sharp takes the `pixelArray` as an input
* await sharp(pixelArray).toFile('my-changed-image.jpg'); * const { width, height, channels } = info;
* await sharp(pixelArray, { raw: { width, height, channels } })
* .toFile('my-changed-image.jpg');
* *
* @param {Object} [options] * @param {Object} [options]
* @param {boolean} [options.resolveWithObject] Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`. * @param {boolean} [options.resolveWithObject] Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.