Convert Figma logo to code with AI

willnorris logoimageproxy

A caching, resizing image proxy written in Go

3,523
481
3,523
87

Top Related Projects

10,063

thumbor is an open-source photo thumbnail service by globo.com

Fast and secure standalone server for resizing and converting remote images

2,112

An image resizing server written in Go

Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing

1,027

Dockerized application that resizes and crops images on the fly, delivering optimized images in formats such as AVIF, WebP, MozJPEG, or PNG using ImageMagick, with an efficient caching system.

1,976

Source code of wsrv.nl (formerly images.weserv.nl), to be used on your own server(s).

Quick Overview

willnorris/imageproxy is a Go-based image proxy server that can resize, crop, and transform images on-the-fly. It fetches images from remote sources, processes them according to specified parameters, and serves the modified images, making it useful for optimizing image delivery in web applications.

Pros

  • Supports various image transformations including resizing, cropping, and rotation
  • Caching mechanism for improved performance
  • Flexible configuration options for security and resource management
  • Easy to deploy as a standalone server or integrate into existing Go applications

Cons

  • Limited to image processing tasks; not a general-purpose media server
  • Requires careful configuration to prevent abuse in public-facing deployments
  • May introduce additional latency for uncached images
  • Depends on external libraries for some image formats, which may require separate installation

Code Examples

Fetching and resizing an image:

import "willnorris.com/go/imageproxy"

proxy := imageproxy.NewProxy(nil, nil)
img, err := proxy.Get("https://example.com/image.jpg,w=300")
if err != nil {
    // Handle error
}
// Use the resized image

Setting up a custom cache:

import (
    "willnorris.com/go/imageproxy"
    "willnorris.com/go/imageproxy/cache/memory"
)

cache := memory.New(100 * 1024 * 1024) // 100MB memory cache
proxy := imageproxy.NewProxy(nil, cache)

Configuring allowed hosts:

proxy := imageproxy.NewProxy(nil, nil)
proxy.AllowHosts([]string{"example.com", "*.example.org"})

Getting Started

To use imageproxy in your Go project:

  1. Install the package:

    go get willnorris.com/go/imageproxy
    
  2. Import and set up a basic proxy server:

    package main
    
    import (
        "log"
        "net/http"
        "willnorris.com/go/imageproxy"
    )
    
    func main() {
        p := imageproxy.NewProxy(nil, nil)
        http.Handle("/", p)
        log.Fatal(http.ListenAndServe(":8080", nil))
    }
    
  3. Run your server and access images via URLs like: http://localhost:8080/300x200/https://example.com/image.jpg

This setup creates a basic image proxy server that listens on port 8080 and can resize images to 300x200 pixels.

Competitor Comparisons

10,063

thumbor is an open-source photo thumbnail service by globo.com

Pros of Thumbor

  • More feature-rich with advanced image processing capabilities like face detection and smart cropping
  • Supports a wider range of image formats and processing options
  • Highly customizable with numerous plugins and extensions available

Cons of Thumbor

  • More complex setup and configuration compared to Imageproxy
  • Higher resource requirements due to its extensive feature set
  • Steeper learning curve for new users

Code Comparison

Thumbor configuration example:

SECURITY_KEY = 'MY_SECURE_KEY'
ALLOW_UNSAFE_URL = False
LOADER = 'thumbor.loaders.http_loader'
STORAGE = 'thumbor.storages.file_storage'

Imageproxy configuration example:

import "willnorris.com/go/imageproxy"

p := imageproxy.NewProxy(nil, nil)
p.Verbose = true
p.SignatureKey = []byte("secret key")

Both projects aim to provide image proxying and manipulation capabilities, but they differ in their approach and feature set. Thumbor offers a more comprehensive solution with advanced image processing features, while Imageproxy focuses on simplicity and ease of use. The choice between the two depends on the specific requirements of your project and the level of complexity you're willing to manage.

Fast and secure standalone server for resizing and converting remote images

Pros of imgproxy

  • Written in Go, offering better performance and lower resource usage
  • Supports a wider range of image processing operations, including face detection and watermarking
  • Provides more advanced security features, such as signature verification and custom headers

