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

imageopt

package
v1.1.9 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package imageopt turns arbitrary uploaded photos into small, good-looking WebP files. It is shared by the upload handlers (api) and the one-off backfill command (cmd/optimize_images) so both produce identical output.

Index

Constants

View Source
const (
	// MaxDim caps the long edge. 1920px keeps small print on a receipt
	// legible while shrinking a 12MP phone photo to a fraction of its size.
	MaxDim = 1920
	// Quality is libwebp's lossy 0-100 scale; ~75 is the "good, not
	// pristine" knee -- visually close to the source at roughly a quarter of
	// the JPEG bytes.
	Quality = 75

	ContentType = "image/webp"
	Ext         = ".webp"
)

Variables

This section is empty.

Functions

func Decode

func Decode(data []byte, maxDim int) (img image.Image, resized bool, err error)

Decode returns the decoded, orientation-corrected image, downscaled if its long edge exceeds maxDim; resized reports whether that downscale happened.

func EncodeWebP

func EncodeWebP(img image.Image, quality int) ([]byte, error)

EncodeWebP encodes img as lossy WebP, holding one of the bounded encode slots for the duration.

Types

type Result

type Result struct {
	Data        []byte
	ContentType string
	Ext         string
	// Optimized is false when the original bytes were kept unchanged.
	Optimized bool
}

Result is what a caller persists: the bytes to write and the metadata that must describe *those* bytes (not the original upload).

func Optimize

func Optimize(original []byte, declaredContentType, declaredExt string) Result

Optimize decodes the photo (honouring EXIF orientation, which a re-encode would otherwise drop and leave phone photos sideways), downsizes it to MaxDim, and re-encodes it as lossy WebP.

It never makes an image worse: if the result isn't smaller than the original, or the format can't be decoded here (e.g. HEIC), the original bytes come back unchanged under their declared type.

Jump to

Keyboard shortcuts

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