thumbnail

package
v2.9.275 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

README

Fotomat Thumbnail

Go-based API that allows on-the-fly resizing and cropping of JPEG, PNG, GIF, and WebP images. See the list of features of this library.

Also see:

Documentation

Index

Constants

View Source
const (
	// DefaultAccept is the default Accept header sent on upstream requests.
	DefaultAccept = "image/jpeg,image/webp;q=0.8,image/png;q=0.4,image/svg+xml;q=0.3,image/gif;q=0.2,application/pdf;q=0.1"
	// DefaultServer is the default Server header sent with responses.
	DefaultServer = "Fotomat"
	// DefaultUserAgent is the default User-Agent header sent on upstream requests.
	DefaultUserAgent = "Fotomat (http://fotomat.org)"
)

Variables

View Source
var (
	// ErrBadOption is returned when option values are out of range.
	ErrBadOption = errors.New("bad option specified")
	// ErrTooBig is returned when an image is too wide or tall.
	ErrTooBig = errors.New("image is too wide or tall")
	// ErrTooSmall is returned when an image is too small.
	ErrTooSmall = errors.New("image is too small")
)
View Source
var (
	// ErrAborted means the operation wasn't executed because the
	// Request.Aborted channel was closed by the caller.
	ErrAborted = errors.New("thumbnail request aborted")
)

Functions

func Thumbnail

func Thumbnail(blob []byte, o Options) ([]byte, error)

Thumbnail scales or crops a compressed image blob according to the Options specified in o and returns a compressed image. Should be called from a thread pool with runtime.LockOSThread() locked.

Types

type Options

type Options struct {
	// Width and Height are the optional maximum sizes of output image,
	// in pixels.  If Crop is false, the original aspect ratio is
	// preserved and the more restrictive of Width or Height are used.
	Width  int
	Height int
	// Crop enables crop mode, where exact supplied Width:Height aspect
	// ratio is preserved and excess pixels are trimmed from the sides.
	Crop bool
	// Sharpen runs a mild sharpening pass on downsampled images.
	Sharpen bool
	// BlurSigma performs a gaussian blur with specified sigma.
	BlurSigma float64
	// MaxBufferPixels specifies how large of an intermediate image
	// buffer to allow, in pixels. RAM usage will be a few bytes per pixel.
	MaxBufferPixels int
	// MaxQueueDuration limits the amount of time spent in a queue before processing starts.
	MaxQueueDuration time.Duration
	// MaxProcessingDuration limits the amount of time processing an
	// image, after which it is assumed the operation has crashed and
	// the server aborts, killing all outstanding requests.
	MaxProcessingDuration time.Duration
	// Save specifies the format.SaveOptions to use when compressing the modified image.
	Save format.SaveOptions
}

Options specifies how a Thumbnail operation should modify an image.

func (Options) Check

func (o Options) Check(m format.Metadata) (Options, error)

Check verifies Options against Metadata and returns a modified Options or an error.

type Pool

type Pool struct {
	RequestCh chan *Request
	// contains filtered or unexported fields
}

Pool represents a Thumbnail worker pool. VIPS keeps thread-local caches, which we retain control over through a combination of a pool of worker goroutines and using runtime.LockOSThread() within those workers.

func NewPool

func NewPool(workers, queueLen int) *Pool

NewPool creates a Thumbnail worker pool with a given number of worker threads and queue length.

func (*Pool) Close

func (p *Pool) Close()

Close shuts down the worker pool and waits for remaining work to be done.

func (*Pool) Thumbnail

func (p *Pool) Thumbnail(ctx context.Context, blob []byte, options Options) ([]byte, error)

Thumbnail is a blocking wrapper that executes thumbnail.Thumbnail requests in a pool of worker threads. Work is skipped if aborted is closed while the request is queued.

type Proxy

type Proxy struct {
	Director  func(*http.Request) (Options, int)
	Client    *http.Client
	Accept    string
	Server    string
	UserAgent string
	// contains filtered or unexported fields
}

Proxy represents an HTTP proxy that can optionally run its contents through Thumbnail. Must be created with NewProxy.

func NewProxy

func NewProxy(director func(*http.Request) (Options, int), pool *Pool, maxActive int, client *http.Client) *Proxy

NewProxy creates a Proxy object, with a given Director, Pool, upper limit on images held in RAM, and http.Client.

func (*Proxy) Close

func (p *Proxy) Close()

Close shuts down a Proxy.

func (*Proxy) ServeHTTP

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

ServeHTTP serves an HTTP request for a given Proxy, using Director to parse the request, fetching an image, calling pool.Thumbnail on it, and returning the result.

type Request

type Request struct {
	Blob       []byte
	Options    Options
	Context    context.Context
	ResponseCh chan<- *Response
}

Request to be sent to Pool.RequestCh to queue a Thumbnail operation.

type Response

type Response struct {
	Blob  []byte
	Error error
}

Response sent to Request.ResponseCh when the Thumbnail operation is done.

Jump to

Keyboard shortcuts

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