Imagor is a fast, Docker-ready image processing server written in Go.
Imagor uses one of the most efficient image processing library libvips. It is typically 4-8x faster than using the quickest ImageMagick and GraphicsMagick settings.
Imagor is a Go application that is highly optimized for concurrent requests. It is ready to be installed and used in any Unix environment, and ready to be containerized using Docker.
Imagor adopts the Thumbor URL syntax and supports tons of image processing use cases representing a lightweight, high-performance drop-in replacement.
docker run -p 8000:8000 ghcr.io/cshum/imagor -imagor-unsafe -imagor-auto-webp
Original images:
https://raw.githubusercontent.com/cshum/imagor/master/testdata/gopher.png
https://raw.githubusercontent.com/cshum/imagor/master/testdata/dancing-banana.gif
https://raw.githubusercontent.com/cshum/imagor/master/testdata/gopher-front.png
Try out the following image URLs:
http://localhost:8000/unsafe/fit-in/200x200/filters:fill(white)/https://raw.githubusercontent.com/cshum/imagor/master/testdata/gopher.png
http://localhost:8000/unsafe/200x200/smart/filters:fill(white):format(jpeg):quality(80)/https://raw.githubusercontent.com/cshum/imagor/master/testdata/gopher.png
http://localhost:8000/unsafe/fit-in/-180x180/10x10/filters:hue(290):saturation(100):fill(yellow)/raw.githubusercontent.com/cshum/imagor/master/testdata/gopher.png
http://localhost:8000/unsafe/30x40:100x150/filters:fill(cyan)/raw.githubusercontent.com/cshum/imagor/master/testdata/dancing-banana.gif
http://localhost:8000/unsafe/fit-in/200x150/filters:fill(yellow):watermark(raw.githubusercontent.com/cshum/imagor/master/testdata/gopher-front.png,repeat,bottom,0,40,40)/raw.githubusercontent.com/cshum/imagor/master/testdata/dancing-banana.gif
Imagor endpoint is a series of URL parts which defines the image operations, followed by the image URI:
/HASH|unsafe/trim/AxB:CxD/fit-in/stretch/-Ex-F/GxH:IxJ/HALIGN/VALIGN/smart/filters:NAME(ARGS):NAME(ARGS):.../IMAGE
HASH
is the URL Signature hash, orunsafe
if unsafe mode is usedtrim
removes surrounding space in images using top-left pixel colorAxB:CxD
means manually crop the image at left-top pointAxB
and right-bottom pointCxD
. Coordinates can also be provided as float values between 0 and 1 (percentage of image dimensions)fit-in
means that the generated image should not be auto-cropped and otherwise just fit in an imaginary box specified byExF
stretch
means resize the image toExF
without keeping its aspect ratios-Ex-F
means resize the image to beExF
of width per height size. The minus signs mean flip horizontally and verticallyGxH:IxJ
add left-top paddingGxH
and right-bottom paddingIxJ
HALIGN
is horizontal alignment of crop. Acceptsleft
,right
orcenter
, defaults tocenter
VALIGN
is vertical alignment of crop. Acceptstop
,bottom
ormiddle
, defaults tomiddle
smart
means using smart detection of focal pointsfilters
a pipeline of image filter operations to be applied, see filters sectionIMAGE
is the image URI
Imagor provides utilities for previewing and generating Imagor endpoint URI, including the imagorpath Go package and the /params
endpoint:
Prepending /params
to the existing endpoint returns the endpoint attributes in JSON form, useful for preview:
curl http://localhost:8000/params/g5bMqZvxaQK65qFPaP1qlJOTuLM=/fit-in/500x400/0x20/filters:fill(white)/raw.githubusercontent.com/cshum/imagor/master/testdata/gopher.png
{
"path": "fit-in/500x400/0x20/filters:fill(white)/raw.githubusercontent.com/cshum/imagor/master/testdata/gopher.png",
"image": "raw.githubusercontent.com/cshum/imagor/master/testdata/gopher.png",
"hash": "g5bMqZvxaQK65qFPaP1qlJOTuLM=",
"fit_in": true,
"width": 500,
"height": 400,
"padding_top": 20,
"padding_bottom": 20,
"filters": [
{
"name": "fill",
"args": "white"
}
]
}
Filters /filters:NAME(ARGS):NAME(ARGS):.../
is a pipeline of image operations that will be sequentially applied to the image. Examples:
/filters:fill(white):format(jpeg)/
/filters:hue(290):saturation(100):fill(yellow):format(jpeg):quality(80)/
/filters:fill(white):watermark(raw.githubusercontent.com/cshum/imagor/master/testdata/gopher-front.png,repeat,bottom,10):format(jpeg)/
Imagor supports the following filters:
background_color(color)
sets the background color of a transparent imagecolor
the color name or hexadecimal rgb expression without the “#” character
blur(sigma)
applies gaussian blur to the imagebrightness(amount)
increases or decreases the image brightnessamount
-100 to 100, the amount in % to increase or decrease the image brightness
contrast(amount)
increases or decreases the image contrastamount
-100 to 100, the amount in % to increase or decrease the image contrast
fill(color)
fill the missing area or transparent image with the specified color:color
- color name or hexadecimal rgb expression without the “#” character- If color is "blur" - missing parts are filled with blurred original image.
- If color is "auto" - the top left image pixel will be chosen as the filling color
focal(AxB:CxD)
adds a focal region for custom transformations, coordinated by left-top pointAxB
and right-bottom pointCxD
. Also accepts float values between 0 and 1 that represents percentage of image dimensions.format(format)
specifies the output format of the imageformat
accepts jpeg, png, gif, webp, tiff, avif
grayscale()
changes the image to grayscalehue(angle)
increases or decreases the image hueangle
the angle in degree to increase or decrease the hue rotation
max_bytes(amount)
automatically degrades the quality of the image until the image is under the specifiedamount
of bytesproportion(percentage)
scales image to the proportion percentage of the image dimensionquality(amount)
changes the overall quality of the image, does nothing for pngamount
0 to 100, the quality level in %
rgb(r,g,b)
amount of color in each of the rgb channels in %. Can range from -100 to 100rotate(angle)
rotates the given image according to the angle value passedangle
accepts 0, 90, 180, 270
round_corner(rx [, ry [, color]])
adds rounded corners to the image with the specified color as backgroundrx
,ry
amount of pixel to use as radius. ry = rx if ry is not providedcolor
the color name or hexadecimal rgb expression without the “#” character
saturation(amount)
increases or decreases the image saturationamount
-100 to 100, the amount in % to increase or decrease the image saturation
sharpen(sigma)
sharpens the imageupscale()
upscale the image iffit-in
is usedwatermark(image, x, y, alpha [, w_ratio [, h_ratio]])
adds a watermark to the image. It can be positioned inside the image with the alpha channel specified and optionally resized based on the image size by specifying the ratioimage
watermark image URI, using the same image loader configured for Imagorx
horizontal position that the watermark will be in:- Positive numbers indicate position from the left and negative numbers indicate position from the right.
- Number followed by a
p
e.g. 20p means calculating the value from the image width as percentage left
,right
,center
positioned left, right or centered respectivelyrepeat
the watermark will be repeated horizontally
y
vertical position that the watermark will be in:- Positive numbers indicate position from the top and negative numbers indicate position from the bottom.
- Number followed by a
p
e.g. 20p means calculating the value from the image height as percentage top
,bottom
,center
positioned top, bottom or centered respectivelyrepeat
the watermark will be repeated vertically
alpha
watermark image transparency, a number between 0 (fully opaque) and 100 (fully transparent).w_ratio
percentage of the width of the image the watermark should fit-inh_ratio
percentage of the height of the image the watermark should fit-in
Imagor Loader
, Storage
and Result Storage
are the building blocks for loading and saving images from various sources:
Loader
loads image. EnableLoader
where you wish to load images from, but without modifying it e.g. static directory.Storage
loads and saves image. This allows subsequent requests for the same image loads directly from the storage, instead of HTTP source.Result Storage
loads and saves the processed image. This allows subsequent request of the same parameters loads from the result storage, saving processing resources.
Imagor provides built-in adaptors that support HTTP(s), Proxy, File System, AWS S3 and Google Cloud Storage. By default, HTTP Loader
is used as fallback. You can choose to enable additional adaptors that fit your use cases.
Docker Compose example with file system, using mounted volume:
version: "3"
services:
imagor:
image: ghcr.io/cshum/imagor:latest
volumes:
- ./:/mnt/data
environment:
PORT: 8000
IMAGOR_UNSAFE: 1 # unsafe URL for testing
FILE_LOADER_BASE_DIR: /mnt/data # enable file loader by specifying base dir
FILE_STORAGE_BASE_DIR: /mnt/data # enable file storage by specifying base dir
FILE_STORAGE_MKDIR_PERMISSION: 0755 # optional
FILE_STORAGE_WRITE_PERMISSION: 0666 # optional
FILE_RESULT_STORAGE_BASE_DIR: /mnt/data/result # enable file result storage by specifying base dir
FILE_RESULT_STORAGE_MKDIR_PERMISSION: 0755 # optional
FILE_RESULT_STORAGE_WRITE_PERMISSION: 0666 # optional
ports:
- "8000:8000"
Docker Compose example with AWS S3. Also works with S3 compatible such as MinIO, DigitalOcean Space.
version: "3"
services:
imagor:
image: ghcr.io/cshum/imagor:latest
environment:
PORT: 8000
IMAGOR_SECRET: mysecret # secret key for URL signature
AWS_ACCESS_KEY_ID: ...
AWS_SECRET_ACCESS_KEY: ...
AWS_REGION: ...
S3_LOADER_BUCKET: mybucket # enable S3 loader by specifying bucket
S3_LOADER_BASE_DIR: images # optional
S3_STORAGE_BUCKET: mybucket # enable S3 storage by specifying bucket
S3_STORAGE_BASE_DIR: images # optional
S3_STORAGE_ACL: public-read # optional - see https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl
S3_RESULT_STORAGE_BUCKET: mybucket # enable S3 result storage by specifying bucket
S3_RESULT_STORAGE_BASE_DIR: images/result # optional
S3_RESULT_STORAGE_ACL: public-read # optional
ports:
- "8000:8000"
Configure custom S3 endpoint for S3 compatible such as MinIO, DigitalOcean Space:
S3_ENDPOINT: http://minio:9000
S3_FORCE_PATH_STYLE: 1
By default, S3 prepends bucket name as subdomain to the request URL:
http://mybucket.minio:9000/image.jpg
this may not be desirable for a self-hosted endpoint. You can also switch to path-style requests using S3_FORCE_PATH_STYLE=1
such that the host remains unchanged:
http://minio:9000/mybucket/image.jpg
Docker Compose example with Google Cloud Storage:
version: "3"
services:
imagor:
image: ghcr.io/cshum/imagor:latest
volumes:
- ./googlesecret:/etc/secrets/google
environment:
PORT: 8000
IMAGOR_SECRET: mysecret # secret key for URL signature
GOOGLE_APPLICATION_CREDENTIALS: /etc/secrets/google/appcredentials.json # google cloud secrets file
GCLOUD_LOADER_BUCKET: mybucket # enable loader by specifying bucket
GCLOUD_LOADER_BASE_DIR: images # optional
GCLOUD_STORAGE_BUCKET: mybucket # enable storage by specifying bucket
GCLOUD_STORAGE_BASE_DIR: images # optional
GCLOUD_STORAGE_ACL: publicRead # optional - see https://cloud.google.com/storage/docs/json_api/v1/objects/insert
GCLOUD_RESULT_STORAGE_BUCKET: mybucket # enable result storage by specifying bucket
GCLOUD_RESULT_STORAGE_BASE_DIR: images/result # optional
GCLOUD_RESULT_STORAGE_ACL: publicRead # optional
ports:
- "8000:8000"
In production environment, it is highly recommended turning off IMAGOR_UNSAFE
and setting up URL signature using IMAGOR_SECRET
, to prevent DDoS attacks that abuse multiple image operations.
The URL signature hash is based on SHA digest, created by taking the URL path (excluding /unsafe/
) with secret. The hash is then Base64 URL encoded.
An example in Node.js:
var hmacSHA1 = require("crypto-js/hmac-sha1")
var Base64 = require("crypto-js/enc-base64")
function sign(path, secret) {
var hash = hmacSHA1(path, secret)
hash = Base64.stringify(hash).replace(/\+/g, '-').replace(/\//g, '_')
return hash + '/' + path
}
console.log(sign('500x500/top/raw.githubusercontent.com/cshum/imagor/master/testdata/gopher.png', 'mysecret'))
// cST4Ko5_FqwT3BDn-Wf4gO3RFSk=/500x500/top/raw.githubusercontent.com/cshum/imagor/master/testdata/gopher.png
Imagor uses SHA1 HMAC signer by default, the same one used by Thumbor. However, SHA1 is not considered cryptographically secure. If that is a concern it is possible to configure different signing method and truncate length. Imagor supports sha1
, sha256
, sha512
signer type:
IMAGOR_SIGNER_TYPE=sha256
IMAGOR_SIGNER_TRUNCATE=32
The URL signature generator then becomes:
var hmacSHA256 = require("crypto-js/hmac-sha256")
var Base64 = require("crypto-js/enc-base64")
function sign(path, secret) {
var hash = hmacSHA256(path, secret)
hash = Base64.stringify(hash).slice(0, 32).replace(/\+/g, '-').replace(/\//g, '_')
return hash + '/' + path
}
Whitelist specific hosts to restrict loading images only from the allowed sources using HTTP_LOADER_ALLOWED_SOURCES
. Accept csv wth glob pattern e.g.:
HTTP_LOADER_ALLOWED_SOURCES=*.foobar.com,my.foobar.com,mybucket.s3.amazonaws.com
Imagor supports command-line arguments and environment variables for the arguments equivalent in capitalized snake case, see available options imagor -h
.
For instances -imagor-secret
would become IMAGOR_SECRET
:
# both are equivalent
imagor -debug -imagor-secret 1234
DEBUG=1 IMAGOR_SECRET=1234 imagor
Configuration can also be specified in a .env
environment variable file and referenced with the -config
flag:
imagor -config path/to/config.env
config.env:
PORT=8000
IMAGOR_SECRET=mysecret
DEBUG=1
imagor -h
Usage of imagor:
-debug
Debug mode
-port int
Sever port (default 8000)
-version
Imagor version
-config string
Retrieve configuration from the given file (default ".env")
-imagor-secret string
Secret key for signing Imagor URL
-imagor-unsafe
Unsafe Imagor that does not require URL signature. Prone to URL tampering
-imagor-auto-webp
Output WebP format automatically if browser supports
-imagor-auto-avif
Output AVIF format automatically if browser supports (experimental)
-imagor-base-params string
Imagor endpoint base params that applies to all resulting images e.g. fitlers:watermark(example.jpg)
-imagor-signer-type string
Imagor URL signature hasher type sha1, sha256, sha512 (default "sha1")
-imagor-signer-truncate int
Imagor URL signature truncate at length
-imagor-cache-header-ttl duration
Imagor HTTP cache header ttl for successful image response (default 168h0m0s)
-imagor-cache-header-swr duration
Imagor HTTP Cache-Control header stale-while-revalidate for successful image response (default 24h0m0s)
-imagor-cache-header-no-cache
Imagor HTTP Cache-Control header no-cache for successful image response
-imagor-request-timeout duration
Timeout for performing Imagor request (default 30s)
-imagor-load-timeout duration
Timeout for Imagor Loader request, should be smaller than imagor-request-timeout (default 20s)
-imagor-save-timeout duration
Timeout for saving image to Imagor Storage (default 20s)
-imagor-process-timeout duration
Timeout for image processing (default 20s)
-imagor-process-concurrency int
Imagor semaphore size for process concurrency control. Set -1 for no limit (default -1)
-imagor-base-path-redirect string
URL to redirect for Imagor / base path e.g. https://www.google.com
-imagor-modified-time-check
Check modified time of result image against the source image. This eliminates stale result but require more lookups
-imagor-disable-params-endpoint
Imagor disable /params endpoint
-imagor-disable-error-body
Imagor disable response body on error
-server-address string
Server address
-server-cors
Enable CORS
-server-strip-query-string
Enable strip query string redirection
-server-path-prefix string
Server path prefix
-server-access-log
Enable server access log
-http-loader-allowed-sources string
HTTP Loader allowed hosts whitelist to load images from if set. Accept csv wth glob pattern e.g. *.google.com,*.github.com.
-http-loader-forward-headers string
Forward request header to HTTP Loader request by csv e.g. User-Agent,Accept
-http-loader-forward-client-headers
Forward browser client request headers to HTTP Loader request
-http-loader-insecure-skip-verify-transport
HTTP Loader to use HTTP transport with InsecureSkipVerify true
-http-loader-max-allowed-size int
HTTP Loader maximum allowed size in bytes for loading images if set
-http-loader-proxy-urls string
HTTP Loader Proxy URLs. Enable HTTP Loader proxy only if this value present. Accept csv of proxy urls e.g. http://user:pass@host:port,http://user:pass@host:port
-http-loader-proxy-allowed-sources string
HTTP Loader Proxy allowed hosts that enable proxy transport, if proxy URLs are set. Accept csv wth glob pattern e.g. *.google.com,*.github.com.
-http-loader-default-scheme string
HTTP Loader default scheme if not specified by image path. Set "nil" to disable default scheme. (default "https")
-http-loader-accept string
HTTP Loader set request Accept header and validate response Content-Type header (default "*/*")
-http-loader-disable
Disable HTTP Loader
-file-safe-chars string
File safe characters to be excluded from image key escape
-file-loader-base-dir string
Base directory for File Loader. Enable File Loader only if this value present
-file-loader-path-prefix string
Base path prefix for File Loader
-file-result-storage-base-dir string
Base directory for File Result Storage. Enable File Result Storage only if this value present
-file-result-storage-mkdir-permission string
File Result Storage mkdir permission (default "0755")
-file-result-storage-path-prefix string
Base path prefix for File Result Storage
-file-result-storage-write-permission string
File Storage write permission (default "0666")
-file-result-storage-expiration duration
File Result Storage expiration duration e.g. 24h. Default no expiration
-file-storage-base-dir string
Base directory for File Storage. Enable File Storage only if this value present
-file-storage-path-prefix string
Base path prefix for File Storage
-file-storage-mkdir-permission string
File Storage mkdir permission (default "0755")
-file-storage-write-permission string
File Storage write permission (default "0666")
-file-storage-expiration duration
File Storage expiration duration e.g. 24h. Default no expiration
-aws-access-key-id string
AWS Access Key ID. Required if using S3 Loader or S3 Storage
-aws-region string
AWS Region. Required if using S3 Loader or S3 Storage
-aws-secret-access-key string
AWS Secret Access Key. Required if using S3 Loader or S3 Storage
-s3-endpoint string
Optional S3 Endpoint to override default
-s3-safe-chars string
S3 safe characters to be excluded from image key escape
-s3-force-path-style
S3 force the request to use path-style addressing s3.amazonaws.com/bucket/key, instead of bucket.s3.amazonaws.com/key
-s3-loader-bucket string
S3 Bucket for S3 Loader. Enable S3 Loader only if this value present
-s3-loader-base-dir string
Base directory for S3 Loader
-s3-loader-path-prefix string
Base path prefix for S3 Loader
-s3-result-storage-bucket string
S3 Bucket for S3 Result Storage. Enable S3 Result Storage only if this value present
-s3-result-storage-base-dir string
Base directory for S3 Result Storage
-s3-result-storage-path-prefix string
Base path prefix for S3 Result Storage
-s3-result-storage-acl string
Upload ACL for S3 Result Storage (default "public-read")
-s3-result-storage-expiration duration
S3 Result Storage expiration duration e.g. 24h. Default no expiration
-s3-storage-bucket string
S3 Bucket for S3 Storage. Enable S3 Storage only if this value present
-s3-storage-base-dir string
Base directory for S3 Storage
-s3-storage-path-prefix string
Base path prefix for S3 Storage
-s3-storage-acl string
Upload ACL for S3 Storage (default "public-read")
-s3-storage-expiration duration
S3 Storage expiration duration e.g. 24h. Default no expiration
-gcloud-safe-chars string
Google Cloud safe characters to be excluded from image key escape
-gcloud-loader-base-dir string
Base directory for Google Cloud Loader
-gcloud-loader-bucket string
Bucket name for Google Cloud Storage Loader. Enable Google Cloud Loader only if this value present
-gcloud-loader-path-prefix string
Base path prefix for Google Cloud Loader
-gcloud-result-storage-acl string
Upload ACL for Google Cloud Result Storage
-gcloud-result-storage-base-dir string
Base directory for Google Cloud Result Storage
-gcloud-result-storage-bucket string
Bucket name for Google Cloud Result Storage. Enable Google Cloud Result Storage only if this value present
-gcloud-result-storage-expiration duration
Google Cloud Result Storage expiration duration e.g. 24h. Default no expiration
-gcloud-result-storage-path-prefix string
Base path prefix for Google Cloud Result Storage
-gcloud-storage-acl string
Upload ACL for Google Cloud Storage
-gcloud-storage-base-dir string
Base directory for Google Cloud
-gcloud-storage-bucket string
Bucket name for Google Cloud Storage. Enable Google Cloud Storage only if this value present
-gcloud-storage-expiration duration
Google Cloud Storage expiration duration e.g. 24h. Default no expiration
-gcloud-storage-path-prefix string
Base path prefix for Google Cloud Storage
-vips-max-animation-frames int
VIPS maximum number of animation frames to be loaded. Set 1 to disable animation, -1 for unlimited
-vips-disable-blur
VIPS disable blur operations for vips processor
-vips-disable-filters string
VIPS disable filters by csv e.g. blur,watermark,rgb
-vips-max-filter-ops int
VIPS maximum number of filter operations allowed. Set -1 for unlimited (default -1)
-vips-max-height int
VIPS max image height
-vips-max-width int
VIPS max image width
-vips-concurrency int
VIPS concurrency. Set -1 to be the number of CPU cores (default 1)
-vips-max-cache-files int
VIPS max cache files
-vips-max-cache-mem int
VIPS max cache mem
-vips-max-cache-size int
VIPS max cache size
-vips-mozjpeg
VIPS enable maximum compression with MozJPEG. Requires mozjpeg to be installed