Cons of imgproxy

  • More complex setup and configuration compared to imageproxy
  • Requires additional dependencies like libvips for image processing
  • May have a steeper learning curve for users new to image processing

Code Comparison

imageproxy:

http.HandleFunc("/", imageproxy.NewProxy(nil, nil).ServeHTTP)
http.ListenAndServe(":8080", nil)

imgproxy:

server := imgproxy.New()
server.Config.Development = true
server.Config.BindAddr = ":8080"
server.Run()

Both projects offer image proxying and processing capabilities, but imgproxy provides more advanced features and better performance at the cost of increased complexity. imageproxy is simpler to set up and use, making it a good choice for basic image proxying needs. imgproxy is better suited for projects requiring extensive image processing and higher performance.

2,112

An image resizing server written in Go

Pros of picfit

  • Supports multiple storage backends (file system, S3, Swift, etc.)
  • Offers more image processing operations (e.g., blur, overlay)
  • Provides a RESTful API for image manipulation

Cons of picfit

  • Less actively maintained (last commit over 2 years ago)
  • Fewer caching options compared to imageproxy
  • More complex setup and configuration

Code Comparison

imageproxy:

http.HandleFunc("/", imageproxy.NewProxy(nil, nil).ServeHTTP)
http.ListenAndServe(":8080", nil)

picfit:

config := picfit.NewConfig()
engine := picfit.NewEngine(config)
http.ListenAndServe(":3001", engine.Handler())

Both projects aim to provide image processing and serving capabilities, but they differ in their approach and feature set. imageproxy focuses on simplicity and efficient caching, while picfit offers more advanced image manipulation options and storage flexibility.

imageproxy is more actively maintained and has a simpler setup process, making it a good choice for projects that require basic image resizing and caching. picfit, on the other hand, is better suited for applications that need more complex image processing operations and support for various storage backends.

When choosing between the two, consider your specific requirements, such as the need for advanced image manipulation, storage options, and the level of active maintenance you prefer for your project's dependencies.

Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing

Pros of Imaginary

  • Supports a wider range of image processing operations, including face detection and watermarking
  • Offers multiple storage backends (File System, AWS S3, Google Cloud Storage)
  • Provides a Docker image for easy deployment

Cons of Imaginary

  • More complex setup and configuration compared to Imageproxy
  • Requires more system resources due to its extensive feature set
  • Less focus on caching and performance optimization

Code Comparison

Imageproxy:

http.HandleFunc("/", imageproxy.NewProxy(nil, nil).ServeHTTP)
http.ListenAndServe(":8080", nil)

Imaginary:

server, err := imaginary.New(imaginary.ServerOptions{
    Port: 8080,
    CORS: false,
    Gzip: true,
})
server.Start()

Both projects offer simple ways to set up an image processing server, but Imaginary provides more configuration options out of the box.

Imageproxy is lightweight and focuses on proxying and basic transformations, making it ideal for simpler use cases and performance-critical applications. Imaginary, on the other hand, offers a more comprehensive set of image processing features, making it suitable for complex image manipulation tasks at the cost of increased complexity and resource usage.

1,027

Dockerized application that resizes and crops images on the fly, delivering optimized images in formats such as AVIF, WebP, MozJPEG, or PNG using ImageMagick, with an efficient caching system.

Pros of flyimg

  • More comprehensive image manipulation features, including face detection and watermarking
  • Supports multiple storage backends (S3, Google Cloud Storage, etc.)
  • Includes a web interface for testing and debugging

Cons of flyimg

  • More complex setup and configuration
  • Potentially higher resource usage due to additional features
  • Less focus on simplicity and lightweight operation

Code Comparison

imageproxy:

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == "/favicon.ico" {
        http.Error(w, "404 Not Found", http.StatusNotFound)
        return
    }
    // ... (additional code)
}

flyimg:

public function upload(Request $request): JsonResponse
{
    try {
        if (!$request->files->get('file')) {
            throw new \Exception('File parameter is missing');
        }
        $uploadedFile = $this->uploadManager->uploadFile($request->files->get('file'));
        // ... (additional code)
    } catch (\Exception $e) {
        return $this->responder->generateErrorResponse($e->getMessage());
    }
}

The code snippets demonstrate the different approaches and languages used by each project. imageproxy uses Go and focuses on serving images, while flyimg uses PHP and includes more complex features like file uploads.

