imageproxy

package module
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2015 License: Apache-2.0 Imports: 17 Imported by: 0

README

imageproxy

imageproxy is a caching image proxy server written in golang. It supports dynamic image resizing and URL whitelisting.

This project was inspired by, and is designed to be an alternative to, WordPress's photon service. Photon is a great free service, but is limited to sites hosted on WordPress.com, or that use the Jetpack plugin. If you don't want to use Jetpack, then you're asked to use a different service. If you're looking for an alternative hosted service, I'd recommend resize.ly, embed.ly, or cloudinary. I decided to try building my own for fun.

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 be supplied in any order. Duplicate parameters overwrite previous values.

The format is a superset of resize.ly's options.

Size

The size option takes the general form {width}x{height}, where width and height are numbers. Integer values greater than 1 are interpreted as exact pixel values. Floats between 0 and 1 are interpreted as percentages of the original image size. If either value is omitted or set to 0, it will be automatically 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 for both height and width.

Crop Mode

Depending on the options specified, an image may be cropped to fit the requested size. In all cases, the original aspect ratio of the image will be preserved; 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 to fill the space, cropping if necessary to fit the exact dimension.

  • If only one of the width or height values is specified, the image will be resized to fit the specified dimension, scaling the other dimension as needed to maintain the aspect ratio.

If the fit option is specified together with a width and height value, the image will be resized to fit within a containing box of the specified size. As always, the original aspect ratio will be preserved. Specifying the fit option with only one of either width or height does the same thing as if fit had 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. Images are rotated after being resized.

Flip

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

Quality

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

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.

Examples

The following live examples demonstrate setting different options on this source image, which measures 1024 by 678 pixels.

Options Meaning Image
200x 200px wide, proportional height 200x
0.15x 15% original width, proportional height 0.15x
x100 100px tall, proportional width x100
100x150 100 by 150 pixels, cropping as needed 100x150
100 100px square, cropping as needed 100
150,fit scale to fit 150px square, no cropping 150,fit
100,r90 100px square, rotated 90 degrees 100,r90
100,fv,fh 100px square, flipped horizontal and vertical 100,fv,fh
200x,q60 200px wide, proportional height, 60% quality 200x,q60

Getting Started

Install the package using:

go get willnorris.com/go/imageproxy/cmd/imageproxy

Once installed, ensure $GOPATH/bin is in your $PATH, then run the proxy using:

imageproxy

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.

Disk cache

By default, the imageproxy command uses an in-memory cache that will grow unbounded. To cache images on disk instead, include the cacheDir flag:

imageproxy -cacheDir /tmp/imageproxy

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

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:

imageproxy -whitelist 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.

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.

Deploying

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

I use goxc to build and deploy to an Ubuntu server. I have a $GOPATH/willnorris.com/go/imageproxy/.goxc.local.json file which 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 at build/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 copy etc/imageproxy.conf to /etc/init/imageproxy.conf on my server and start it using sudo service imageproxy start. You will certainly want to modify that upstart script to suit your desired configuration.

License

This application is distributed under the Apache 2.0 license found in the LICENSE file.

Documentation

Overview

Package imageproxy provides an image proxy server. For typical use of creating and using a Proxy, see cmd/imageproxy/main.go.

Index

Constants

This section is empty.

Variables

View Source
var NopCache = new(nopCache)

NopCache provides a no-op cache implementation that doesn't actually cache anything.

Functions

func Transform

func Transform(img []byte, opt Options) ([]byte, error)

Transform the provided image. img should contain the raw bytes of an encoded image in one of the supported formats (gif, jpeg, or png). The bytes of a similarly encoded image is returned.

Types

type Cache

type Cache interface {
	// Get retrieves the cached data for the provided key.
	Get(key string) (data []byte, ok bool)

	// Set caches the provided data.
	Set(key string, data []byte)

	// Delete deletes the cached data at the specified key.
	Delete(key string)
}

The Cache interface defines a cache for storing arbitrary data. The interface is designed to align with httpcache.Cache.

type Options

