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 |
|
5 | The typical use case for this high speed Node-API module
|
6 | is to convert large images in common formats to
|
7 | smaller, web-friendly JPEG, PNG, WebP, GIF and AVIF images of varying dimensions.
|
8 |
|
9 | It can be used with all JavaScript runtimes
|
10 | that provide support for Node-API v9, including
|
11 | Node.js (^18.17.0 or >= 20.3.0), Deno and Bun.
|
12 |
|
13 | Resizing an image is typically 4x-5x faster than using the
|
14 | quickest ImageMagick and GraphicsMagick settings
|
15 | due to its use of [libvips](https://github.com/libvips/libvips).
|
16 |
|
17 | Colour spaces, embedded ICC profiles and alpha transparency channels are all handled correctly.
|
18 | Lanczos resampling ensures quality is not sacrificed for speed.
|
19 |
|
20 | As well as image resizing, operations such as
|
21 | rotation, extraction, compositing and gamma correction are available.
|
22 |
|
23 | Most modern macOS, Windows and Linux systems
|
24 | do not require any additional install or runtime dependencies.
|
25 |
|
26 | ## Documentation
|
27 |
|
28 | Visit [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
|
37 | npm install sharp
|
38 | ```
|
39 |
|
40 | ```javascript
|
41 | const sharp = require('sharp');
|
42 | ```
|
43 |
|
44 | ### Callback
|
45 |
|
46 | ```javascript
|
47 | sharp(inputBuffer)
|
48 | .resize(320, 240)
|
49 | .toFile('output.webp', (err, info) => { ... });
|
50 | ```
|
51 |
|
52 | ### Promise
|
53 |
|
54 | ```javascript
|
55 | sharp('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
|
67 | const 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
|
82 | const roundedCorners = Buffer.from(
|
83 | '<svg><rect x="0" y="0" width="200" height="200" rx="50" ry="50"/></svg>'
|
84 | );
|
85 |
|
86 | const roundedCornerResizer =
|
87 | sharp()
|
88 | .resize(200, 200)
|
89 | .composite([{
|
90 | input: roundedCorners,
|
91 | blend: 'dest-in'
|
92 | }])
|
93 | .png();
|
94 |
|
95 | readableStream
|
96 | .pipe(roundedCornerResizer)
|
97 | .pipe(writableStream);
|
98 | ```
|
99 |
|
100 | ## Contributing
|
101 |
|
102 | A [guide for contributors](https://github.com/lovell/sharp/blob/main/.github/CONTRIBUTING.md)
|
103 | covers reporting bugs, requesting features and submitting code changes.
|
104 |
|
105 | ## Licensing
|
106 |
|
107 | Copyright 2013 Lovell Fuller and others.
|
108 |
|
109 | Licensed under the Apache License, Version 2.0 (the "License");
|
110 | you may not use this file except in compliance with the License.
|
111 | You 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 |
|
114 | Unless required by applicable law or agreed to in writing, software
|
115 | distributed under the License is distributed on an "AS IS" BASIS,
|
116 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
117 | See the License for the specific language governing permissions and
|
118 | limitations under the License.
|