image

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknownFormat is returned when trying to encode an image whose format
	// is not registered in an Encoder.
	ErrUnknownFormat = errors.New("unknown format")
)

Functions

func GIFEncoder

func GIFEncoder(w io.Writer, img image.Image) error

GIFEncoder encodes images using gif.Encode with default options.

func JPEGEncoder

func JPEGEncoder(w io.Writer, img image.Image) error

JPEGEncoder encodes images using jpeg.Encode with maximum quality.

func PNGEncoder

func PNGEncoder(w io.Writer, img image.Image) error

PNGEncoder encodes images using a png.Encoder with png.BestCompression as the compression level.

Types

type Dimensions

type Dimensions struct {
	Width  int
	Height int
}

Dimensions are image dimensions (width & height).

func (Dimensions) Resize

func (dim Dimensions) Resize(img image.Image) *image.NRGBA

Resize resizes an Image into the Dimensions.

type Encoder

type Encoder interface {
	// Encode encodes an image using the appropriate FormatEncoder for the
	// specified format.
	Encode(io.Writer, image.Image, string) error
}

Encoder is a multi-format image encoder.

func NewEncoder

func NewEncoder(opts ...EncoderOption) Encoder

NewEncoder returns a new Encoder with default support for JPEGs, GIFs and PNGs. When provided an unknown image format, Encoder falls back to the PNG encoder.

type EncoderOption

type EncoderOption func(*encoder)

EncoderOption is an Encoder option.

func WithFormat

func WithFormat(format string, enc FormatEncoder) EncoderOption

WithFormat returns an EncoderOption that registers a FormatEncoder for the given image format.

Format must be the same as would be returned by image.Decode. Provide an empty string as format to set the default encoder for unknown formats.

type FormatEncoder

type FormatEncoder interface {
	// Encode encodes the provided Image and writes the result into the
	// specified Writer.
	Encode(io.Writer, image.Image) error
}

FormatEncoder encodes images of a specific format (JPEG, PNG etc.).

type Func

type Func func(io.Writer, image.Image) error

Func allows functions to be used as FormatEncoders.

func (Func) Encode

func (fn Func) Encode(w io.Writer, img image.Image) error

Encode returns fn(w, img).

type PNGCompressor

type PNGCompressor png.CompressionLevel

PNGCompressor compresses images using a png.Encoder.

var img image.Image
c := PNGCompressor(png.BestCompression)
compressed := c.Compress(img)

func (PNGCompressor) Compress

func (comp PNGCompressor) Compress(img image.Image) (image.Image, error)

Compress compresses the provided Image and returns the compressed Image. Compressor uses a png.Encoder to compress the Image with comp.CompressionLevel() as the compression level.

func (PNGCompressor) CompressionLevel

func (comp PNGCompressor) CompressionLevel() png.CompressionLevel

CompressionLevel returns the png.CompressionLevel.

type Resizer

type Resizer map[string]Dimensions

A Resizer resizes images into different Dimensions. Resizing is done in parallel for each Dimensions.

func (*Resizer) Resize

func (r *Resizer) Resize(img image.Image) map[string]image.Image

Resize resizes an Image into the configured Dimensions. Providing 0 as the Width or Height for Dimensions of a size preserves the aspect-ratio for that size.

var img image.Image

r := Resizer{
	"small": {Width: 640, Height: 0},
	"medium": {Width: 1280, Height: 0},
	"large": {Width: 1920, Height: 0},
}

resized := r.Resize(img)
// resized["small"].Bounds().Dx() == 640
// resized["medium"].Bounds().Dx() == 1280
// resized["large"].Bounds().Dx() == 1920

Directories

Path Synopsis
mock_gallery
Package mock_gallery is a generated GoMock package.
Package mock_gallery is a generated GoMock package.
Package mock_image is a generated GoMock package.
Package mock_image is a generated GoMock package.

Jump to

Keyboard shortcuts

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