imstor

package module
v0.0.0-...-61f1049 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2015 License: MIT Imports: 17 Imported by: 2

README

Imstor

A Golang image storage engine. Used to create and store different sizes/thumbnails of user uploaded images.

Build Status Coverage GoDoc

Description

Imstor enables you to create copies (or thumbnails) of your images and stores them along with the original image on your filesystem. The image and its copies are stored in a file structure based on the (zero-prefixed, decimal) CRC 64 checksum of the original image. The last 2 characters of the checksum are used as the lvl 1 directory name.

Imstor supports any image format you can decode to go's own image.Image and then back to your preferred format. The decoder for any given image is chosen by the image's mimetype.

Example folder structure

Folder name and contents, given the checksum 08446744073709551615 and sizes named "small" and "large":

/configured/root/path/15/08446744073709551615/original.jpg
/configured/root/path/15/08446744073709551615/small.jpg
/configured/root/path/15/08446744073709551615/large.jpg

Usage

See tests for usage examples.

Documentation

Overview

Package imstor enables you to create copies (or thumbnails) of your images and stores them along with the original image on your filesystem. The image and its copies are are stored in a file structure based on the (zero-prefixed, decimal) CRC 64 checksum of the original image. The last 2 characters of the checksum are used as the lvl 1 directory name.

Example folder name and contents, given the checksum 08446744073709551615 and sizes named "small" and "large":

/configured/root/path/15/08446744073709551615/original.jpeg
/configured/root/path/15/08446744073709551615/small.jpeg
/configured/root/path/15/08446744073709551615/large.jpeg

Index

Constants

This section is empty.

Variables

View Source
var DefaultResizer = defaultResizer{}

Functions

This section is empty.

Types

type Config

type Config struct {
	RootPath  string
	CopySizes []Size
	Formats   []Format
}

func NewConfig

func NewConfig(copySizes []Size, formats []Format) *Config

type Format

type Format interface {
	DecodableMediaType() string
	Decode(io.Reader) (image.Image, error)
	Encode(io.Writer, image.Image) error
	EncodedExtension() string
}

A Format describes how an image of a certaing mimetype can be decoded and then encoded.

var JPEGFormat Format = jpegFormat{}

JPEGFormat decodes a jpeg image and encodes it as a JPEG with the extension jpg

var PNG2JPEG Format = png2JPEG{}

PNG2JPEG format decodes an image from the PNG format and encodes it as a JPEG

type Resizer

type Resizer interface {
	// Resize should scale an image to new width and height. If one of the
	// parameters width or height is set to 0, its size will be calculated so that
	// the aspect ratio is that of the originating image.
	Resize(width, height uint, i image.Image) image.Image
	// Thumbnail should downscale provided image to max width and height preserving
	// original aspect ratio. It should return original image, without processing,
	// if original sizes are already smaller than the provided constraints.
	Thumbnail(maxWidth, maxHeight uint, i image.Image) image.Image
}

A Resizer can resize an image into the given dimensions

type Size

type Size struct {
	Name   string
	Height uint
	Width  uint
}

Size specifies a set of dimensions and a name that a copy of an image will be stored as

type Storage

type Storage interface {
	Store(mediaType string, data []byte) error
	StoreDataURL(string) error
	Checksum([]byte) string
	ChecksumDataURL(string) (string, error)
	PathFor(checksum string) (string, error)
	PathForSize(checksum, size string) (string, error)
	HasSizesForChecksum(checksum string, sizes []string) (bool, error)
	GetSize(checksum, size string) (image.Image, error)
}

Storage is the engine that can be used to store images and retrieve their paths

func New

func New(conf *Config) Storage

New creates a storage engine using the default Resizer

func NewWithCustomResizer

func NewWithCustomResizer(conf *Config, resizer Resizer) Storage

NewWithCustomResizer creates a storage engine using a custom resizer

Jump to

Keyboard shortcuts

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