Help us shape the future of Go! Take the 2026 Go Developer Survey

optimize

package
v1.9.5 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLadder = []int{95, 90, 85, 80, 75, 65, 55, 40}
View Source
var MaxPixels = 50_000_000

MaxPixels bounds how large an image may be before it is even decoded. A small file can declare enormous dimensions — a few KB of PNG can claim 100000x100000 and decode to hundreds of gigabytes of RGBA — so the header is checked before any pixels are allocated. 50 megapixels clears any real camera; raise it if you genuinely need to.

Functions

func ClampQuality

func ClampQuality(q int) int

func ColorsForQuality

func ColorsForQuality(quality int) int

func Encode

func Encode(img image.Image, format Format, quality int) ([]byte, error)

func HumanBytes

func HumanBytes(n int) string

func ParseBytes

func ParseBytes(s string) (int, error)

func ParseScale

func ParseScale(s string) (float64, error)

func Quantize

func Quantize(img image.Image, n int) color.Palette

func Resample

func Resample(src image.Image, w, h int) image.Image

func Saving

func Saving(before, after int) float64

func TargetDimensions

func TargetDimensions(w, h int, scale float64, maxW, maxH int) (int, int)

Types

type Format

type Format int
const (
	FormatUnknown Format = iota
	FormatJPEG
	FormatPNG
	FormatGIF
	FormatWebP
	FormatTIFF
	FormatBMP
)

func EncodableFormats

func EncodableFormats() []Format

func ParseFormat

func ParseFormat(s string) (Format, bool)

func ResolveOutputFormat

func ResolveOutputFormat(explicit, src Format) Format

func (Format) CanEncode

func (f Format) CanEncode() bool

func (Format) Ext

func (f Format) Ext() string

func (Format) Lossy

func (f Format) Lossy() bool

func (Format) MIME

func (f Format) MIME() string

func (Format) String

func (f Format) String() string

type Options

type Options struct {
	Quality   int
	Scale     float64
	MaxWidth  int
	MaxHeight int
	Format    Format
}

type Result

type Result struct {
	Data    []byte
	Format  Format
	Quality int
	Width   int
	Height  int
	Bytes   int
}

func FitTo

func FitTo(src Source, opts Options, budget int) (Result, error)

func Optimize

func Optimize(src Source, opts Options) (Result, error)

type Source

type Source struct {
	Name   string
	Image  image.Image
	Format Format
	Width  int
	Height int
	Bytes  int
}

func Decode

func Decode(name string, data []byte) (Source, error)

type Step

type Step struct {
	Quality int `json:"quality"`
	Bytes   int `json:"bytes"`
}

func Ladder

func Ladder(src Source, opts Options, qualities []int) ([]Step, error)

Jump to

Keyboard shortcuts

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