Skip to content

Commit ec26c8a

Browse files
committedMar 21, 2021
Docs: ensure toBuffer pixel example works #2624
1 parent da43a30 commit ec26c8a

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed
 

‎docs/api-output.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,20 @@ sharp(input)
8787
```
8888

8989
```javascript
90-
const data = await sharp('my-image.jpg')
90+
const { data, info } = await sharp('my-image.jpg')
9191
// output the raw pixels
9292
.raw()
93-
.toBuffer();
93+
.toBuffer({ resolveWithObject: true });
9494

9595
// create a more type safe way to work with the raw pixel data
9696
// this will not copy the data, instead it will change `data`s underlying ArrayBuffer
9797
// so `data` and `pixelArray` point to the same memory location
9898
const pixelArray = new Uint8ClampedArray(data.buffer);
9999

100100
// When you are done changing the pixelArray, sharp takes the `pixelArray` as an input
101-
await sharp(pixelArray).toFile('my-changed-image.jpg');
101+
const { width, height, channels } = info;
102+
await sharp(pixelArray, { raw: { width, height, channels } })
103+
.toFile('my-changed-image.jpg');
102104
```
103105

104106
Returns **[Promise][5]<[Buffer][8]>** when no callback is provided

‎lib/output.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,20 @@ function toFile (fileOut, callback) {
105105
* .catch(err => { ... });
106106
*
107107
* @example
108-
* const data = await sharp('my-image.jpg')
108+
* const { data, info } = await sharp('my-image.jpg')
109109
* // output the raw pixels
110110
* .raw()
111-
* .toBuffer();
111+
* .toBuffer({ resolveWithObject: true });
112112
*
113113
* // create a more type safe way to work with the raw pixel data
114114
* // this will not copy the data, instead it will change `data`s underlying ArrayBuffer
115115
* // so `data` and `pixelArray` point to the same memory location
116116
* const pixelArray = new Uint8ClampedArray(data.buffer);
117117
*
118118
* // When you are done changing the pixelArray, sharp takes the `pixelArray` as an input
119-
* await sharp(pixelArray).toFile('my-changed-image.jpg');
119+
* const { width, height, channels } = info;
120+
* await sharp(pixelArray, { raw: { width, height, channels } })
121+
* .toFile('my-changed-image.jpg');
120122
*
121123
* @param {Object} [options]
122124
* @param {boolean} [options.resolveWithObject] Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.

0 commit comments

Comments
 (0)
Please sign in to comment.