image

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: MIT Imports: 11 Imported by: 3

README

Image Processing Pipeline

Package image provides a Pluggable Image Processor. Multiple Processors can be chained together to form a Pipeline, which can be executed to arbitrarily process images. This can be used to, for example, resize and compress images in a web application that supports image uploads.

Usage

package example

import (
	stdimage "image"
	"github.com/modernice/media-tools/image"
)

func example(img stdimage.Image) {
	pipeline := image.Pipeline{
		// First, resize the image to 5 named dimensions.
		image.Resize(image.DimensionMap{
			"xs": {360},
			"sm": {640},
			"md": {960},
			"lg": {1280},
			"xl": {1920},
		}),

		// Then, compress the original and each resized image
		// to two qualities.
		image.Compress(compression.JPEG(80), compression.JPEG(50)),
	}

	result, err := pipeline.Run(context.TODO(), img)
	if err != nil {
		panic(err)
	}

	// 1 original + 1 original compressed
	// + 5 resized + 5 resized-compressed
	len(result.Images) == 12

	compressed := result.Find("compressed")
	len(compressed) == 6

	image.CompressionName(compressed[0].Tags) == "jpeg"
	image.CompressionQuality(compressed[0].Tags) == 80

	resized := result.Find("resized")
	len(resized) == 6

	image.DimensionName(resized[0].Tags) == "xs"
	image.DimensionName(resized[1].Tags) == "sm"
	image.DimensionName(resized[4].Tags) == "xl"

	sm := result.Find("size=lg")
	len(sm) == 1

	image.DimensionName(sm[0].Tags) == "sm"
}

Documentation

Index

Constants

View Source
const (
	// Compressed is the tag that is assigned to compressed images.
	Compressed = "compressed"

	// AnonymousCompression is the name [Compression.Name] of a CompressionFunc.
	AnonymousCompression = "anonymous"
)
View Source
const Original = "original"

Original is the tag that is assigned to the original image that is passed to Pipeline.Run.

View Source
const Resized = "resized"

Resized is the tag that is assigned to resized images.

Variables

This section is empty.

Functions

func CompressionName

func CompressionName(tags Tags) string

CompressionName extracts the name of the Compression from the tags of a processed image.

func CompressionQuality

func CompressionQuality(tags Tags) int

CompressionQuality extracts the name of the compression quality from the tags of a processed image. If the tags to not provide the compression quality, -1 is returned.

func DimensionName

func DimensionName(tags Tags) string

DimensionName extracts the dimension name from the tags of a processed image.

Types

type Compression

type Compression interface {
	// Compress compresses an image.
	Compress(img image.Image) (image.Image, error)
}

Compression provides the actual implementation for compressing images.

type CompressionFunc

type CompressionFunc func(image.Image) (image.Image, error)

CompressionFunc allow a function to be used as a Compression.

func (CompressionFunc) Compress

func (fn CompressionFunc) Compress(img image.Image) (image.Image, error)

type Compressor

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

Compressor compresses images.

func Compress

func Compress(compression Compression, opts ...CompressorOption) *Compressor

Compress returns a *Compressor that compresses images using the provided Compression. If the provided Compression has a `Tags() Tags` method, the returned *Compressor will append these tags to compressed images when calling *Compressor.Process.

func CompressMany

func CompressMany(compressions []Compression, opts ...CompressorOption) *Compressor

CompressMany returns a *Compressor that compresses images using the provided [Compression]s. If a provided Compression method has a `Tags() Tags` method, the returned *Compressor will append these tags to compressed image when calling *Compressor.Process.

func (*Compressor) Compress

func (c *Compressor) Compress(img image.Image) ([]image.Image, error)

Compress compresses an image using the configured [Compression]s.

func (*Compressor) Process

func (c *Compressor) Process(ctx ProcessorContext) ([]Processed, error)

Process implements Processor. By default, the original image will not be compressed and returned as is to preserve quality. To also compress the original image, pass the CompressOriginal option to Compress.

type CompressorOption

type CompressorOption func(*Compressor)

CompressorOption is an option for a *Compressor.

func CompressOriginal

func CompressOriginal(v bool) CompressorOption

CompressOriginal returns a CompressorOption that enables compression of the original image of a Pipeline. By default, the original image will not be compressed and instead returned as is to preserve the original image quality.

type DimensionList

type DimensionList []Dimensions

DimensionList is a list of Dimensions.

func (DimensionList) Dimensions

func (dl DimensionList) Dimensions() []Dimensions

Dimensions returns the dimension list.

type DimensionMap

type DimensionMap map[string]Dimensions

DimensionMap provides named Dimensions.

func (DimensionMap) Dimensions

func (dm DimensionMap) Dimensions() []Dimensions

Dimensions returns the dimension list.

func (DimensionMap) Tag

func (dm DimensionMap) Tag(dim Dimensions) string

Tag returns the configured tag for the given Dimensions.

type DimensionProvider

type DimensionProvider interface {
	Dimensions() []Dimensions
}

DimensionProvider provides image dimensions to Resizer. DimensionProvider is implemented by DimensionList and DimensionMap.

type Dimensions

type Dimensions [2]int