type Options struct {
	// See ParseOptions for interpretation of Width and Height values
	Width  float64
	Height float64

	// If true, resize the image to fit in the specified dimensions.  Image
	// will not be cropped, and aspect ratio will be maintained.
	Fit bool

	// Rotate image the specified degrees counter-clockwise.  Valid values
	// are 90, 180, 270.
	Rotate int

	FlipVertical   bool
	FlipHorizontal bool

	// Quality of output image
	Quality int
}

Options specifies transformations to be performed on the requested image.

func ParseOptions

func ParseOptions(str string) Options

ParseOptions parses str as a list of comma separated transformation options. The following options can be specified in any order:

Size and Cropping

The size option takes the general form "{width}x{height}", where width and height are numbers. Integer values greater than 1 are interpreted as exact pixel values. Floats between 0 and 1 are interpreted as percentages of the original image size. If either value is omitted or set to 0, it will be automatically 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 for both height and width.

Depending on the size options specified, an image may be cropped to fit the requested size. In all cases, the original aspect ratio of the image will be preserved; 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 to fill the space, cropping if necessary to fit the exact dimension.

- If only one of the width or height values is specified, the image will be resized to fit the specified dimension, scaling the other dimension as needed to maintain the aspect ratio.

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

Rotation and Flips

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

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

Quality

The "q{qualityPercentage}" option can be used to specify the quality of the output file (JPEG only)

Examples

0x0       - no resizing
200x      - 200 pixels wide, proportional height
0.15x     - 15% original width, proportional height
x100      - 100 pixels tall, proportional width
100x150   - 100 by 150 pixels, cropping as needed
100       - 100 pixels square, cropping as needed
150,fit   - scale to fit 150 pixels square, no cropping
100,r90   - 100 pixels square, rotated 90 degrees
100,fv,fh - 100 pixels square, flipped horizontal and vertical
200x,q80  - 200 pixels wide, proportional height, 80% quality

func (Options) String

func (o Options) String() string

type Proxy

type Proxy struct {
	Client *http.Client // client used to fetch remote URLs
	Cache  Cache        // cache used to cache responses

	// Whitelist specifies a list of remote hosts that images can be
	// proxied from.  An empty list means all hosts are allowed.
	Whitelist []string
}

Proxy serves image requests.

Note that a Proxy should not be run behind a http.ServeMux, since the ServeMux aggressively cleans URLs and removes the double slash in the embedded request URL.

func NewProxy

func NewProxy(transport http.RoundTripper, cache Cache) *Proxy

NewProxy constructs a new proxy. The provided http RoundTripper will be used to fetch remote URLs. If nil is provided, http.DefaultTransport will be used.

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles image requests.

type Request

type Request struct {
	URL     *url.URL // URL of the image to proxy
	Options Options  // Image transformation to perform
}

Request is an imageproxy request which includes a remote URL of an image to proxy, and an optional set of transformations to perform.

func NewRequest

func NewRequest(r *http.Request) (*Request, error)

NewRequest parses an http.Request into an imageproxy Request. Options and the remote image URL are specified in the request path, formatted as: /{options}/{remote_url}. Options may be omitted, so a request path may simply contian /{remote_url}. The remote URL must be an absolute "http" or "https" URL, should not be URL encoded, and may contain a query string.

Assuming an imageproxy server running on localhost, the following are all valid imageproxy requests:

http://localhost/100x200/http://example.com/image.jpg
http://localhost/100x200,r90/http://example.com/image.jpg?foo=bar
http://localhost//http://example.com/image.jpg
http://localhost/http://example.com/image.jpg

type TransformingTransport

type TransformingTransport struct {
	// Transport is the underlying http.RoundTripper used to satisfy
	// non-transform requests (those that do not include a URL fragment).
	Transport http.RoundTripper

	// CachingClient is used to fetch images to be resized.  This client is
	// used rather than Transport directly in order to ensure that
	// responses are properly cached.
	CachingClient *http.Client
}

TransformingTransport is an implementation of http.RoundTripper that optionally transforms images using the options specified in the request URL fragment.

func (*TransformingTransport) RoundTrip

func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface.

type URLError

type URLError struct {
	Message string
	URL     *url.URL
}

URLError reports a malformed URL error.

func (URLError) Error

func (e URLError) Error() string

Directories

Path Synopsis
cmd
imageproxy
imageproxy starts an HTTP server that proxies requests for remote images.
imageproxy starts an HTTP server that proxies requests for remote images.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL