thumbnail

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2022 License: BSD-3-Clause Imports: 8 Imported by: 2

README

godocs.io Go

Thumbnail Generation Package for Go

This package provides method to create thumbnails from provided images.

Installation

Use to go command:

go get github.com/prplecake/go-thumbnail

Example

See thumbnail_test.go for an example implementation at this time.

Resources

GoDocs can be found at godocs.io.

Documentation

Overview

Package thumbnail provides a method to create thumbnails from images.

Example
var config = Generator{
	DestinationPath:   "",
	DestinationPrefix: "thumb_",
	Scaler:            "CatmullRom",
}

imagePath := "path/to/image.jpg"
dest := "path/to/thumb_image.jpg"
gen := NewGenerator(config)

i, err := gen.NewImageFromFile(imagePath)
if err != nil {
	panic(err)
}

thumbBytes, err := gen.CreateThumbnail(i)
if err != nil {
	panic(err)
}

err = ioutil.WriteFile(dest, thumbBytes, 0644)
if err != nil {
	panic(err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidMimeType is returned when a non-image content type is
	// detected.
	ErrInvalidMimeType = errors.New("invalid mimetype")

	// ErrInvalidScaler is returned when an unrecognized scaler is
	// passed to the Generator.
	ErrInvalidScaler = errors.New("invalid scaler")
)

Functions

This section is empty.

Types

type Dimensions

type Dimensions struct {
	// Width is the width of an image in pixels.
	Width int

	// Height is the height on an image in pixels.
	Height int

	// X is the right-most X-coordinate.
	X int

	// Y is the top-most Y-coordinate.
	Y int
}

Dimensions stores dimensional information for an Image.

type Generator

type Generator struct {
	// Width is the destination thumbnail width.
	Width int

	// Height is the destination thumbnail height.
	Height int

	// DestinationPath is the destination thumbnail path.
	DestinationPath string

	// DestinationPrefix is the prefix for the destination thumbnail
	// filename.
	DestinationPrefix string

	// Scaler is the scaler to be used when generating thumbnails.
	Scaler string
}

Generator registers a generator configuration to be used when creating thumbnails.

func NewGenerator

func NewGenerator(c Generator) *Generator

NewGenerator returns an instance of a thumbnail generator with a given configuration.

func (*Generator) CreateThumbnail

func (gen *Generator) CreateThumbnail(i *Image) ([]byte, error)

CreateThumbnail generates a thumbnail.

func (*Generator) NewImageFromByteArray

func (gen *Generator) NewImageFromByteArray(imageBytes []byte) (*Image, error)

NewImageFromByteArray reads in an image from a byte array and populates an Image object. That new Image object is returned along with any errors that occur during the operation.

func (*Generator) NewImageFromFile

func (gen *Generator) NewImageFromFile(path string) (*Image, error)

NewImageFromFile reads in an image file from the file system and populates an Image object. That new Image object is returned along with any errors that occur during the operation.

type Image

type Image struct {
	// Path is a path to an image.
	Path string

	// ContentType is the content type of the image.
	ContentType string

	// Data is the image data in a byte-array
	Data []byte

	// Size is the length of Data
	Size int

	// Current stores the existing image's dimensions
	Current Dimensions

	// Future store the new thumbnail dimensions.
	Future Dimensions
}

An Image is an image and information about it.

Jump to

Keyboard shortcuts

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