Docs: refresh usage examples

This commit is contained in:
Lovell Fuller 2019-03-18 21:29:17 +00:00
parent 83cdb558f6
commit 2bfea0ad76

View File

@ -6,10 +6,6 @@
npm install sharp npm install sharp
``` ```
```sh
yarn add sharp
```
The typical use case for this high speed Node.js module The typical use case for this high speed Node.js module
is to convert large images in common formats to is to convert large images in common formats to
smaller, web-friendly JPEG, PNG and WebP images of varying dimensions. smaller, web-friendly JPEG, PNG and WebP images of varying dimensions.
@ -33,22 +29,42 @@ do not require any additional install or runtime dependencies.
const sharp = require('sharp'); const sharp = require('sharp');
``` ```
### Callback
```javascript ```javascript
sharp(inputBuffer) sharp(inputBuffer)
.resize(320, 240) .resize(320, 240)
.toFile('output.webp', (err, info) => ... ); .toFile('output.webp', (err, info) => { ... });
// A Promises/A+ promise is returned when callback is not provided.
``` ```
### Promise
```javascript ```javascript
sharp('input.jpg') sharp('input.jpg')
.rotate() .rotate()
.resize(200) .resize(200)
.toBuffer() .toBuffer()
.then( data => ... ) .then( data => { ... })
.catch( err => ... ); .catch( err => { ... });
``` ```
### Async/await
```javascript
const semiTransparentRedPng = await sharp({
create: {
width: 48,
height: 48,
channels: 4,
background: { r: 255, g: 0, b: 0, alpha: 0.5 }
}
})
.png()
.toBuffer();
```
### Stream
```javascript ```javascript
const roundedCorners = Buffer.from( const roundedCorners = Buffer.from(
'<svg><rect x="0" y="0" width="200" height="200" rx="50" ry="50"/></svg>' '<svg><rect x="0" y="0" width="200" height="200" rx="50" ry="50"/></svg>'
@ -57,7 +73,10 @@ const roundedCorners = Buffer.from(
const roundedCornerResizer = const roundedCornerResizer =
sharp() sharp()
.resize(200, 200) .resize(200, 200)
.overlayWith(roundedCorners, { cutout: true }) .composite([{
input: roundedCorners,
blend: 'dest-in'
}])
.png(); .png();
readableStream readableStream