thumbnailer

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2025 License: MIT Imports: 8 Imported by: 0

README

Go Reference

Thumbnailer

Thumbnailer is a simple utility library and CLI for generating image thumbnails, written in Golang.

It's heavily inspired by prplecake/go-thumbnail, with some additional functionality and adjusted ergonomics.

Usage

As a library

To install Thumbnailer as a library, go get it:

go get github.com/jordanfitz/thumbnailer

Then, you might make use of the library like this:

package main

import "github.com/jordanfitz/thumbnailer"

func main() {
    inputData, _ := os.ReadFile("input-image.jpg")

    t := thumbnailer.New().
        With(thumbnailer.OutFormat(thumbnailer.JPG)). // output a JPG
        With(thumbnailer.Quality(50)).                // encode the JPG with quality 50
        With(thumbnailer.MaxSize(200)).               // don't let the thumbnail exceed 200px, width or height
        With(thumbnailer.Image(inputData))            // use inputData as the source image

    outputData, _ := t.Create()

    // do something with outputData
    // ...
}
As a command-line utility

To install the CLI, run go install:

go install github.com/jordanfitz/thumbnailer/cmd/thumbnailer@latest

Then, use the utility like this:

# generate thumbnails for all PNG files in my-images/, outputting
# JPG files with a quality of 50 into the directory my-images/thumbs/
thumbnailer my-images/*.png -o jpg -q 50 -o my-images/thumbs

More information can be gleaned from thumbnailer -h.

Documentation

Overview

Package thumbnailer contains a utility with which thumbnails can be generated from an image.

Index

Constants

View Source
const (
	DefaultMaxSize = 300
)

Variables

View Source
var (
	ErrInvalidImage = errors.New("invalid image")
)

Functions

This section is empty.

Types

type Option

type Option func(t *Thumbnailer)

func Image

func Image(value []byte) Option

Image sets the JPG or PNG image data from which thumbnails can be generated.

func MaxSize

func MaxSize(value int) Option

MaxSize sets a size which the scaled image's largest dimension will not exceed.

func OutFormat

func OutFormat(value OutputFormat) Option

OutFormat sets the output image format used by Create. By default, the format of the original image is used.

func Quality

func Quality(value int) Option

Quality sets the JPG quality used by Create. It has no effect if the output format is not JPG. By default, jpeg.DefaultQuality is used.

func Scaler

func Scaler(value draw.Scaler) Option

Scaler sets the draw.Scaler used by Create. By default, the draw.ApproxBiLinear scaler is used.

type OutputFormat

type OutputFormat uint8
const (
	OriginalFormat OutputFormat = iota
	JPG
	PNG
)

type Thumbnailer

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

func New

func New(options ...Option) Thumbnailer

New creates a new instance of Thumbnailer with which thumbnails can be generated.

func (Thumbnailer) Create

func (t Thumbnailer) Create() ([]byte, error)

Create generates a thumbnail, returning the encoded thumbnail image or an error.

func (Thumbnailer) With

func (t Thumbnailer) With(o Option) Thumbnailer

With applies an option to the Thumbnailer instance.

Directories

Path Synopsis
cmd
thumbnailer command

Jump to

Keyboard shortcuts

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