imgry

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2016 License: MIT Imports: 7 Imported by: 4

README

Imgry

Imgry is an on-demand image delivery web service for responsive applications.

Usage

First install Go 1.7+ and copy the etc/imgry.conf.sample (the default is fine), then..

cd imgry/
make tools
make deps
make build
./bin/imgry-server -config=etc/imgry.conf

Open browser to:

http://localhost:4446/mybucket?url=http://i.imgur.com/vEZy2Oh.jpg

this will download the image from the source, cache it, persist it, and return the sized image (in this case, with zero sizing) to the client.

Now, some other variations:

Scale to 300x

http://localhost:4446/mybucket?url=http://i.imgur.com/vEZy2Oh.jpg&size=300x

Resize to exactly 300x300

http://localhost:4446/mybucket?url=http://i.imgur.com/vEZy2Oh.jpg&size=300x300

Resize to 300x300 and maintain aspect ratio

http://localhost:4446/mybucket?url=http://i.imgur.com/vEZy2Oh.jpg&size=300x300&op=cover

Same as above with a cropbox at points (x1:10%,y1:10%) to (x2:90%,y2:90%)

http://localhost:4446/mybucket?url=http://i.imgur.com/vEZy2Oh.jpg&size=300x300&op=cover&cb=0.1,0.1,0.9,0.9

Webapp usage

<img src="http://localhost:4446/mybucket?url=http%3A%2F%2Fi.imgur.com%2FvEZy2Oh.jpg&size=300x300&op=cover" />

Caching and persistence

Imgry is built with some clever caching such as:

  • A layered cache store that stores/loads data from memory > on-disk (boltdb) > s3
  • Once an image has been downloaded once, every other sizing operation will be loaded from the chainstore
  • Saving to the on-disk and s3 layers are done in the background
  • Hashing of the sizing operations to find already sized images
  • Redisdb is used for storing the bucket information of images sized

Deployment

A Dockerfile is packaged with the project that includes a custom build of ImageMagick 6.9 with the latest libjpeg-turbo and libpng.

We use github.com/siddontang/ledisdb in production instead of redisdb. It's an Redis-API compatible engine that is designed for long-term persistence of the data set.. pretty much Redis on LevelDB.

Other

  • Imgry and its sizing operations can be used as a library, without the API server
  • Imgry supports pluggable image processing engines, but for now comes packaged with an ImageMagick engine by default (imgry/imagick)

License

MIT License (See LICENSE file)

Documentation

Index

Constants

View Source
const (
	VERSION = "1.1.0"
)

Variables

View Source
var (
	ZeroSizing = &Sizing{}

	DefaultSizingGranularity = 10
)
View Source
var (
	ZeroRect         = &Rect{}
	ZeroFloatingRect = &FloatingRect{&FloatPoint{}, &FloatPoint{}}
	ZeroFloatPoint   = &FloatPoint{}
)
View Source
var (
	ErrInvalidImageData = errors.New("invalid image data")
)

Functions

This section is empty.

Types

type Engine

type Engine interface {
	Version() string
	Initialize(tmpDir string) error
	Terminate()

	LoadFile(filename string, srcFormat ...string) (Image, error)
	LoadBlob(b []byte, srcFormat ...string) (Image, error)
	GetImageInfo(b []byte, srcFormat ...string) (*ImageInfo, error)
}

type FloatPoint

type FloatPoint struct {
	X, Y float64
}

func NewFloatPoint

func NewFloatPoint(x, y float64) *FloatPoint

func NewFloatPointFromQuery

func NewFloatPointFromQuery(q string) (*FloatPoint, error)

func (*FloatPoint) Equal

func (f *FloatPoint) Equal(other *FloatPoint) bool

Returns whether the two FloatPoints have equal values

func (*FloatPoint) ToString

func (f *FloatPoint) ToString() string

Returns a comma delimited string representing the point (ie. "0.1,0.2" or "100,150")

type FloatingRect

type FloatingRect struct {
	Min, Max *FloatPoint // can be whole or percentages
}

func NewFloatingRect

func NewFloatingRect(x0, y0, x1, y2 float64) *FloatingRect

func NewFloatingRectFromQuery

func NewFloatingRectFromQuery(q string) (*FloatingRect, error)

func (*FloatingRect) Equal

func (f *FloatingRect) Equal(other *FloatingRect) bool

func (*FloatingRect) ToString

func (f *FloatingRect) ToString() string

type Image

type Image interface {
	Data() []byte
	Width() int
	Height() int
	Format() string
	SetFormat(format string) error

	Release()
	Released() bool

	SizeIt(sizing *Sizing) error
	WriteToFile(string) error
}

type ImageInfo

type ImageInfo struct {
	URL           string  `json:"url"`
	Format        string  `json:"format"`
	Mimetype      string  `json:"mimetype"`
	Width         int     `json:"width"`
	Height        int     `json:"height"`
	AspectRatio   float64 `json:"aspect_ratio"`
	ContentLength int     `json:"content_length"`
}

type Rect

type Rect struct {
	Width, Height int
}

func NewRect

func NewRect(w, h int) *Rect

func NewRectFromQuery

func NewRectFromQuery(q string) (*Rect, error)

func (*Rect) AspectRatio

func (r *Rect) AspectRatio() float64

func (*Rect) DiffSize

func (r *Rect) DiffSize(src *Rect) (int, int)

Return the width and height difference with the src rect

func (*Rect) Equal

func (r *Rect) Equal(other *Rect) bool

func (*Rect) ToString

func (r *Rect) ToString() string

type Sizing

type Sizing struct {
	Size       *Rect         // The asking image size
	CropBox    *FloatingRect // The asking image crop box (as percentages)
	FocalPoint *FloatPoint   // The asking image focal point (as percentages)
	Canvas     *Rect

	Op          string
	Format      string
	Quality     int
	Granularity int
	Flatten     bool
}

func NewSizing

func NewSizing() *Sizing

func NewSizingFromQuery

func NewSizingFromQuery(q string) (*Sizing, error)

func (*Sizing) CalcCropBox

func (sz *Sizing) CalcCropBox(srcSize *Rect) (cropBox *Rect, cropOrigin *image.Point, err error)

func (*Sizing) CalcResizeRect

func (sz *Sizing) CalcResizeRect(srcSize *Rect) (resizedRect *Rect, cropRect *Rect, cropOrigin *image.Point)

func (*Sizing) GranularizedHeight

func (sz *Sizing) GranularizedHeight() int

Returns the granularized asking height

func (*Sizing) GranularizedSize

func (sz *Sizing) GranularizedSize() *Rect

func (*Sizing) GranularizedWidth

func (sz *Sizing) GranularizedWidth() int

Returns the granularized width

func (*Sizing) SetFromQuery

func (sz *Sizing) SetFromQuery(q string) error

func (*Sizing) ToQuery

func (sz *Sizing) ToQuery() url.Values

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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