Personally, I use it primarily to dynamically resize images hosted on my ownsite (read more in this post). But you can also enable request signing anduse it as an SSL proxy for remote images, similar to atmos/camo but withadditional image adjustment options.

URL Structure

imageproxy URLs are of the form http://localhost/{options}/{remote_url}.

Options

Options are specified as a comma delimited list of parameters, which can besupplied in any order. Duplicate parameters overwrite previous values.

Size

The size option takes the general form {width}x{height}, where width andheight are numbers. Integer values greater than 1 are interpreted as exactpixel values. Floats between 0 and 1 are interpreted as percentages of theoriginal image size. If either value is omitted or set to 0, it will beautomatically set to preserve the aspect ratio based on the other dimension.If a single number is provided (with no "x" separator), it will be used forboth height and width.

Crop Mode

Depending on the options specified, an image may be cropped to fit therequested size. In all cases, the original aspect ratio of the image will bepreserved; imageproxy will never stretch the original image.

When no explicit crop mode is specified, the following rules are followed:

If both width and height values are specified, the image will be scaled tofill the space, cropping if necessary to fit the exact dimension.

If only one of the width or height values is specified, the image will beresized to fit the specified dimension, scaling the other dimension asneeded to maintain the aspect ratio.

If the fit option is specified together with a width and height value, theimage will be resized to fit within a containing box of the specified size. Asalways, the original aspect ratio will be preserved. Specifying the fitoption with only one of either width or height does the same thing as if fithad not been specified.

Rotate

The r{degrees} option will rotate the image the specified number of degrees,counter-clockwise. Valid degrees values are 90, 180, and 270. Imagesare rotated after being resized.

Flip

The fv option will flip the image vertically. The fh option will flip theimage horizontally. Images are flipped after being resized and rotated.

Quality

The q{percentage} option can be used to specify the output quality (JPEGonly). If not specified, the default value of 95 is used.

Signature

The s{signature} option specifies an optional base64 encoded HMAC used tosign the remote URL in the request. The HMAC key used to verify signatures isprovided to the imageproxy server on startup.

s3 URL (e.g. s3://s3-us-west-2.amazonaws.com/my-bucket) - will cacheimages on Amazon S3. This requires either an IAM role and instance profilewith access to your your bucket or AWS_ACCESS_KEY_ID and AWS_SECRET_KEYenvironmental parameters set.

For example, to cache files on disk in the /tmp/imageproxy directory:

imageproxy -cache /tmp/imageproxy

Reload the codercat URL, and then inspect the contents of/tmp/imageproxy. Within the subdirectories, there should be two files, onefor the original full-size codercat image, and one for the resized 500pxversion.

Referrer Whitelist

You can limit images to only be accessible for certain hosts in the HTTPreferrer header, which can help prevent others from hotlinking to images. It canbe enabled by running:

imageproxy -referrers example.com

Reload the codercat URL, and you should now get an error message. You canspecify multiple hosts as a comma separated list, or prefix a host value with*. to allow all sub-domains as well.

Host whitelist

You can limit the remote hosts that the proxy will fetch images from using thewhitelist flag. This is useful, for example, for locking the proxy down toyour own hosts to prevent others from abusing it. Of course if you want tosupport fetching from any host, leave off the whitelist flag. Try it out byrunning:

imageproxy -whitelist example.com

Reload the codercat URL, and you should now get an error message. You canspecify 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 isuseful in preventing abuse when you don't have just a static list of hosts youwant to allow. Signatures are generated using HMAC-SHA256 against the remoteURL, 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 flagbegins with an "@", the remainder of the value is interpreted as a file on diskwhich contains the HMAC key.

Some simple code samples for generating signatures in various languages can befound in URL Signing.

If both a whiltelist and signatureKey are specified, requests can match either.In other words, requests that match one of the whitelisted hosts don'tnecessarily need to be signed, though they can be.

Run imageproxy -help for a complete list of flags the command accepts. Ifyou want to use a different caching implementation, it's probably easiest tojust make a copy of cmd/imageproxy/main.go and customize it to fit yourneeds... it's a very simple command.

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 abase URL and then specify remote images relative to that base. Try it out byrunning:

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 aneffective method to mask the true source of the images being proxied; it istrivial to discover the base URL being used. Even when a base URL isspecified, you can always provide the absolute URL of the image to be proxied.

Scaling beyond original size

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:

imageproxy -scaleUp true

Deploying

You can build and deploy imageproxy using any standard go toolchain, but here'show I do it.

I use goxc to build and deploy to an Ubuntuserver. I have a $GOPATH/willnorris.com/go/imageproxy/.goxc.local.json filewhich limits builds to 64-bit linux:

{
"ConfigVersion": "0.9",
"BuildConstraints": "linux,amd64"
}

I then run goxc which compiles the static binary and creates a deb package atbuild/0.2.1/imageproxy_0.2.1_amd64.deb (or whatever the current version is).I copy this file to my server and install it using sudo dpkg -i
imageproxy_0.2.1_amd64.deb, which is installed to /usr/bin/imageproxy.

Ubuntu uses upstart to manage services, so I copyetc/imageproxy.conf to /etc/init/imageproxy.conf onmy server and start it using sudo service imageproxy start. You willcertainly want to modify that upstart script to suit your desiredconfiguration.

Deploying to Heroku

It's easy to vendorize the dependencies with Godep and deploy to Heroku. Takea look at this GitHub repo