UNPKG

3.19 kBMarkdownView Raw
1# sharp
2
3<img src="https://cdn.jsdelivr.net/gh/lovell/sharp@main/docs/image/sharp-logo.svg" width="160" height="160" alt="sharp logo" align="right">
4
5The typical use case for this high speed Node-API module
6is to convert large images in common formats to
7smaller, web-friendly JPEG, PNG, WebP, GIF and AVIF images of varying dimensions.
8
9It can be used with all JavaScript runtimes
10that provide support for Node-API v9, including
11Node.js (^18.17.0 or >= 20.3.0), Deno and Bun.
12
13Resizing an image is typically 4x-5x faster than using the
14quickest ImageMagick and GraphicsMagick settings
15due to its use of [libvips](https://github.com/libvips/libvips).
16
17Colour spaces, embedded ICC profiles and alpha transparency channels are all handled correctly.
18Lanczos resampling ensures quality is not sacrificed for speed.
19
20As well as image resizing, operations such as
21rotation, extraction, compositing and gamma correction are available.
22
23Most modern macOS, Windows and Linux systems
24do not require any additional install or runtime dependencies.
25
26## Documentation
27
28Visit [sharp.pixelplumbing.com](https://sharp.pixelplumbing.com/) for complete
29[installation instructions](https://sharp.pixelplumbing.com/install),
30[API documentation](https://sharp.pixelplumbing.com/api-constructor),
31[benchmark tests](https://sharp.pixelplumbing.com/performance) and
32[changelog](https://sharp.pixelplumbing.com/changelog).
33
34## Examples
35
36```sh
37npm install sharp
38```
39
40```javascript
41const sharp = require('sharp');
42```
43
44### Callback
45
46```javascript
47sharp(inputBuffer)
48 .resize(320, 240)
49 .toFile('output.webp', (err, info) => { ... });
50```
51
52### Promise
53
54```javascript
55sharp('input.jpg')
56 .rotate()
57 .resize(200)
58 .jpeg({ mozjpeg: true })
59 .toBuffer()
60 .then( data => { ... })
61 .catch( err => { ... });
62```
63
64### Async/await
65
66```javascript
67const semiTransparentRedPng = await sharp({
68 create: {
69 width: 48,
70 height: 48,
71 channels: 4,
72 background: { r: 255, g: 0, b: 0, alpha: 0.5 }
73 }
74})
75 .png()
76 .toBuffer();
77```
78
79### Stream
80
81```javascript
82const roundedCorners = Buffer.from(
83 '<svg><rect x="0" y="0" width="200" height="200" rx="50" ry="50"/></svg>'
84);
85
86const roundedCornerResizer =
87 sharp()
88 .resize(200, 200)
89 .composite([{
90 input: roundedCorners,
91 blend: 'dest-in'
92 }])
93 .png();
94
95readableStream
96 .pipe(roundedCornerResizer)
97 .pipe(writableStream);
98```
99
100## Contributing
101
102A [guide for contributors](https://github.com/lovell/sharp/blob/main/.github/CONTRIBUTING.md)
103covers reporting bugs, requesting features and submitting code changes.
104
105## Licensing
106
107Copyright 2013 Lovell Fuller and others.
108
109Licensed under the Apache License, Version 2.0 (the "License");
110you may not use this file except in compliance with the License.
111You may obtain a copy of the License at
112[https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0)
113
114Unless required by applicable law or agreed to in writing, software
115distributed under the License is distributed on an "AS IS" BASIS,
116WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
117See the License for the specific language governing permissions and
118limitations under the License.