https://github.com/intob/sharp-server
Node server to handle image processing using sharp
https://github.com/intob/sharp-server
http-server image nodejs optimization
Last synced: 2 months ago
JSON representation
Node server to handle image processing using sharp
- Host: GitHub
- URL: https://github.com/intob/sharp-server
- Owner: intob
- Created: 2021-06-05T19:43:49.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-07-23T15:40:23.000Z (over 4 years ago)
- Last Synced: 2025-10-13T12:16:13.719Z (3 months ago)
- Topics: http-server, image, nodejs, optimization
- Language: JavaScript
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# sharp-server
## Convert any image with a HTTP POST request
- Send a HTTP POST request
- Let [sharp](https://github.com/lovell/sharp) convert/optimize your image
- Do something with the response
## Server configuration
This repo uses a node HTTPS server.
You should export an object with at least a `key` & `cert` from your `config.js`.
See [how to create node HTTPS server](https://nodejs.org/en/knowledge/HTTP/servers/how-to-create-a-HTTPS-server/) for more details.
### Basic
```javascript
const fs = require("fs");
const options = {
key: fs.readFileSync("./privkey.pem"),
cert: fs.readFileSync("./cert.pem")
};
module.exports = options;
```
## Start server
`npm run start /path/to/my/config.js`
## Accept
Set the `accept` header with the desired output MIME type.
e.g. `"accept": "image/webp"`
## Options
### Resize
Set the `resize-options` header, with a JSON string of [sharp resize options](https://sharp.pixelplumbing.com/api-resize).
### Output
Set the `output-options` header, with a JSON string of [sharp output options](https://sharp.pixelplumbing.com/api-output)
relevant to the output type set in `accept` header.
## Example usage
### Using defaults
```javascript
fetch("my.domain.io",{
method: "POST",
headers: {
"content-type": "image/jpeg",
"accept": "image/webp"
},
body: arrayBuffer
});
```
### Setting resize & output options
#### AVIF output
```javascript
fetch("my.domain.io",{
method: "POST",
headers: {
"content-type": "image/jpeg",
"accept": "image/avif"
"resize-options": { "width": 750, "withoutEnlargement": true }
"output-options": { "speed": 8 }
},
body: arrayBuffer
});
```
#### WebP output
```javascript
fetch("my.domain.io",{
method: "POST",
headers: {
"content-type": "image/jpeg",
"accept": "image/webp"
"resize-options": { "width": 750, "withoutEnlargement": true }
"output-options": { "reductionEffort": 6 }
},
body: arrayBuffer
});
```