thumbnail

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2025 License: BSD-3-Clause Imports: 9 Imported by: 0

README

Thumbnail Generation Package for Go

This package provides method to create thumbnails from provided images.

Installation

Use to go command:

go get github.com/adderly/go-thumbnail

Example

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

Developing

Build:

go build ./...

Run tests:

go test -v ./...

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: "",
	Prefix:          "thumb_",
	//Scaler:            "CatmullRom",
}

imagePath := "path/to/image.jpg"
//dest := "path/to/thumb_image.jpg"
gen := NewGenerator(config, []ImageDimension{
	defaultImageOutput,
})

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

thumbBytes, err := gen.GetProcessedImage(i, defaultImageOutput)
if err != nil {
	//t.Error(err)
	panic(err)
}

img := i
img.ImageData = thumbBytes

_, err = gen.Save(img)

if err != nil {
	panic(err)
}

//
//thumbBytes, err := gen.CreateThumbnail(i)
//if err != nil {
//	panic(err)
//}
//
//err = os.WriteFile(dest, thumbBytes, 0644)
//if err != nil {
//	panic(err)
//}

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")
	ErrInvalidImageData           = errors.New("invalid image data ")
	ErrInvalidNoTransformProvided = errors.New("no transform data was provided ")

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

	// DefaultThumbnailPercentage the default value to use on percentage resizing
	DefaultThumbnailPercentage = 0.4

	// DefaultThumbnailSize the default dimensions used for resizing
	DefaultThumbnailSize = ImageSize{Width: 220, Height: 220}
)

Functions

func CreateThumbnail added in v0.1.1

func CreateThumbnail(i *Image, dimension ImageDimension) (img image.Image, err error)

CreateThumbnail generates a thumbnail.

Types

type GenerationResult

type GenerationResult struct {
	// Filename the name of the file
	Filename string
	// Path the path of the file in the file system
	Path string
	//Error the error reported by the process of the generation
	Error error
}

func SaveRaw added in v0.1.1

func SaveRaw(i image.Image, path string, format imgconv.FormatOption) (result GenerationResult, err error)

SaveRaw generates a thumbnail.

type Generator

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

	// Height is the destination thumbnail height.
	Height int

	// The preferred format for exporting the thumbnails
	PreferredFormat imgconv.FormatOption

	//Name is the game it will output ass
	Name string

	// DestinationPath is the destination thumbnail path.
	DestinationPath string

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

	// OutputFormats the formats (dimensions), that the image will be exported to.
	OutputFormats []ImageDimension
}

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

func New added in v0.1.1

func New(c Generator) *Generator

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

func NewGenerator

func NewGenerator(c Generator, outputFormats []ImageDimension) *Generator

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

func (*Generator) Generate

func (gen *Generator) Generate(i *Image) ([]GenerationResult, error)

Generate generates all the images for the specified file with the dimensions on the generator

func (*Generator) GetGeneratorDimension added in v0.1.1

func (gen *Generator) GetGeneratorDimension() ImageDimension

GetGeneratorDimension return a dimension object based on the values inside the generator.

func (*Generator) GetProcessedImage

func (gen *Generator) GetProcessedImage(i *Image, dimension ImageDimension) (img image.Image, err error)

GetProcessedImage get the processed image from resize.

func (*Generator) NewImageFromByteArray

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

NewImageFromByteArray 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.

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.

func (*Generator) NewImageFromFilewWithDefault added in v0.1.1

func (gen *Generator) NewImageFromFilewWithDefault(path string, defaultImg 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.

func (*Generator) Save

func (gen *Generator) Save(i *Image) (result GenerationResult, err error)

Save save the image

func (*Generator) SaveWithDimension

func (gen *Generator) SaveWithDimension(i *Image, imgConf *ImageDimension) (result GenerationResult, err error)

SaveWithDimension generates a thumbnail.

type Image

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

	// Data is the image data in a byte-array
	ImageData image.Image

	// Current stores the existing image's dimensions
	Size ImageSize

	// Future store the new thumbnail dimensions.
	//TODO: compatibility reasons
	TargetDimension ImageSize
}

An Image is an image and information about it.

func ImageFromFile added in v0.1.1

func ImageFromFile(path string) (*Image, error)

type ImageDimension

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

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

	// Percentage
	Percentage float64

	//For selecting the images there is need for the selection of the names.
	// Prefix > Name > Default [ the order of the selection of the namings]
	//Prefix
	Prefix string

	//Name
	Name string

	//Name
	DestinationOverride string
}

ImageDimension stores dimensional information for an Image.

type ImageSize

type ImageSize struct {
	Width  int
	Height int
}

Jump to

Keyboard shortcuts

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