1,976

Source code of wsrv.nl (formerly images.weserv.nl), to be used on your own server(s).

Pros of Images

  • More comprehensive image processing features, including face detection and watermarking
  • Supports a wider range of image formats, including WebP and AVIF
  • Better documentation and examples for usage

Cons of Images

  • More complex setup and configuration
  • Potentially higher resource usage due to additional features
  • Less focus on simplicity and lightweight operation

Code Comparison

Images:

img, err := imaging.Open(inputPath)
if err != nil {
    return err
}
resized := imaging.Resize(img, width, height, imaging.Lanczos)

Imageproxy:

m := imaging.Resize(m, width, 0, imaging.Linear)
m = imaging.Crop(m, imaging.CenterAnchor)

Both projects use the imaging library for image manipulation, but Images offers more advanced processing options and supports a wider range of formats. Imageproxy focuses on simpler resizing and cropping operations, prioritizing performance and ease of use.

Images is better suited for projects requiring extensive image processing capabilities, while Imageproxy is ideal for lightweight, straightforward image proxying and basic transformations. The choice between the two depends on the specific needs of your project and the desired balance between features and simplicity.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

imageproxy

GoDoc Test Status Test Coverage CII Best Practices

imageproxy is a caching image proxy server written in go. It features:

  • basic image adjustments like resizing, cropping, and rotation
  • access control using allowed hosts list or request signing (HMAC-SHA256)
  • 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
  • easy deployment, since it's pure go

Personally, I use it primarily to dynamically resize images hosted on my own 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.

I aim to keep imageproxy compatible with the two most recent major go releases. I also keep track of the minimum go version that still works (currently go1.18), but that might change at any time. You can see the go versions that are tested against in .github/workflows/tests.yml.

URL Structure

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

Options

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.

See the full list of available options at https://godoc.org/willnorris.com/go/imageproxy#ParseOptions.

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.

OptionsMeaningImage
200x200px wide, proportional height200x
x0.1515% original height, proportional widthx0.15
100x150100 by 150 pixels, cropping as needed100x150
100100px square, cropping as needed100
150,fitscale to fit 150px square, no cropping150,fit
100,r90100px square, rotated 90 degrees100,r90
100,fv,fh100px square, flipped horizontal and vertical100,fv,fh
200x,q60200px wide, proportional height, 60% quality200x,q60
200x,png200px wide, converted to PNG format200x,png
cx175,cw400,ch300,100xcrop to 400x300px starting at (175,0), scale to 100px widecx175,cw400,ch300,100x

The smart crop feature can best be seen by comparing crops of this source image, with and without smart crop enabled.

OptionsMeaningImage
150x300150x300px, standard crop200x400,sc
150x300,sc150x300px, smart crop200x400

Transformation also works on animated gifs. Here is this source image resized to 200px square and rotated 270 degrees:

200,r270

Getting Started

Install the package using:

