img

package
v6.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CacheTTL int

Number of seconds that will be written to max-age HTTP header

Log writer that can be overrided. Should implement interface glogi.Logger. By default is using glogi.SimpleLogger.

Functions

This section is empty.

Types

type Command

type Command struct {
	Optimise         OptimiseCmd
	Resize           ResizeCmd
	Image            []byte
	ImgId            string
	Size             string
	Resp             http.ResponseWriter
	SupportedFormats []string
	Result           *Image
	FinishedCond     *sync.Cond
	Finished         bool
	Err              error
}

type Image

type Image struct {
	Data     []byte
	MimeType string
}

type Info

type Info struct {
	Format  string
	Quality int
	Opaque  bool
	Width   int
	Height  int
	// Size is the size of the image in bytes
	Size int64
}

Info holds basic information about an image

type Loader

type Loader interface {
	// Load loads an image from the given source.
	//
	// ctx is a context of the current transaction. Typically it's a context
	// of an incoming HTTP request, so it's possible to pass values through middlewares.
	//
	// Returns an image.
	Load(src string, ctx context.Context) (*Image, error)
}

Loaders is responsible for loading an original image for transformation

type OpCallback

type OpCallback func()

type OptimiseCmd

type OptimiseCmd func([]byte, string, []string) (*Image, error)

type Processor

type Processor interface {
	// Resize resizes given image preserving aspect ratio.
	// Format of the the size argument is width'x'height.
	// Any dimension could be skipped.
	// For example:
	//* 300x200
	//* 300 - only width
	//* x200 - only height
	Resize(data []byte, size string, imageId string, supportedFormats []string) (*Image, error)

	// FitToSize resizes given image cropping it to the given size and does not respect aspect ratio.
	// Format of the the size string is width'x'height, e.g. 300x400.
	FitToSize(data []byte, size string, imageId string, supportedFormats []string) (*Image, error)

	// Optimise optimises given image to reduce size of the served image.
	Optimise(data []byte, imageId string, supportedFormats []string) (*Image, error)
}

Processor is an interface for transforming/optimising images.

Each function accepts original image and a list of supported output format by client. Each format should be a MIME type, e.g. image/png, image/webp. The output image will be encoded in one of those formats.

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

func NewQueue

func NewQueue() *Queue

func (*Queue) AddAndWait

func (q *Queue) AddAndWait(op *Command, callback OpCallback)

type ResizeCmd

type ResizeCmd func([]byte, string, string, []string) (*Image, error)

type Service

type Service struct {
	Loader    Loader
	Processor Processor
	Q         []*Queue
	// contains filtered or unexported fields
}

func NewService

func NewService(r Loader, p Processor, procNum int) (*Service, error)

func (*Service) AsIs

func (r *Service) AsIs(resp http.ResponseWriter, req *http.Request)

swagger:operation GET /img/{imgUrl}/asis asisImage

Respond with original image without any modifications

--- tags: - images produces: - image/png - image/jpeg parameters:

  • name: imgUrl required: true in: path type: string description: url of the image

responses:

'200':
  description: Requested image.

func (*Service) FitToSizeUrl

func (r *Service) FitToSizeUrl(resp http.ResponseWriter, req *http.Request)

swagger:operation GET /img/{imgUrl}/fit fitImage

Resize image to the exact size and optimizes it. Will resize image and crop it to the size. If you need to resize image with preserved aspect ratio then use /img/resize endpoint.

--- tags: - images produces: - image/png - image/jpeg parameters:

  • name: imgUrl required: true in: path type: string description: url of the original image
  • name: size required: true in: query type: string pattern: \d{1,4}x\d{1,4} description: | size of the image in the response. Should be in the format 'width'x'height', e.g. 200x300

responses:

'200':
  description: Resized image in the same format as original.

func (*Service) GetRouter

func (r *Service) GetRouter() *mux.Router

func (*Service) OptimiseUrl

func (r *Service) OptimiseUrl(resp http.ResponseWriter, req *http.Request)

swagger:operation GET /img/{imgUrl}/optimise optimiseImage

Optimises image from the given url.

--- tags: - images produces: - image/png - image/jpeg parameters:

  • name: imgUrl required: true in: path type: string description: url of the original image

responses:

'200':
  description: Optimised image in the same format as original.

func (*Service) ResizeUrl

func (r *Service) ResizeUrl(resp http.ResponseWriter, req *http.Request)

swagger:operation GET /img/{imgUrl}/resize resizeImage

Resize image with preserving aspect ratio and optimizes it. If you need the exact size then use /fit operation.

--- tags: - images produces: - image/png - image/jpeg parameters:

  • name: imgUrl required: true in: path type: string description: url of the original image
  • name: size required: true in: query type: string description: | size of the image in the response. Should be in format 'width'x'height', e.g. 200x300 Only width or height could be passed, e.g 200, x300.

responses:

'200':
  description: Resized image in the same format as original.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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