imagor

repository·master·Indexed 26 days ago

https://github.com/cshum/imagor

A high-performance, secure image processing server and Go library built on top of libvips. It provides a thumbor-style image transformation URL system and includes packages for concurrent data streaming (fanoutreader), signed endpoint generation (imagorpath), seekable streams (seekstream), and a flexible Blob abstraction for handling various image formats and sources.

Tokens
26.1K
Snippets
80
Records
157
Agent score
87%

What's inside imagor

  1. Explore imagor community projects

    master

    Several community projects extend or integrate with imagor:

    • imagor-studio: A self-hosted image workspace featuring built-in editing, layered compositing, and reusable templates.
    • imagor-toy: A ReactJS-based application designed for experimenting with imagor features.
  2. Understand imagor Loader, Storage, and Result Storage

    master

    imagor uses three primary components to manage image lifecycle:

    • Loader: Responsible for loading the original source images.
    • Storage: Loads and saves source images to allow reuse on subsequent requests.
    • Result Storage: Loads and saves processed (transformed) images to allow reuse on subsequent requests.

    By default, imagor uses the HTTP Loader for remote HTTP/HTTPS images. Additional adaptors for Storage and Result Storage can be enabled based on your requirements.

  3. Explore official imagor plugins

    master

    The imagor ecosystem includes official plugins for specialized media processing:

    • imagorvideo: A video thumbnail server that utilizes ffmpeg C bindings.
    • imagorface: Provides fast, on-the-fly face detection, which can be used for smart cropping or privacy redaction.
  4. Understand imagor performance and benchmarks

    master

    imagor is a high-performance image processing server that leverages libvips and vipsgen (Go bindings) to achieve high throughput. It is designed to be competitive with other major image servers like imgproxy and generally faster than thumbor due to its efficient streamed request path and reduced buffering.

    Performance Characteristics

    • Strengths: High throughput for PNG and AVIF workloads; highly efficient for resize and thumbnail tasks.
    • Comparison: In recent benchmarks on AWS c7i.large, imagor outperformed thumbor across JPEG, PNG, WebP, and AVIF. It is competitive with imgproxy, leading in PNG and AVIF, while imgproxy led in JPEG and WebP.
    • Architecture: The speed comes from preparing streamed inputs for efficient libvips consumption, reusing blobs to reduce reader churn, and using a seekable libvips source path.
  5. Understand the imagor URL syntax

    master

    The imagor endpoint is constructed as a series of URL parts defining operations, followed by the image URI. The general structure is:

    /HASH|unsafe/trim/AxB:CxD/(adaptive-)(full-)fit-in/stretch/-Ex-F/GxH:IxJ/HALIGN/VALIGN/smart/filters:NAME(ARGS):NAME(ARGS):.../IMAGE

    Key components include:

    • HASH: The URL signature hash (use unsafe for unsigned URLs).
    • trim: Removes surrounding whitespace.
    • AxB:CxD: Manual crop coordinates.
    • fit-in, full-fit-in, adaptive-fit-in: Resizing modes that do not auto-crop.
    • stretch: Resizes without preserving aspect ratio.
    • -Ex-F: Resizing with optional flipping (using - signs).
    • GxH:IxJ: Padding dimensions.
    • HALIGN/VALIGN: Alignment for crops (left, right, center / top, bottom, middle).
    • smart: Uses focal point detection for cropping.
    • filters: A pipeline of operations applied after resizing.
    • IMAGE: The source image path or URI.
  6. Secure imagor with URL signatures

    master

    In production, you must set the IMAGOR_SECRET environment variable. This requires every request URL to carry a valid HMAC signature, preventing DDoS attacks and unauthenticated use.

    Warning: Do not use IMAGOR_UNSAFE in production, as it bypasses signature verification entirely.

  7. Add padding to images

    master

    Add padding around the image after resizing using the format GxH:IxJ, where GxH is the left/top padding and IxJ is the right/bottom padding. This is often used with fit-in and the fill() filter to create colored borders.

    /unsafe/fit-in/360x360/20x20:20x20/filters:fill(yellow)/IMAGE
  8. Tune libvips concurrency with VIPS_CONCURRENCY

    master

    You can control the number of threads libvips uses for image operations by setting the VIPS_CONCURRENCY environment variable.

    Note: This is a global setting that controls threading within each individual image operation, not the number of concurrent requests handled by imagor.

    • VIPS_CONCURRENCY=1 (Default): Single-threaded processing. Recommended for most deployments where concurrency is managed at the application level (e.g., via multiple imagor processes or containers).
    • VIPS_CONCURRENCY=-1: Uses all available CPU cores. This can speed up processing for single large images but may cause resource contention under high request concurrency.
    • VIPS_CONCURRENCY=[number]: Sets a specific number of threads for fine-tuned control.
    VIPS_CONCURRENCY=1    # Single-threaded (default)
    VIPS_CONCURRENCY=-1   # Use all available CPU cores
    VIPS_CONCURRENCY=4    # Use 4 threads
  9. Use fanoutreader to concurrently stream from a single data source

    master

    The fanoutreader package allows you to fan out an arbitrary number of reader streams concurrently from a single data source, provided the total size of the data is known. This is achieved using memory buffers and channels, preventing backpressure from one slow consumer from blocking other consumers (unlike io.TeeReader or io.MultiWriter).

    To use it, wrap your io.ReadCloser source using fanoutreader.New(source, size) and then generate individual readers using fanout.NewReader().

  10. Trim whitespace from images

    master

    Remove surrounding borders or whitespace by detecting the background color from a corner pixel. Trim is applied before resizing.

    • trim: Uses the top-left corner pixel as the reference color.
    • trim:bottom-right: Uses the bottom-right corner pixel.
    • :TOLERANCE: An optional integer to control color variation sensitivity.
    /unsafe/trim/IMAGE
    /unsafe/trim:bottom-right/IMAGE
    /unsafe/trim:100/IMAGE
    /unsafe/trim:bottom-right:100/IMAGE
  11. Parse and generate imagor endpoints with imagorpath

    master

    The imagorpath package allows you to programmatically generate and parse signed imagor endpoint paths using Go structs. You can define image transformation parameters in an imagorpath.Params struct and use a signer to generate a secure path, or parse an existing signed path back into a Params struct.

    import "github.com/cshum/imagor/imagorpath"
    
    // 1. Define your transformation parameters
    params := imagorpath.Params{
    	Image:      "path/to/image.png",
    	FitIn:      true,
    	Width:      500,
    	Height:     400,
    	PaddingTop: 20,
    	PaddingBottom: 20,
    	Filters: imagorpath.Filters{
    		{
    			Name: "fill",
    			Args: "white",
    		},
    	},
    }
    
    // 2. Generate a signed endpoint path using a signer
    // NewDefaultSigner requires your secret key
    path := imagorpath.Generate(params, imagorpath.NewDefaultSigner("mysecret"))
    
    // 3. Parse a signed path back into a Params struct
    parsedParams := imagorpath.Parse(path)