imgdiet-go – git.sr.ht/~jamesponddotco/imgdiet-go Index | Files | Directories

package imgdiet

import "git.sr.ht/~jamesponddotco/imgdiet-go"

Package imgdiet offers a simple and fast image processing and compression solution by leveraging C's [libvips] image processing library and its Go binding, [govips].

[libvips]: https://github.com/libvips/libvips [govips]: https://github.com/davidbyttow/govips

Index

Constants

const (
	ErrOpenImage               xerrors.Error = "failed to open image"
	ErrNilImage                xerrors.Error = "image is nil"
	ErrInvalidResizeDimensions xerrors.Error = "dimensions must be greater than 0"
)
const (
	ImageTypeJPEG string = "JPEG"
	ImageTypePNG  string = "PNG"
	ImageTypeGIF  string = "GIF"
)

List of image types supported by this package.

const (
	LogLevelError    = vips.LogLevelError
	LogLevelCritical = vips.LogLevelCritical
	LogLevelWarning  = vips.LogLevelWarning
	LogLevelMessage  = vips.LogLevelMessage
	LogLevelInfo     = vips.LogLevelInfo
	LogLevelDebug    = vips.LogLevelDebug
)

List of libvips logging levels for convenience.

const ErrUnsupportedImageFormat xerrors.Error = "unsupported image format"

ErrUnsupportedImageFormat is returned when the image format is not supported by this package.

Functions

func DefaultLogger

func DefaultLogger(_ string, verbosity vips.LogLevel, message string)

DefaultLogger is a very simple logger the package uses by default. It complies with vips.LoggingHandlerFunction.

func DetectImageSize

func DetectImageSize(image []byte) int64

DetectImageSize takes an image as a byte array input and detects the image size in bytes.

func DetectImageType

func DetectImageType(image []byte) (string, error)

DetectImageType takes an image as a byte array input and detects its type based on its magic bytes. It returns a string representation of the image type and an error if the image type is not supported.

func Start

func Start(cfg *Config)

Start initializes the libvips library with the given configuration.

func Stop

func Stop()

Stop shuts down the libvips library.

Types

type Config

type Config struct {
	// Logger defines the logger to be used by libvips.
	Logger func(domain string, verbosity vips.LogLevel, message string)

	// LogLevel is the level of logging to be used by libvips.
	LogLevel vips.LogLevel

	// Cache defines the size of the libvips cache in bytes.
	Cache int

	// MaxConcurrency defines the maximum number of concurrent operations that
	// libvips can perform.
	MaxConcurrency int

	// ReportLeaks defines whether libvips should report memory leaks.
	ReportLeaks bool
}

Config defines the configuration for the libvips library.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a set of opinionated and sane defaults for the libvips library.

type Image

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

Image defines an image to be optimized and manages its lifecycle.

func Open

func Open(r io.Reader) (*Image, error)

Open takes an io.Reader as input for reading and returns an Image instance.

func (*Image) Close

func (i *Image) Close()

Close releases the resources associated with the Image.

func (*Image) Height

func (i *Image) Height() int

Height returns the height of the image in pixels.

func (*Image) Optimize

func (i *Image) Optimize(opts *Options) ([]byte, error)

Optimize takes the given Options and optimizes the image accordingly. It returns the optimized image as a byte slice or an error if the optimization fails.

func (*Image) Resize

func (i *Image) Resize(width, height int, opts *Options) ([]byte, error)

Resize takes a set of dimensions and resizes the image to those dimensions. If opts is not nil, the resulting image is optimized according to the given Options.

func (*Image) Saved

func (i *Image) Saved() int64

Saved returns the size of the image after optimization in bytes.

func (*Image) Size

func (i *Image) Size() int64

Size returns the size of the image in bytes.

func (*Image) Width

func (i *Image) Width() int

Width returns the width of the image in pixels.

type Options

type Options struct {
	// Quality defines the quality of the output image. It is a number between 0
	// and 100.
	Quality int

	// Compression defines the compression level of the output image. It is a
	// number between 0 and 9.
	//
	// Only valid for PNG images.
	Compression int

	// Effort defines the level of CPU effort to be used when optimizing the
	// output image. It is a number between 0 and 9.
	//
	// Only valid for GIF images.
	Effort int

	// QuantTable defines the quantization table to be used for the output
	// image. It is a number between 0 and 8.
	//
	// Only valid for JPEG images.
	QuantTable int

	// Bitdepth defines the number of bits per pixel of the output image. It is
	// a number between 1 and 8.
	//
	// Only valid for GIF and PNG images.
	Bitdepth int

	// Dither defines the amount of dithering to be applied during 8bpp (bits
	// per pixel) quantization. It is a floating-point number between 0 and 1.
	//
	// Only valid for GIF and PNG images.
	Dither float64

	// OptimizeCoding defines whether the output image should have its coding
	// optimized.
	//
	// Only valid for JPEG images.
	OptimizeCoding bool

	// Interlaced defines whether the output image should be interlaced.
	Interlaced bool

	// StripMetadata defines whether the output image should have its metadata
	// stripped.
	StripMetadata bool

	// OptimizeICCProfile defines whether the output image should have its ICC
	// profile optimized.
	OptimizeICCProfile bool

	// TrellisQuant defines whether the output image should have its
	// quantization tables optimized using trellis quantization.
	//
	// Only valid for JPEG images.
	TrellisQuant bool

	// OvershootDeringing defines whether the output image should have its
	// quantization tables optimized using overshoot deringing.
	//
	// Only valid for JPEG images.
	OvershootDeringing bool

	// OptimizeScans defines whether the output image should have its scans
	// optimized.
	//
	// Only valid for JPEG images.
	OptimizeScans bool
}

Options represents the parameters used to optimize an image.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns a set of opinionated defaults for optimizing images.

Source Files

config.go image.go imgdiet.go log.go

Directories

PathSynopsis
cmd
cmd/imgdiet
cmd/imgdiet/internal
Version
v0.2.0 (latest)
Published
Oct 22, 2024
Platform
linux/amd64
Imports
9 packages
Last checked
2 days ago

Tools for package owners.