imgscale

package
v0.0.0-...-249d4e9 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2017 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

Package imagscale is middleware handler for scaling image in golang. Use for serving images in different predefined formats.

import (
        "fmt"
        "github.com/codegangsta/negroni"
        "github.com/go-martini/martini"
        "github.com/vanng822/imgscale/imgscale"
        "net/http"
)

func main() {
        n := negroni.New()
        handler := imgscale.Configure("./config/formats.json")
        defer handler.Cleanup()
        // Example how to run an arbitrary remote image provider
        handler.SetImageProvider(imgscale.NewImageProviderHTTP(""))
        n.UseHandler(handler)
        go http.ListenAndServe(fmt.Sprintf("%s:%d", "127.0.0.1", 8081), n)

        // Martini
        app := martini.Classic()
        mhandler := imgscale.Configure("./config/formats.json")
        defer mhandler.Cleanup()
        app.Use(mhandler.ServeHTTP)
        go http.ListenAndServe(fmt.Sprintf("%s:%d", "127.0.0.1", 8080), app)

        // http.Handler
        handler2 := imgscale.Configure("./config/formats.json")
        defer handler2.Cleanup()
        // Example how to run an host limited remote image provider, can not run arbitrary here
        handler2.SetImageProvider(imgscale.NewImageProviderHTTP("http://127.0.0.1:8080/img/original/"))
        http.Handle("/", handler2)
        http.ListenAndServe(fmt.Sprintf("%s:%d", "127.0.0.1", 8082), nil)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoRotate

func AutoRotate(img *imagick.MagickWand) (err error)

AutoRotate will rotate the image to right "viewing perspective" by inspecting orientation of the image. If unknown orientation the image will leave unchanged

func ProcessImage

func ProcessImage(img *imagick.MagickWand, info *ImageInfo) (err error)

ProcessImage will crop/scale image to dimension specified in ImageInfo

Types

type Config

type Config struct {
	Path       string
	Prefix     string
	Separator  string
	Formats    []*Format
	Exts       []string
	Comment    string
	AutoRotate bool
	Watermark  *Watermark
	// contains filtered or unexported fields
}

Path: path of the folder which contains images

Prefix: use as image path indicator in url

Separator: separator between format prefix and filename

Formats: list of Format

Exts: allow extensions, only jpg, jpeg and png available. Be aware that jpg and jpeg handled as different extension.

Comment: will store in meta data if specified

AutoRotate: Autorotate image according to orientation stored in the image meta data

Watermark: this watermark is used when watermark on a format is enabled

func LoadConfig

func LoadConfig(filename string) *Config

LoadConfig parse configuration file. It will panic if any error

type Format

type Format struct {
	Prefix    string
	Height    uint
	Width     uint
	Ratio     float64
	Thumbnail bool
	Watermark bool
	Quality   uint
	Strip     bool
}

Prefix: use in the url to identify the format

Height: the target height

Width: only use on scale only

Ratio: zero for keeping ratio

Thumbnail: true to use thumbnail feature in imagemagick, it is quick and optimized but you loose meta data

Watermark: Indicate if watermark should apply on this format

Quality: Compression quality

Strip: strips all profiles and original comments

type Handler

type Handler interface {
	// http.HandleFunc
	ServeHTTP(res http.ResponseWriter, req *http.Request)
	// For setting own image provider, such as remote storage
	SetImageProvider(provider ImageProvider)
	// For setting validator of filename/name of the image
	SetValidator(validator Validator)
	// Free C pointers and terminate MagickWand environment
	Cleanup()
	// Reload configuration
	Reload()
	// func(next http.Handler) http.Handler
	Middleware() func(next http.Handler) http.Handler
	// HandlerFunc With Next HandlerFunc
	HandlerFuncWithNext() func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}

Handler implements http.Handler so it can use for many frameworks available Handler.ServeHTTP can use similar to http.HandleFunc in case frameworks support only this.

Beside that Handler has some more methods, Handler.SetValidator for setting own validation of the filename Handler.SetImageProvider is suitable when you have customized image provider, default is imageProviderFile. And the last method Handler.Cleanup should always call at the end (or defer) to cleanup C pointers. Reload will reload new configurations without stopping service

func Configure

func Configure(filename interface{}) Handler

Configure returns Handler which implement http.Handler Filename is the configuration file in json, content looks something like this

{
	"Path": "./data",
	"Prefix": "img",
	"Formats": [
		{"Prefix": "100x100", "Height": 100, "Ratio": 1.0, "Thumbnail": true},
		{"Prefix": "66x100", "Height": 100, "Ratio": 0.67, "Thumbnail": true},
		{"Prefix": "100x75", "Height": 75, "Ratio": 1.335, "Thumbnail": true},
		{"Prefix": "100x0", "Height": 100, "Ratio": 0.0, "Thumbnail": true, "Watermark": true},
		{"Prefix": "originalx1", "Height": 0, "Ratio": 1.0, "Thumbnail": false, "Watermark": true},
		{"Prefix": "original", "Height": 0, "Ratio": 0.0, "Thumbnail": false, "Watermark": true}
	],
	"Separator": "/",
	"Exts": ["jpg", "jpeg", "png"],
	"Comment": "Copyright",
	"AutoRotate": true,
	"Watermark": {"Filename": "./data/eyes.gif"}
}

The returned handler could use as middleware handler

Negroni middleware:

app := negroni.New()
handler := imgscale.Configure("./config/formats.json")
defer handler.Cleanup()
app.UseHandler(handler)
http.ListenAndServe(fmt.Sprintf("%s:%d", "127.0.0.1", 8080), app)

Martini middleware:

app := martini.Classic()
handler := imgscale.Configure("./config/formats.json")
defer handler.Cleanup()
app.Use(handler.ServeHTTP)
http.ListenAndServe(fmt.Sprintf("%s:%d", "127.0.0.1", 8080), app)

http.Handle:

handler := imgscale.Configure("./config/formats.json")
defer handler.Cleanup()
http.Handle("/", handler)
http.ListenAndServe(fmt.Sprintf("%s:%d", "", 8080), nil)

type ImageInfo

type ImageInfo struct {
	Filename string
	Format   *Format
	Comment  string
}

Filename: <baseUrl>/<image prefix>/<format prefix>/<Filename>

Format: See Format

Comment: will be written in image meta data

type ImageProvider

type ImageProvider interface {
	Fetch(filename string) (*imagick.MagickWand, error)
}

ImageProvider implements image fetching and provide the image source for the handler to serve the request. Local filesystem imageProviderFile is bundled but there are more providers. Take look at provider folder Fetch has to return MagickWand in favour of imageProviderFile. This case we read image data directly to MagickWand

func NewImageProviderFile

func NewImageProviderFile(path string) ImageProvider

NewImageProviderFile returns an instance of imageProviderFile the path is required and should point to folder where images are located

type Validator

type Validator interface {
	// Name of the image, ie everything after "<prefix>/<format><separator>"
	Validate(filename string) bool
}

Validator implements the validation of the filename The filename can identified as baseurl/<image prefix>/<format prefix><separator><filename> An example is http://127.0.0.1:8080/img/original/kth.jpg and filename is kth.jpg

type Watermark

type Watermark struct {
	Filename string
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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