An endpoint, using Fast API, that allows resizing an image.
To run the server in a docker container, and have access to logs and cached data on the host
inside /tmp/image-resizer
, use the Docker container hosted on Github packages:
docker pull ghcr.io/caarmen/image-resizer:latest
mkdir -p /tmp/image-resizer/cache
mkdir -p /tmp/image-resizer/logs
docker run \
--volume /tmp/image-resizer/cache:/var/cache/image-resizer \
--volume /tmp/image-resizer/logs:/var/log/image-resizer \
--detach \
--publish 8000:8000 \
--env WEB_CONCURRENCY=4 \
--env CACHE_VALIDITY_S=86400 \
--env CACHE_CLEAN_INTERVAL_S=86400 \
ghcr.io/caarmen/image-resizer
Then resize your first image here:
git clone https://github.com/caarmen/image-resizer.git
cd image-resizer
# optional: command to create virtual environment
pip install -r requirements/prod.txt
python -m imageresizer.main
git clone https://github.com/caarmen/image-resizer.git
cd image-resizer
docker build -t imageresizer .
docker run --detach --publish 8000:8000 imageresizer
The following options are valid whether you downloaded the image from Github packages, built the Docker image locally, or are running the server directly on your machine.
The examples specify the image from Github packages ghcr.io/caarmen/image-resizer
. If you built the image locally,
use imageresizer
instead.
By default, the server stores the cache data and logs in the root of the project when run locally, and in
/var/cache/image-resizer
and /var/log/image-resizer
in the Docker container.
You can change the location of the cache and logs locations with the CACHE_DIR
and LOG_DIR
environment variables.
For example, to store cache and logs in /tmp/image-resizer/cache
and /tmp/image-resizer/logs
:
Docker:
docker run --detach --volume /tmp/image-resizer/cache:/var/cache/image-resizer --volume /tmp/image-resizer/logs:/var/log/image-resizer --publish 8000:8000 ghcr.io/caarmen/image-resizer
Local:
CACHE_DIR=/tmp/image-resizer/cache LOG_DIR=/tmp/image-resizer/logs python -m imageresizer.main
By default, the server runs with one worker. To change this, specify the number of workers with the
WEB_CONCURRENCY
environment variable. For example, to set 4 workers:
Docker:
docker run --detach --env WEB_CONCURRENCY=4 --publish 8000:8000 ghcr.io/caarmen/image-resizer
Local:
WEB_CONCURRENCY=4 python -m imageresizer.main
By default, when purging the cache, the cache is cleaned every 24 hours starting from server launch, and images older than 24 hours are deleted.
To change this:
- set the
CACHE_VALIDITY_S
environment variable for the duration which images should be cached (in seconds). - set the
CACHE_CLEAN_INTERVAL_S
environment variable to specify the interval in seconds between cleaning tasks.
For example, to purge images older than one hour, every 2 minutes::
Docker:
docker run --detach --env CACHE_VALIDITY_S=3600 --env CACHE_CLEAN_INTERVAL_S=120 --publish 8000:8000 ghcr.io/caarmen/image-resizer
Local:
CACHE_VALIDITY_S=3600 CACHE_CLEAN_INTERVAL_S=120 python -m imageresizer.main
By default, the server runs on port 8000. To set it to run on a different port, like 8102
for example:
Docker:
docker run --detach --publish 8102:8000 ghcr.io/caarmen/image-resizer
Local:
UVICORN_PORT=8102 python -m imageresizer.main
The following generated documentation urls are exposed. They can be disabled by unsetting their environment variables.
Documentation type | Environment variable | Default value |
---|---|---|
OpenAPI json | OPENAPI_URL |
/openapi.json |
ReDoc | REDOC_URL |
/redoc |
Swagger UI | DOCS_URL |
/docs |
Example to disable all documentation urls:
Docker:
docker run --detach --env OPENAPI_URL= --env REDOC_URL= --env DOCS_URL= --publish 8000:8000 ghcr.io/caarmen/image-resizer
Local:
OPENAPI_URL= REDOC_URL= DOCS_URL= python -m imageresizer.main
By default, only the https schema is allowed for image urls. To change this, set the SUPPORTED_IMAGE_URL_SCHEMAS
environment variable.
For example, to support both https and http:
Docker:
docker run --env SUPPORTED_IMAGE_URL_SCHEMAS='["https","http"]' --detach --publish 8000:8000 ghcr.io/caarmen/image-resizer
Local:
SUPPORTED_IMAGE_URL_SCHEMAS='["https","http"]' python -m imageresizer.main
By default, no restrictions are placed on the domains of image urls. To specifically deny a list of domains, or to
specifically only allow a list of domains, set the DENIED_DOMAINS
or ALLOWED_DOMAINS
environment variables.
For example, to ban resizing images on baddomain.com:
Docker:
docker run --env DENIED_DOMAINS='["baddomain.com"]' --detach --publish 8000:8000 ghcr.io/caarmen/image-resizer
Local:
DENIED_DOMAINS='["baddomain.com"]' python -m imageresizer.main
To stop the running imageresizer containers:
for container in $(docker ps --filter ancestor=ghcr.io/caarmen/image-resizer --format="{{.ID}}"); do docker stop $container; done
To stop and delete all the imageresizer containers, a utility script is provided in this repository:
bash scripts/docker_remove_containers.bash
Browse the api docs at http://127.0.0.1:8000/docs
Use the resize
endpoint.
Example resizing the GitHub logo to 50x100:
You can browse the documentation at the following links:
- Running on the server:
- Redoc: http://localhost:8000/redoc
- Swagger: http://localhost:8000/docs
- Open api doc: http://localhost:8000/openapi.json
- Static documentation:
- Github pages: https://caarmen.github.io/image-resizer/