img

package
v7.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 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 is a logger that could be overridden. Should implement interface glogi.Logger. By default is using glogi.SimpleLogger.

View Source
var SaveDataEnabled bool = true

SaveDataEnabled is flag to enable/disable Save-Data client hint. Sometime CDN doesn't support Save-Data in Vary response header in which case you would need to set this to false

Functions

This section is empty.

Types

type Cmd

type Cmd func(input *TransformationConfig) (*Image, error)

type Command

type Command struct {
	Transformation Cmd
	Config         *TransformationConfig
	Resp           http.ResponseWriter
	Result         *Image
	FinishedCond   *sync.Cond
	Finished       bool
	Err            error
}

type Image

type Image struct {
	// Id of the image mainly used for debugging purposes
	Id       string
	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 we make it possible to pass values through middlewares.
	//
	// Returns an image.
	Load(src string, ctx context.Context) (*Image, error)
}

Loader is responsible for loading an original image for transformation

type OpCallback

type OpCallback func()

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(input *TransformationConfig) (*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(input *TransformationConfig) (*Image, error)

	// Optimise optimises given image to reduce size of the served image.
	Optimise(input *TransformationConfig) (*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 Quality

type Quality int
const (
	DEFAULT Quality = 1 + iota
	LOW
)

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 ResizeConfig

type ResizeConfig struct {
	// Size is a size of output images in the format WxH.
	Size string
}

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
  • name: save-data required: false in: query type: string enum: ["off", hide] description: | Sets an optional behaviour when Save-Data header is "on". When passing "off" value the result image won't use additional compression when data saver mode is on. When passing "hide" value the result image will be an empty image.

responses:

'200':
  description: Resized image

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
  • name: save-data required: false in: query type: string enum: ["off", hide] description: | Sets an optional behaviour when Save-Data header is "on". When passing "off" value the result image won't use additional compression when data saver mode is on. When passing "hide" value the result image will be an empty 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.
  • name: save-data required: false in: query type: string enum: ["off", hide] description: | Sets an optional behaviour when Save-Data header is "on". When passing "off" value the result image won't use additional compression when data saver mode is on. When passing "hide" value the result image will be an empty image.

responses:

'200':
  description: Resized image.

type TransformationConfig

type TransformationConfig struct {
	// Src is a source image to transform.
	// This field is required for transformations.
	Src *Image
	// SupportedFormats is a list of output formats supported by client.
	// Processor will use one of those formats for result image. If list
	// is empty the format of the source image will be used.
	SupportedFormats []string
	// Quality defines quality of output image
	Quality Quality
	// Config is a configuration for the specific transformation
	Config interface{}
}

TransformationConfig is a configuration passed to Processor that used during transformations.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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