go install willnorris.com/go/imageproxy/cmd/imageproxy@latest

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 allowed host list (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.

Cache

By default, the imageproxy command does not cache responses, but caching can be enabled using the -cache flag. It supports the following values:

  • 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.

  • directory on local disk (e.g. /tmp/imageproxy) - will cache images on disk

  • s3 URL (e.g. s3://region/bucket-name/optional-path-prefix) - will cache 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 environmental variables be set. (Additional methods of loading credentials are documented in the aws-sdk-go session package).

    Additional configuration options (further documented here) may be specified as URL query string parameters, which are mostly useful when working with s3-compatible services:

    • "endpoint" - specify an alternate API endpoint
    • "disableSSL" - set to "1" to disable SSL when calling the API
    • "s3ForcePathStyle" - set to "1" to force the request to use path-style addressing

    For example, when working with minio, which doesn't use regions, provide a dummy region value and custom endpoint value:

    s3://fake-region/bucket/folder?endpoint=minio:9000&disableSSL=1&s3ForcePathStyle=1
    

    Similarly, for Digital Ocean Spaces, provide a dummy region value and the appropriate endpoint for your space:

    s3://fake-region/bucket/folder?endpoint=sfo2.digitaloceanspaces.com
    
  • gcs URL (e.g. gcs://bucket-name/optional-path-prefix) - will cache images on Google Cloud Storage. Authentication is documented in Google's Application Default Credentials docs.

  • azure URL (e.g. azure://container-name/) - will cache images on Azure Storage. This requires AZURESTORAGE_ACCOUNT_NAME and AZURESTORAGE_ACCESS_KEY environment variables to bet set.

  • 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. Rather than specify password in the URI, use the REDIS_PASSWORD environment variable.

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, one for the original full-size codercat image, and one for the resized 500px version.

Multiple caches can be specified by separating them by spaces or by repeating the -cache flag multiple times. The 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/

Allowed Referrer List

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 be enabled by running:

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.

Allowed and Denied Hosts List

You can limit the remote hosts that the proxy will fetch images from using the allowHosts and denyHosts flags. 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 these flags.

Try it out by running:

imageproxy -allowHosts example.com

Reload the codercat URL, and you should now get an error message. Alternately, try running:

imageproxy -denyHosts octodex.github.com

Reloading the codercat URL will still return an error message.

You can specify multiple hosts as a comma separated list to either flag, or prefix a host value with *. to allow or deny all sub-domains. You can also specify a netblock in CIDR notation (127.0.0.0/8) -- this is useful for blocking reserved ranges like 127.0.0.0/8, 192.168.0.0/16, etc.

If a host matches both an allowed and denied host, the request will be denied.

Allowed Content-Type List

You can limit what content types can be proxied by using the contentTypes flag. By default, this is set to image/*, meaning that imageproxy will process any image types. You can specify multiple content types as a comma separated list, and suffix values with * to perform a wildcard match. Set the flag to an empty string to proxy all requests, regardless of content type.

Signed Requests

Instead of an allowed host list, 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 "secretkey"

Reload the codercat URL, and you should see an error message. Now load a signed codercat URL (which contains the signature option) and verify that it loads properly.

Some simple code samples for generating signatures in various languages can be found in docs/url-signing.md. Multiple valid signature keys may be provided to support key rotation by repeating the signatureKey flag multiple times, or by providing a space-separated list of keys. To use a key with a literal space character, load the key from a file using the "@" prefix documented above.

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

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

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.

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

WebP and TIFF support

Imageproxy can proxy remote webp images, but they will be served in either jpeg 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.

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.

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.

Environment Variables

All configuration flags have equivalent environment variables of the form IMAGEPROXY_$NAME. For example, an on-disk cache could be configured by calling

IMAGEPROXY_CACHE="/tmp/imageproxy" imageproxy

Deploying

In most cases, you can follow the normal procedure for building a deploying any go application. For example:

  • go build willnorris.com/go/imageproxy/cmd/imageproxy
  • copy resulting binary to /usr/local/bin
  • copy 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.

Heroku

It's easy to vendorize the dependencies with Godep and deploy to Heroku. Take a look at this GitHub repo (make sure you use the heroku branch).

AWS Elastic Beanstalk

O’Reilly Media set up a repository with everything you need to deploy imageproxy to Elastic Beanstalk. Just follow the instructions in the README.

Docker

A docker image is available at ghcr.io/willnorris/imageproxy.

You can run it by

docker run -p 8080:8080 ghcr.io/willnorris/imageproxy -addr 0.0.0.0:8080

Or in your Dockerfile:

ENTRYPOINT ["/app/imageproxy", "-addr 0.0.0.0:8080"]

If running imageproxy inside docker with a bind-mounted on-disk cache, make sure the container is running as a user that has write permission to the mounted host directory. See more details in #198.

Note that all configuration options can be set using environment variables, which is often the preferred approach for containers.

nginx

Use the proxy_pass directive to send requests to your imageproxy instance. For example, to run imageproxy at the path "/api/imageproxy/", set:

  location /api/imageproxy/ {
    proxy_pass http://localhost:4593/;
  }

Depending on other directives you may have in your nginx config, you might need to alter the precedence order by setting:

  location ^~ /api/imageproxy/ {
    proxy_pass http://localhost:4593/;
  }

Clients

License

imageproxy is copyright its respective authors. All of my personal work on imageproxy through 2020 (which accounts for the majority of the code) is copyright Google, my employer at the time. It is available under the Apache 2.0 License.