mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
docs(clone): add promise example
This commit is contained in:
parent
6ed1f49ad3
commit
1eedb22ef5
@ -89,6 +89,55 @@ readableStream.pipe(pipeline);
|
||||
// secondWritableStream receives auto-rotated, extracted region of readableStream
|
||||
```
|
||||
|
||||
```javascript
|
||||
// Create a pipeline that will download an image, resize it and format it to different files
|
||||
// Using Promises to know when the pipeline is complete
|
||||
const fs = require("fs");
|
||||
const got = require("got");
|
||||
const sharpStream = sharp({
|
||||
failOnError: false
|
||||
});
|
||||
|
||||
const promises = [];
|
||||
|
||||
promises.push(
|
||||
sharpStream
|
||||
.clone()
|
||||
.jpeg({ quality: 100 })
|
||||
.toFile("originalFile.jpg")
|
||||
);
|
||||
|
||||
promises.push(
|
||||
sharpStream
|
||||
.clone()
|
||||
.resize({ width: 500 })
|
||||
.jpeg({ quality: 80 })
|
||||
.toFile("optimized-500.jpg")
|
||||
);
|
||||
|
||||
promises.push(
|
||||
sharpStream
|
||||
.clone()
|
||||
.resize({ width: 500 })
|
||||
.webp({ quality: 80 })
|
||||
.toFile("optimized-500.webp")
|
||||
);
|
||||
|
||||
// https://github.com/sindresorhus/got#gotstreamurl-options
|
||||
got.stream("https://www.example.com/some-file.jpg").pipe(sharpStream);
|
||||
|
||||
Promise.all(promises)
|
||||
.then(res => { console.log("Done!", res); })
|
||||
.catch(err => {
|
||||
console.error("Error processing files, let's clean it up", err);
|
||||
try {
|
||||
fs.unlinkSync("originalFile.jpg");
|
||||
fs.unlinkSync("optimized-500.jpg");
|
||||
fs.unlinkSync("optimized-500.webp");
|
||||
} catch (e) {}
|
||||
});
|
||||
```
|
||||
|
||||
Returns **[Sharp][8]**
|
||||
|
||||
[1]: https://nodejs.org/api/buffer.html
|
||||
|
@ -253,6 +253,54 @@ util.inherits(Sharp, stream.Duplex);
|
||||
* // firstWritableStream receives auto-rotated, resized readableStream
|
||||
* // secondWritableStream receives auto-rotated, extracted region of readableStream
|
||||
*
|
||||
* @example
|
||||
* // Create a pipeline that will download an image, resize it and format it to different files
|
||||
* // Using Promises to know when the pipeline is complete
|
||||
* const fs = require("fs");
|
||||
* const got = require("got");
|
||||
* const sharpStream = sharp({
|
||||
* failOnError: false
|
||||
* });
|
||||
*
|
||||
* const promises = [];
|
||||
*
|
||||
* promises.push(
|
||||
* sharpStream
|
||||
* .clone()
|
||||
* .jpeg({ quality: 100 })
|
||||
* .toFile("originalFile.jpg")
|
||||
* );
|
||||
*
|
||||
* promises.push(
|
||||
* sharpStream
|
||||
* .clone()
|
||||
* .resize({ width: 500 })
|
||||
* .jpeg({ quality: 80 })
|
||||
* .toFile("optimized-500.jpg")
|
||||
* );
|
||||
*
|
||||
* promises.push(
|
||||
* sharpStream
|
||||
* .clone()
|
||||
* .resize({ width: 500 })
|
||||
* .webp({ quality: 80 })
|
||||
* .toFile("optimized-500.webp")
|
||||
* );
|
||||
*
|
||||
* // https://github.com/sindresorhus/got#gotstreamurl-options
|
||||
* got.stream("https://www.example.com/some-file.jpg").pipe(sharpStream);
|
||||
*
|
||||
* Promise.all(promises)
|
||||
* .then(res => { console.log("Done!", res); })
|
||||
* .catch(err => {
|
||||
* console.error("Error processing files, let's clean it up", err);
|
||||
* try {
|
||||
* fs.unlinkSync("originalFile.jpg");
|
||||
* fs.unlinkSync("optimized-500.jpg");
|
||||
* fs.unlinkSync("optimized-500.webp");
|
||||
* } catch (e) {}
|
||||
* });
|
||||
*
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
function clone () {
|
||||
|
Loading…
x
Reference in New Issue
Block a user