thumbnail

package
v1.1.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SupportedMimeTypes contains an all mimetypes which are supported by the thumbnailer.
	SupportedMimeTypes = map[string]struct{}{
		"image/png":                         {},
		"image/jpg":                         {},
		"image/jpeg":                        {},
		"image/gif":                         {},
		"image/bmp":                         {},
		"image/x-ms-bmp":                    {},
		"image/tiff":                        {},
		"text/plain":                        {},
		"audio/flac":                        {},
		"audio/mpeg":                        {},
		"audio/ogg":                         {},
		"application/vnd.geogebra.slides":   {},
		"application/vnd.geogebra.pinboard": {},
	}
)

Functions

func GetExtForMime

func GetExtForMime(fileType string) string

GetExtForMime return the supported extension by mime

func IsMimeTypeSupported

func IsMimeTypeSupported(m string) bool

IsMimeTypeSupported validate if the mime type is supported

func ParseResolution

func ParseResolution(s string) (image.Rectangle, error)

ParseResolution returns an image.Rectangle representing the resolution given as a string

Types

type DefinableProcessor

type DefinableProcessor struct {
	Slug      string
	Converter func(img image.Image, width, height int, filter imaging.ResampleFilter) *image.NRGBA
}

DefinableProcessor is the simplest processor, it holds a replaceable image converter function.

func ProcessorFor

func ProcessorFor(id, fileType string) (DefinableProcessor, error)

ProcessorFor returns a matching Processor

func (DefinableProcessor) ID

func (p DefinableProcessor) ID() string

ID returns the processor identification.

func (DefinableProcessor) Process

func (p DefinableProcessor) Process(img image.Image, width, height int, filter imaging.ResampleFilter) *image.NRGBA

Process transforms the given image.

type Encoder

type Encoder interface {
	// Encode encodes the image to a format.
	Encode(w io.Writer, img interface{}) error
	// Types returns the formats suffixes.
	Types() []string
	// MimeType returns the mimetype used by the encoder.
	MimeType() string
}

Encoder encodes the thumbnail to a specific format.

func EncoderForType

func EncoderForType(fileType string) (Encoder, error)

EncoderForType returns the encoder for a given file type or nil if the type is not supported.

type Generator

type Generator interface {
	Generate(size image.Rectangle, img interface{}) (interface{}, error)
	Dimensions(img interface{}) (image.Rectangle, error)
	ProcessorID() string
}

Generator generates a web friendly file version.

func GeneratorFor

func GeneratorFor(fileType, processorID string) (Generator, error)

GeneratorFor returns the generator for a given file type or nil if the type is not supported.

type GifEncoder

type GifEncoder struct{}

GifEncoder encodes to gif

func (GifEncoder) Encode

func (e GifEncoder) Encode(w io.Writer, img interface{}) error

Encode encodes the image to a gif format

func (GifEncoder) MimeType

func (e GifEncoder) MimeType() string

MimeType returns the mimetype used by the encoder.

func (GifEncoder) Types

func (e GifEncoder) Types() []string

Types returns the supported types of the GifEncoder

type GifGenerator

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

GifGenerator is used to create a web friendly version of the provided gif image.

func NewGifGenerator

func NewGifGenerator(filetype, process string) (GifGenerator, error)

func (GifGenerator) Dimensions

func (g GifGenerator) Dimensions(img interface{}) (image.Rectangle, error)

func (GifGenerator) Generate

func (g GifGenerator) Generate(size image.Rectangle, img interface{}) (interface{}, error)

Generate generates a alternative gif version.

func (GifGenerator) ProcessorID

func (g GifGenerator) ProcessorID() string

ProcessorID returns the processor identification.

type JpegEncoder

type JpegEncoder struct{}

JpegEncoder encodes to jpg

func (JpegEncoder) Encode

func (e JpegEncoder) Encode(w io.Writer, img interface{}) error

Encode encodes to jpg

func (JpegEncoder) MimeType

func (e JpegEncoder) MimeType() string

MimeType returns the mimetype for jpg files.

func (JpegEncoder) Types

func (e JpegEncoder) Types() []string

Types returns the jpg suffixes.

type Manager

type Manager interface {
	// Generate creates a thumbnail and stores it.
	// The function returns a key with which the actual file can be retrieved.
	Generate(r Request, img interface{}) (string, error)
	// CheckThumbnail checks if a thumbnail with the requested attributes exists.
	// The function will return a status if the file exists and the key to the file.
	CheckThumbnail(r Request) (string, bool)
	// GetThumbnail will load the thumbnail from the storage and return its content.
	GetThumbnail(key string) ([]byte, error)
}

Manager is responsible for generating thumbnails

type PngEncoder

type PngEncoder struct{}

PngEncoder encodes to png

func (PngEncoder) Encode

func (e PngEncoder) Encode(w io.Writer, img interface{}) error

Encode encodes to png format

func (PngEncoder) MimeType

func (e PngEncoder) MimeType() string

MimeType returns the mimetype for png files.

func (PngEncoder) Types

func (e PngEncoder) Types() []string

Types returns the png suffix

type Processor

type Processor interface {
	ID() string
	Process(img image.Image, width, height int, filter imaging.ResampleFilter) *image.NRGBA
}

Processor processes the thumbnail by applying different transformations to it.

type Request

type Request struct {
	Resolution image.Rectangle
	Encoder    Encoder
	Generator  Generator
	Checksum   string
}

Request bundles information needed to generate a thumbnail for a file

func PrepareRequest

func PrepareRequest(width, height int, tType, checksum, pID string) (Request, error)

PrepareRequest prepare the request based on image parameters

type Resolutions

type Resolutions []image.Rectangle

Resolutions is a list of image.Rectangle representing resolutions.

func ParseResolutions

func ParseResolutions(strs []string) (Resolutions, error)

ParseResolutions creates an instance of Resolutions from resolution strings.

func (Resolutions) ClosestMatch

func (rs Resolutions) ClosestMatch(requested image.Rectangle, sourceSize image.Rectangle) image.Rectangle

ClosestMatch returns the resolution which is closest to the provided resolution. If there is no exact match the resolution will be the next higher one. If the given resolution is bigger than all available resolutions the biggest available one is used.

type SimpleGenerator

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

SimpleGenerator is the default image generator and is used for all image types expect gif.

func NewSimpleGenerator

func NewSimpleGenerator(filetype, process string) (SimpleGenerator, error)

func (SimpleGenerator) Dimensions

func (g SimpleGenerator) Dimensions(img interface{}) (image.Rectangle, error)

func (SimpleGenerator) Generate

func (g SimpleGenerator) Generate(size image.Rectangle, img interface{}) (interface{}, error)

Generate generates a alternative image version.

func (SimpleGenerator) ProcessorID

func (g SimpleGenerator) ProcessorID() string

ProcessorID returns the processor identification.

type SimpleManager

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

SimpleManager is a simple implementation of Manager

func NewSimpleManager

func NewSimpleManager(resolutions Resolutions, storage storage.Storage, logger log.Logger, maxInputWidth, maxInputHeight int) SimpleManager

NewSimpleManager creates a new instance of SimpleManager

func (SimpleManager) CheckThumbnail

func (s SimpleManager) CheckThumbnail(r Request) (string, bool)

CheckThumbnail checks if a thumbnail with the requested attributes exists.

func (SimpleManager) Generate

func (s SimpleManager) Generate(r Request, img interface{}) (string, error)

Generate creates a thumbnail and stores it

func (SimpleManager) GetThumbnail

func (s SimpleManager) GetThumbnail(key string) ([]byte, error)

GetThumbnail will load the thumbnail from the storage and return its content.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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