Sharp is a high performance Node.js library for image manipulation. This wraps it in an Express app to perform some common operations such as resizing images and converting them to better formats.
Operations are streamed directly from request to response so memory usage should be very low even for large files.
It is built into a Docker image, universityofwarwick/image-resizer, which exposes a simple API on port 3000.
No volumes are required as it is completely stateless,
converting in memory and returning the result each time.
If you want to add caching, that's best implemented by
a different service.
Have Docker.
./build_image.sh
./run_image.sh
Resize image to maximum width of 512 pixels, keeping original format. The request body should be the image data.
POST /resize?width=512
Content-type: image/png
...image dataAs above but converts to WebP.
POST /resize/webp?width=512And this converts to AVIF
POST /resize/webp?width=512You can also pass height, or both, and it will use them as a maximum while keeping the original aspect ratio.
You can also pass lossless as true or false if you wish to force lossless encoding. Otherwise it will do some basic heuristics to guess whether it looks like something that's better off lossy, like a photo saved as a PNG (which happens quite a lot).
Example curl command to resize locally:
curl -XPOST \
--data-binary @big_image.png \
http://localhost:3000/resize/webp?width=800 \
--output small.webpThe response body will be the converted image, along with some possible headers, which might help you handle the result appropriately:
Content-Typeof course reflects the new image typeX-Result-Losslessistrueorfalseto reflect the output image.X-Result-Actionslists the changes it has made, which may be one of:orientif EXIF was present and not 1scale:downif it is resizing down (we never scale up currently)convert:webporconvert:avifif the format is changing to one of these formats