Dimensions are the width and height of an image, in pixels.

func (Dimensions) Height

func (d Dimensions) Height() int

Width returns the height of the image.

func (Dimensions) JSON added in v0.0.3

func (d Dimensions) JSON() JSONDimensions

func (Dimensions) MarshalJSON added in v0.0.2

func (d Dimensions) MarshalJSON() ([]byte, error)

func (Dimensions) String

func (d Dimensions) String() string

func (*Dimensions) UnmarshalJSON added in v0.0.3

func (d *Dimensions) UnmarshalJSON(data []byte) error

func (Dimensions) Width

func (d Dimensions) Width() int

Width returns the width of the image.

type JSONDimensions added in v0.0.3

type JSONDimensions struct {
	Width  int `json:"width"`
	Height int `json:"height"`
}

type Pipeline

type Pipeline []Processor

Pipeline is a list of Processors that are applied to an image.

func (Pipeline) Run

func (pipeline Pipeline) Run(ctx context.Context, img image.Image) (PipelineResult, error)

Run runs the pipeline on an image and returns the PipelineResult, containing the processed images.

type PipelineResult

type PipelineResult struct {
	// Images are the processed images.
	Images []Processed

	// Input is the original image that was passed to the [Pipeline].
	Input image.Image
}

PipelineResult is the result of running a Pipeline on an image.

func (PipelineResult) Find

func (result PipelineResult) Find(tags ...string) []Processed

Find returns the processed images that have at least 1 of the given tags. If no tags are provided, nil is returned.

func (PipelineResult) Match

func (result PipelineResult) Match(re *regexp.Regexp) []Processed

Match returns the processed images that have at least 1 tag that matches the given regular expression.

func (PipelineResult) Original

func (result PipelineResult) Original() (Processed, bool)

Original returns the processed image that is tagged as the original image. Depending on the Pipeline, the original image may have been transformed by one or more [Processor]s. [PipelineResult.Input] is the actual image that was passed to Pipeline.Run.

If the Pipeline discarded the original image from its result, false is returned.

type Processed

type Processed struct {
	Image    image.Image
	Tags     Tags
	Original bool
}

Processed is a processed image, except if the Original field is set to true. In that case, it is the original image that was passed to Pipeline.Run.

type Processor

type Processor interface {
	Process(ProcessorContext) ([]Processed, error)
}

A Processor processes an image and returns possibly multiple processed images.

type ProcessorContext

type ProcessorContext interface {
	context.Context

	// Image is the input image for the Processor.
	Image() Processed
}

ProcessorContext is passed to Processors.

func NewProcessorContext

func NewProcessorContext(ctx context.Context, img Processed) ProcessorContext

NewProcessorContext returns a new ProcessorContext for a Processor.

type ProcessorFunc

type ProcessorFunc func(ProcessorContext) ([]Processed, error)

ProcessorFunc allows functions to be used a Processors.

type Resizer

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

Resizer resizes images to a set of dimensions.

func Resize

func Resize(dimensions DimensionProvider, opts ...ResizerOption) *Resizer

Resize returns a Resizer that resizes images to the given dimensions.

func (*Resizer) Process

func (r *Resizer) Process(ctx ProcessorContext) ([]Processed, error)

Process implements Processor. The input image is returned in the result as the first element.

func (*Resizer) Resize

func (r *Resizer) Resize(img image.Image) ([]image.Image, error)

Resize resizes an image to the configured dimensinos. The input image is not returned in the result.

type ResizerOption

type ResizerOption func(*Resizer)

ResizerOption is an option for a Resizer.

func DiscardInput

func DiscardInput(v bool) ResizerOption

DiscardInput returns a ResizerOption that discards the input image from the resize result when executed in a Pipeline.

func ResampleFilter

func ResampleFilter(filter imaging.ResampleFilter) ResizerOption

ResampleFilter returns a ResizerOption that sets the imaging.ResampleFilter to use when resizing images. Defaults to imaging.Lanczos.

type Tagger

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

Tagger is a Processor that adds tags to images.

func Tag

func Tag(tags Tags) *Tagger

Tag returns a Tagger that adds the provided tags to images.

func TagBy

func TagBy(fn func(Processed) Tags) *Tagger

TagBy returns a Tagger that adds tags to images. For each image, the provided function is called to determine which tags to add to the image.

func (*Tagger) Process

func (tagger *Tagger) Process(ctx ProcessorContext) ([]Processed, error)

Process implements Processor. It adds the configured tags to the image.

type Tags

type Tags []string

Tags is a list of tags that Processors assigned to images in a Pipeline.

func NewTags

func NewTags(tags ...string) Tags

NewTags returns the given tags as Tags. Duplicates are removed.

func (Tags) Contains

func (tags Tags) Contains(tag string) bool

Contains returns whether a tag is contained within tags.

func (Tags) Match

func (tags Tags) Match(re *regexp.Regexp) []string

Match returns the tags that match the given regular expression.

func (Tags) With

func (tags Tags) With(add ...string) Tags

With appends additional tags and returns the new Tags. Duplicate tags are removed.

func (Tags) Without

func (tags Tags) Without(remove ...string) Tags

Without returns a copy of tags without the given tags.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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