2018-02-06 11:09:10 -05:00
# imageproxy
[![GoDoc ](https://godoc.org/willnorris.com/go/imageproxy?status.svg )](https://godoc.org/willnorris.com/go/imageproxy)
[![Build Status ](https://travis-ci.org/willnorris/imageproxy.svg?branch=master )](https://travis-ci.org/willnorris/imageproxy)
[![Apache 2.0 License ](https://img.shields.io/badge/license-Apache%202.0-blue.svg?style=flat )](LICENSE)
2013-11-27 11:42:33 -05:00
2015-05-21 11:07:38 -05:00
imageproxy is a caching image proxy server written in go. It features:
- basic image adjustments like resizing, cropping, and rotation
- access control using host whitelists or request signing (HMAC-SHA256)
2017-09-26 10:49:08 -05:00
- support for jpeg, png, webp (decode only), tiff, and gif image formats
(including animated gifs)
- caching in-memory, on disk, or with Amazon S3, Google Cloud Storage, Azure
Storage, or Redis
2015-05-21 11:07:38 -05:00
- easy deployment, since it's pure go
2015-05-17 23:43:55 -05:00
Personally, I use it primarily to dynamically resize images hosted on my own
2015-05-21 11:07:38 -05:00
site (read more in [this post][]). But you can also enable request signing and
use it as an SSL proxy for remote images, similar to [atmos/camo][] but with
additional image adjustment options.
2013-11-27 11:42:33 -05:00
2015-05-17 23:43:55 -05:00
[this post]: https://willnorris.com/2014/01/a-self-hosted-alternative-to-jetpacks-photon-service
[atmos/camo]: https://github.com/atmos/camo
2013-11-27 11:42:33 -05:00
## URL Structure ##
2013-12-26 14:14:28 -05:00
imageproxy URLs are of the form `http://localhost/{options}/{remote_url}` .
2013-11-27 11:42:33 -05:00
### Options ###
2017-08-31 02:27:35 -05:00
Options are available for cropping, resizing, rotation, flipping, and digital
signatures among a few others. Options for are specified as a comma delimited
list of parameters, which can be supplied in any order. Duplicate parameters
overwrite previous values.
2013-12-26 16:35:23 -05:00
2017-06-01 09:29:09 -05:00
See the full list of available options at
< https: / / godoc . org / willnorris . com / go / imageproxy # ParseOptions > .
2015-08-14 08:57:25 -05:00
2013-11-27 11:42:33 -05:00
### Remote URL ###
The URL of the original image to load is specified as the remainder of the
path, without any encoding. For example,
`http://localhost/200/https://willnorris.com/logo.jpg` .
In order to [optimize caching][], it is recommended that URLs not contain query
strings.
[optimize caching]: http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/
2013-12-06 20:01:13 -05:00
### Examples ###
The following live examples demonstrate setting different options on [this
2013-12-06 20:40:35 -05:00
source image][small-things], which measures 1024 by 678 pixels.
2013-12-06 20:01:13 -05:00
2014-07-06 16:43:01 -05:00
[small-things]: https://willnorris.com/2013/12/small-things.jpg
2013-12-06 20:01:13 -05:00
Options | Meaning | Image
--------|------------------------------------------|------
2014-11-21 12:00:42 -05:00
200x | 200px wide, proportional height | < a href = "https://willnorris.com/api/imageproxy/200x/https://willnorris.com/2013/12/small-things.jpg" > < img src = "https://willnorris.com/api/imageproxy/200x/https://willnorris.com/2013/12/small-things.jpg" alt = "200x" > < / a >
2017-08-31 02:24:59 -05:00
x0.15 | 15% original height, proportional width | < a href = "https://willnorris.com/api/imageproxy/x0.15/https://willnorris.com/2013/12/small-things.jpg" > < img src = "https://willnorris.com/api/imageproxy/x0.15/https://willnorris.com/2013/12/small-things.jpg" alt = "x0.15" > < / a >
2014-11-21 12:00:42 -05:00
100x150 | 100 by 150 pixels, cropping as needed | < a href = "https://willnorris.com/api/imageproxy/100x150/https://willnorris.com/2013/12/small-things.jpg" > < img src = "https://willnorris.com/api/imageproxy/100x150/https://willnorris.com/2013/12/small-things.jpg" alt = "100x150" > < / a >
100 | 100px square, cropping as needed | < a href = "https://willnorris.com/api/imageproxy/100/https://willnorris.com/2013/12/small-things.jpg" > < img src = "https://willnorris.com/api/imageproxy/100/https://willnorris.com/2013/12/small-things.jpg" alt = "100" > < / a >
150,fit | scale to fit 150px square, no cropping | < a href = "https://willnorris.com/api/imageproxy/150,fit/https://willnorris.com/2013/12/small-things.jpg" > < img src = "https://willnorris.com/api/imageproxy/150,fit/https://willnorris.com/2013/12/small-things.jpg" alt = "150,fit" > < / a >
100,r90 | 100px square, rotated 90 degrees | < a href = "https://willnorris.com/api/imageproxy/100,r90/https://willnorris.com/2013/12/small-things.jpg" > < img src = "https://willnorris.com/api/imageproxy/100,r90/https://willnorris.com/2013/12/small-things.jpg" alt = "100,r90" > < / a >
100,fv,fh | 100px square, flipped horizontal and vertical | < a href = "https://willnorris.com/api/imageproxy/100,fv,fh/https://willnorris.com/2013/12/small-things.jpg" > < img src = "https://willnorris.com/api/imageproxy/100,fv,fh/https://willnorris.com/2013/12/small-things.jpg" alt = "100,fv,fh" > < / a >
2015-01-12 12:32:00 -05:00
200x,q60 | 200px wide, proportional height, 60% quality | < a href = "https://willnorris.com/api/imageproxy/200x,q60/https://willnorris.com/2013/12/small-things.jpg" > < img src = "https://willnorris.com/api/imageproxy/200x,q60/https://willnorris.com/2013/12/small-things.jpg" alt = "200x,q60" > < / a >
2017-08-31 02:24:59 -05:00
200x,png | 200px wide, converted to PNG format | < a href = "https://willnorris.com/api/imageproxy/200x,png/https://willnorris.com/2013/12/small-things.jpg" > < img src = "https://willnorris.com/api/imageproxy/200x,png/https://willnorris.com/2013/12/small-things.jpg" alt = "200x,png" > < / a >
2017-08-31 02:27:35 -05:00
cx175,cw400,ch300,100x | crop to 400x300px starting at (175,0), scale to 100px wide | < a href = "https://willnorris.com/api/imageproxy/cx175,cw400,ch300,100x/https://willnorris.com/2013/12/small-things.jpg" > < img src = "https://willnorris.com/api/imageproxy/cx175,cw400,ch300,100x/https://willnorris.com/2013/12/small-things.jpg" alt = "cx175,cw400,ch300,100x" > < / a >
2013-12-06 20:01:13 -05:00
2015-05-21 11:07:38 -05:00
Transformation also works on animated gifs. Here is [this source
image][material-animation] resized to 200px square and rotated 270 degrees:
[material-animation]: https://willnorris.com/2015/05/material-animations.gif
< a href = "https://willnorris.com/api/imageproxy/200,r270/https://willnorris.com/2015/05/material-animations.gif" > < img src = "https://willnorris.com/api/imageproxy/200,r270/https://willnorris.com/2015/05/material-animations.gif" alt = "200,r270" > < / a >
2017-09-26 19:54:15 -05:00
The smart crop feature can best be seen by comparing the following images, with and without smart crop.
< a href = "https://willnorris.com/api/imageproxy/150x300,sc/https://judahnorris.com/images/judah-sheets.jpg" > < img src = "https://willnorris.com/api/imageproxy/150x300,sc/https://judahnorris.com/images/judah-sheets.jpg" alt = "200x400,sc" > < / a >
2017-09-26 22:47:03 -05:00
< a href = "https://willnorris.com/api/imageproxy/150x300/https://judahnorris.com/images/judah-sheets.jpg" > < img src = "https://willnorris.com/api/imageproxy/150x300/https://judahnorris.com/images/judah-sheets.jpg" alt = "200x400" > < / a >
2013-11-27 11:42:33 -05:00
2014-11-21 12:42:42 -05:00
## Getting Started ##
Install the package using:
2015-02-15 01:39:14 -05:00
go get willnorris.com/go/imageproxy/cmd/imageproxy
2014-11-21 12:42:42 -05:00
2015-12-07 22:49:28 -05:00
Once installed, ensure `$GOPATH/bin` is in your `$PATH` , then run the proxy
using:
2014-11-21 12:42:42 -05:00
2015-02-15 01:39:14 -05:00
imageproxy
2014-11-21 12:42:42 -05:00
This will start the proxy on port 8080, without any caching and with no host
whitelist (meaning any remote URL can be proxied). Test this by navigating to
< http: / / localhost:8080 / 500 / https: / / octodex . github . com / images / codercat . jpg > and
you should see a 500px square coder octocat.
2015-12-04 18:52:40 -05:00
### Cache ###
2014-11-21 12:42:42 -05:00
2015-12-04 18:52:40 -05:00
By default, the imageproxy command does not cache responses, but caching can be
enabled using the `-cache` flag. It supports the following values:
2014-11-21 12:42:42 -05:00
2017-09-17 06:00:49 -05:00
- `memory` - uses an in-memory LRU cache. By default, this is limited to
100mb. To customize the size of the cache or the max age for cached items,
use the format `memory:size:age` where size is measured in mb and age is a
duration. For example, `memory:200:4h` will create a 200mb cache that will
cache items no longer than 4 hours.
2015-12-04 18:52:40 -05:00
- directory on local disk (e.g. `/tmp/imageproxy` ) - will cache images
2015-12-07 23:56:56 -05:00
on disk
2017-09-11 23:11:41 -05:00
- s3 URL (e.g. `s3://region/bucket-name/optional-path-prefix` ) - will cache
2015-06-28 23:13:57 -05:00
images on Amazon S3. This requires either an IAM role and instance profile
with access to your your bucket or `AWS_ACCESS_KEY_ID` and `AWS_SECRET_KEY`
2017-09-11 23:11:41 -05:00
environmental variables be set. (Additional methods of loading credentials
are documented in the [aws-sdk-go session
package](https://docs.aws.amazon.com/sdk-for-go/api/aws/session/)).
2016-12-28 12:36:30 -05:00
- gcs URL (e.g. `gcs://bucket-name/optional-path-prefix` ) - will cache images
2018-02-02 05:05:56 -05:00
on Google Cloud Storage. Authentication is documented in Google's
[Application Default Credentials
docs](https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application).
2016-12-28 12:36:30 -05:00
- azure URL (e.g. `azure://container-name/` ) - will cache images on
Azure Storage. This requires `AZURESTORAGE_ACCOUNT_NAME` and
2018-02-02 05:05:56 -05:00
`AZURESTORAGE_ACCESS_KEY` environment variables to bet set.
2017-09-09 02:14:44 -05:00
- redis URL (e.g. `redis://hostname/` ) - will cache images on
the specified redis host. The full URL syntax is defined by the [redis URI
registration](https://www.iana.org/assignments/uri-schemes/prov/redis).
Rather than specify password in the URI, use the `REDIS_PASSWORD`
environment variable.
2015-12-04 18:52:40 -05:00
2015-12-07 23:56:56 -05:00
For example, to cache files on disk in the `/tmp/imageproxy` directory:
2015-12-04 18:52:40 -05:00
2015-12-07 23:56:56 -05:00
imageproxy -cache /tmp/imageproxy
2014-11-21 12:42:42 -05:00
2015-05-12 00:46:00 -05:00
Reload the [codercat URL][], and then inspect the contents of
2015-12-07 23:56:56 -05:00
`/tmp/imageproxy` . Within the subdirectories, there should be two files, one
for the original full-size codercat image, and one for the resized 500px
version.
2015-05-12 00:46:00 -05:00
[codercat URL]: http://localhost:8080/500/https://octodex.github.com/images/codercat.jpg
2014-11-21 12:42:42 -05:00
2017-09-17 04:55:41 -05:00
If the `-cache` flag is specified multiple times, multiple caches will be
created in a [tiered fashion][]. Typically this is used to put a smaller and
faster in-memory cache in front of a larger but slower on-disk cache. For
example, the following will first check an in-memory cache for an image,
followed by a gcs bucket:
imageproxy -cache memory -cache gcs://my-bucket/
[tiered fashion]: https://godoc.org/github.com/die-net/lrucache/twotier
2015-06-14 03:26:40 -05:00
### Referrer Whitelist ###
2015-12-07 22:49:28 -05:00
You can limit images to only be accessible for certain hosts in the HTTP
referrer header, which can help prevent others from hotlinking to images. It can
2015-12-07 23:56:56 -05:00
be enabled by running:
2015-06-14 03:26:40 -05:00
imageproxy -referrers example.com
Reload the [codercat URL][], and you should now get an error message. You can
specify multiple hosts as a comma separated list, or prefix a host value with
`*.` to allow all sub-domains as well.
2014-11-21 12:42:42 -05:00
### Host whitelist ###
You can limit the remote hosts that the proxy will fetch images from using the
`whitelist` flag. This is useful, for example, for locking the proxy down to
your own hosts to prevent others from abusing it. Of course if you want to
support fetching from any host, leave off the whitelist flag. Try it out by
running:
2015-02-15 02:28:45 -05:00
imageproxy -whitelist example.com
2014-11-21 12:42:42 -05:00
2015-05-12 00:46:00 -05:00
Reload the [codercat URL][], and you should now get an error message. You can
specify multiple hosts as a comma separated list, or prefix a host value with
`*.` to allow all sub-domains as well.
### Signed Requests ###
Instead of a host whitelist, you can require that requests be signed. This is
useful in preventing abuse when you don't have just a static list of hosts you
want to allow. Signatures are generated using HMAC-SHA256 against the remote
URL, and url-safe base64 encoding the result:
base64urlencode(hmac.New(sha256, < key > ).digest(< remote_url > ))
The HMAC key is specified using the `signatureKey` flag. If this flag
begins with an "@", the remainder of the value is interpreted as a file on disk
which contains the HMAC key.
Try it out by running:
imageproxy -signatureKey "secret key"
Reload the [codercat URL][], and you should see an error message. Now load a
[signed codercat URL][] and verify that it loads properly.
[signed codercat URL]: http://localhost:8080/500,sXyMwWKIC5JPCtlYOQ2f4yMBTqpjtUsfI67Sp7huXIYY=/https://octodex.github.com/images/codercat.jpg
Some simple code samples for generating signatures in various languages can be
2015-08-14 11:56:47 -05:00
found in [URL Signing ](https://github.com/willnorris/imageproxy/wiki/URL-signing ).
2015-05-12 00:46:00 -05:00
If both a whiltelist and signatureKey are specified, requests can match either.
In other words, requests that match one of the whitelisted hosts don't
necessarily need to be signed, though they can be.
2015-02-20 00:34:22 -05:00
### Default Base URL ###
Typically, remote images to be proxied are specified as absolute URLs.
However, if you commonly proxy images from a single source, you can provide a
2015-12-07 22:49:28 -05:00
base URL and then specify remote images relative to that base. Try it out by
running:
2015-02-20 00:34:22 -05:00
imageproxy -baseURL https://octodex.github.com/
Then load the codercat image, specified as a URL relative to that base:
< http: / / localhost:8080 / 500 / images / codercat . jpg > . Note that this is not an
effective method to mask the true source of the images being proxied; it is
trivial to discover the base URL being used. Even when a base URL is
specified, you can always provide the absolute URL of the image to be proxied.
2015-08-12 13:45:19 -05:00
### Scaling beyond original size ###
2015-12-07 22:49:28 -05:00
By default, the imageproxy won't scale images beyond their original size.
However, you can use the `scaleUp` command-line flag to allow this to happen:
2015-08-12 13:45:19 -05:00
imageproxy -scaleUp true
2014-11-21 12:42:42 -05:00
2017-08-31 19:03:17 -05:00
### WebP and TIFF support ###
2017-06-01 10:10:34 -05:00
Imageproxy can proxy remote webp images, but they will be served in either jpeg
2017-08-31 02:24:59 -05:00
or png format (this is because the golang webp library only supports webp
decoding) if any transformation is requested. If no format is specified,
imageproxy will use jpeg by default. If no transformation is requested (for
example, if you are just using imageproxy as an SSL proxy) then the original
webp image will be served as-is without any format conversion.
2017-08-31 19:03:17 -05:00
Because so few browsers support tiff images, they will be converted to jpeg by
default if any transformation is requested. To force encoding as tiff, pass the
"tiff" option. Like webp, tiff images will be served as-is without any format
conversion if no transformation is requested.
2017-08-31 02:24:59 -05:00
Run `imageproxy -help` for a complete list of flags the command accepts. If
you want to use a different caching implementation, it's probably easiest to
just make a copy of `cmd/imageproxy/main.go` and customize it to fit your
needs... it's a very simple command.
2017-06-01 10:10:34 -05:00
2014-11-21 12:42:42 -05:00
## Deploying ##
2017-09-09 03:14:20 -05:00
In most cases, you can follow the normal procedure for building a deploying any
go application. For example, I build it directly on my production debian server
using:
- `go build willnorris.com/go/imageproxy/cmd/imageproxy`
- copy resulting binary to `/usr/local/bin`
- copy [`etc/imageproxy.service` ](etc/imageproxy.service ) to
`/lib/systemd/system` and enable using `systemctl` .
Instructions have been contributed below for running on other platforms, but I
don't have much experience with them personally.
2014-11-21 12:42:42 -05:00
2017-06-01 10:12:15 -05:00
### Heroku ###
2015-08-13 11:47:33 -05:00
2015-12-07 22:49:28 -05:00
It's easy to vendorize the dependencies with `Godep` and deploy to Heroku. Take
a look at [this GitHub repo ](https://github.com/oreillymedia/prototype-imageproxy )
2015-08-13 11:47:33 -05:00
2017-06-01 10:12:15 -05:00
### Docker ###
2015-05-29 04:49:24 -05:00
A docker image is available at [`willnorris/imageproxy` ](https://registry.hub.docker.com/u/willnorris/imageproxy/dockerfile/ ).
You can run it by
```
docker run -p 8080:8080 willnorris/imageproxy -addr 0.0.0.0:8080
```
2014-11-21 12:42:42 -05:00
2016-05-01 00:55:03 -05:00
Or in your Dockerfile:
```
ENTRYPOINT ["/go/bin/imageproxy", "-addr 0.0.0.0:8080"]
```
2017-06-01 10:12:15 -05:00
### nginx ###
2016-05-01 00:55:03 -05:00
You can use follow config to prevent URL overwritting:
```
location ~ ^/api/imageproxy/ {
# pattern match to capture the original URL to prevent URL
# canonicalization, which would strip double slashes
if ($request_uri ~ "/api/imageproxy/(.+)") {
set $path $1;
rewrite .* /$path break;
}
proxy_pass http://localhost:8080;
}
```
2013-11-27 11:42:33 -05:00
## License ##
2015-05-17 23:43:55 -05:00
imageproxy is copyright Google, but is not an official Google product. It is
available under the [Apache 2.0 License ](./LICENSE ).