mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 02:30:12 +02:00
Docs: refresh usage examples
This commit is contained in:
parent
83cdb558f6
commit
2bfea0ad76
37
README.md
37
README.md
@ -6,10 +6,6 @@
|
||||
npm install sharp
|
||||
```
|
||||
|
||||
```sh
|
||||
yarn add sharp
|
||||
```
|
||||
|
||||
The typical use case for this high speed Node.js module
|
||||
is to convert large images in common formats to
|
||||
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');
|
||||
```
|
||||
|
||||
### Callback
|
||||
|
||||
```javascript
|
||||
sharp(inputBuffer)
|
||||
.resize(320, 240)
|
||||
.toFile('output.webp', (err, info) => ... );
|
||||
// A Promises/A+ promise is returned when callback is not provided.
|
||||
.toFile('output.webp', (err, info) => { ... });
|
||||
```
|
||||
|
||||
### Promise
|
||||
|
||||
```javascript
|
||||
sharp('input.jpg')
|
||||
.rotate()
|
||||
.resize(200)
|
||||
.toBuffer()
|
||||
.then( data => ... )
|
||||
.catch( err => ... );
|
||||
.then( data => { ... })
|
||||
.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
|
||||
const roundedCorners = Buffer.from(
|
||||
'<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 =
|
||||
sharp()
|
||||
.resize(200, 200)
|
||||
.overlayWith(roundedCorners, { cutout: true })
|
||||
.composite([{
|
||||
input: roundedCorners,
|
||||
blend: 'dest-in'
|
||||
}])
|
||||
.png();
|
||||
|
||||
readableStream
|
||||
|
Loading…
x
Reference in New Issue
Block a user