compressor

package
v0.0.0-...-cfc82a8 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateReduction

func CalculateReduction(original, compressed int64) float64

CalculateReduction calculates the percentage reduction

func FormatBytes

func FormatBytes(bytes int64) string

FormatBytes formats bytes into human-readable string

func GenerateOutputPath

func GenerateOutputPath(inputPath string, options CompressionOptions) string

GenerateOutputPath creates the output path based on options

Types

type CompressionOptions

type CompressionOptions struct {
	// Common options
	Quality       int     // 1-100 for JPEG, ignored for PNG
	ResizePercent float64 // 0-100, 0 means no resize
	ResizeWidth   int     // Target width, 0 means auto
	ResizeHeight  int     // Target height, 0 means auto
	StripMetadata bool    // Remove EXIF and other metadata
	OutputDir     string  // Output directory, empty means same as input
	OutputSuffix  string  // Suffix to add to filename (e.g., "_compressed")

	// JPEG specific
	Progressive     bool   // Progressive JPEG encoding
	ChromaSubsample string // "4:4:4", "4:2:2", "4:2:0"

	// PNG specific
	CompressionLevel int  // 0-9, higher = more compression
	Interlaced       bool // Adam7 interlacing
}

CompressionOptions holds all compression settings

func DefaultOptions

func DefaultOptions() CompressionOptions

DefaultOptions returns sensible default compression options

type CompressionResult

type CompressionResult struct {
	InputPath  string
	OutputPath string
	InputSize  int64
	OutputSize int64
	Reduction  float64 // Percentage reduction
	Width      int
	Height     int
	Success    bool
	Error      error
}

CompressionResult contains the result of a compression operation

type Compressor

type Compressor interface {
	Compress(inputPath string, options CompressionOptions) (*CompressionResult, error)
	GetInfo(inputPath string) (*ImageInfo, error)
	EstimateSize(inputPath string, options CompressionOptions) (int64, error)
}

Compressor interface defines the compression operations

type ImageFormat

type ImageFormat string

ImageFormat represents supported image formats

const (
	FormatJPEG ImageFormat = "jpeg"
	FormatPNG  ImageFormat = "png"
)

func GetImageFormat

func GetImageFormat(path string) (ImageFormat, error)

GetImageFormat detects the image format from file extension

type ImageInfo

type ImageInfo struct {
	Path      string
	Format    ImageFormat
	Width     int
	Height    int
	Size      int64
	ColorMode string
}

ImageInfo contains information about an image

func GetImageInfo

func GetImageInfo(path string) (*ImageInfo, error)

GetImageInfo returns information about an image file

type JPEGCompressor

type JPEGCompressor struct{}

JPEGCompressor handles JPEG image compression

func NewJPEGCompressor

func NewJPEGCompressor() *JPEGCompressor

NewJPEGCompressor creates a new JPEG compressor

func (*JPEGCompressor) Compress

func (c *JPEGCompressor) Compress(inputPath string, options CompressionOptions) (*CompressionResult, error)

Compress compresses a JPEG image with the given options

func (*JPEGCompressor) EstimateSize

func (c *JPEGCompressor) EstimateSize(inputPath string, options CompressionOptions) (int64, error)

EstimateSize estimates the compressed size based on options

func (*JPEGCompressor) GetInfo

func (c *JPEGCompressor) GetInfo(inputPath string) (*ImageInfo, error)

GetInfo returns information about a JPEG image

type PNGCompressor

type PNGCompressor struct{}

PNGCompressor handles PNG image compression

func NewPNGCompressor

func NewPNGCompressor() *PNGCompressor

NewPNGCompressor creates a new PNG compressor

func (*PNGCompressor) Compress

func (c *PNGCompressor) Compress(inputPath string, options CompressionOptions) (*CompressionResult, error)

Compress compresses a PNG image with the given options

func (*PNGCompressor) EstimateSize

func (c *PNGCompressor) EstimateSize(inputPath string, options CompressionOptions) (int64, error)

EstimateSize estimates the compressed size based on options

func (*PNGCompressor) GetInfo

func (c *PNGCompressor) GetInfo(inputPath string) (*ImageInfo, error)

GetInfo returns information about a PNG image

Jump to

Keyboard shortcuts

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