Welcome to the Image Servicing and Formatting API repository. This tool allows you to create your own image hosting service with a converter and formatter.
To get started, follow these steps:
- Ensure you have Docker installed and running on your system.
- Clone this repository using Git.
- Create a
.env
file with the necessary environment variables. - Run
docker-compose up
to start the service.
You can use the following options to customize the output of your images:
quality: q_[1-100]
: Specifies the output image quality (1 to 100).width: w_[>1]
: Specifies the output image width (greater than 1).height: h_[>1]
: Specifies the output image height (greater than 1).resize: r_[cover|contain|fill|inside|outside]
: Specifies the output image resize type (default value:cover
).
After uploading an image, you will receive a URL that looks like this:
http://localhost:3000/uploads/6188f92dde5947189ec3205a.jpg
To apply multiple output image options, separate them with commas in the URL:
- Example: Resize the image to 250x250 pixels and fill all space:
http://localhost:3000/uploads/r_fill,w_250,h_250/6188f92dde5947189ec3205a.jpg
- Example: Request an image with a maximum size of 500 pixels, preserving the original aspect ratio, and with a quality of
50%
:http://localhost:3000/uploads/r_inside,w_500,h_500,q_50/6188f92dde5947189ec3205a.jpg
We currently support the following image extensions:
jpg
jpeg
png
webp
When requesting an image, you can specify the desired image extension in the URL:
http://localhost:3000/uploads/6188f92dde5947189ec3205a.<jpg|jpeg|png|webp>
When providing both width and height options, the image can be adjusted using the following methods:
cover
(default): Preserves the aspect ratio and ensures the image covers both provided dimensions by cropping/clipping to fit.contain
: Preserves the aspect ratio and fits the image within both provided dimensions using "letterboxing" where necessary.fill
: Ignores the aspect ratio of the input and stretches the image to fit both provided dimensions.inside
: Preserves the aspect ratio and resizes the image to be as large as possible while ensuring its dimensions are less than or equal to both specified values.outside
: Preserves the aspect ratio and resizes the image to be as small as possible while ensuring its dimensions are greater than or equal to both specified values.
When using the API, you may encounter the following errors and corresponding responses:
[POST] /upload
:StatusCode: 201, Message: Image uploaded
: The image was uploaded successfully, and the response will contain{ fileId: 'uploaded_image_id', link: 'direct_link_to_uploaded_image' }
.StatusCode: 400, Message: Failed to upload image
: Occurs in case of any other errors that happen during the image upload process. Please check your logs for details.StatusCode: 401, Message: Not authorized
: Occurs when a secret key is required but not provided.StatusCode: 406, Message: File not passed
StatusCode: 415, Message: File mime type not supported
StatusCode: 415, Message: File extension not supported
StatusCode: 413, Message: Max image size exceeded
: Occurs when the uploaded image file size exceeds theenv.MAX_IMAGE_SIZE
property.
To test the server load, you can use the autocannon
tool with the following command:
autocannon -c 100 -d 5 -p 10 http://localhost:3000/
Find autocannon
here: https://github.com/mcollina/autocannon
We chose to use the 'Sharp' image processing library due to its excellent performance and powerful features. You can learn more about its performance here: https://sharp.pixelplumbing.com/performance
For detailed information about the 'Sharp' API, visit: https://sharp.pixelplumbing.com/api-constructor
By following these instructions, you can set up your own image hosting service with versatile image formatting options. Enjoy using the Image Servicing and Formatting API!