imageprep

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

imageprep

Tests Go Reference

Prepare untrusted images with bounded decoding, EXIF orientation correction, resizing, square cropping, and JPEG or PNG output.

The Go implementation is the leading implementation.

Use

go get github.com/wikilayer/imageprep@v0.1.0
result, err := imageprep.Prepare(source, imageprep.Options{
    MaxBytes:    15 << 20,
    MaxPixels:   50_000_000,
    Size:        1600,
    Mode:        imageprep.Fit,
    Format:      imageprep.JPEG,
    JPEGQuality: 85,
})
if err != nil {
    return err
}
if result.MetadataError != nil {
    slog.Warn("image metadata could not be read", "error", result.MetadataError)
}

source is an io.Reader. result.Bytes holds the encoded image; ContentType, Width, and Height describe that output.

JPEG, PNG, GIF, and static WebP inputs are supported. GIF uses its first frame; animated WebP is not supported. Byte and declared-pixel limits are checked before decoding; registering another Go image decoder does not expand the accepted formats. Supports reports whether an exact MIME type is accepted.

Fit preserves the aspect ratio within Size and never enlarges the picture. Square crops the centre and produces exactly Size by Size pixels, enlarging small inputs when needed. The pixel budget also bounds a square output. FitSize calculates fit dimensions without decoding an image.

All budgets and dimensions must be positive. JPEG quality must be 1–100 when writing JPEG; PNG ignores that field. There are no implicit input limits.

EXIF orientations 1–8 are applied before resizing. Metadata is not copied into the output. EXIF decoder errors and warnings are returned as MetadataError alongside a successful result in its decoded orientation. They do not turn an otherwise readable picture into a failed preparation. PNG preserves transparency; JPEG composites transparent pixels on black.

The package does not download, authorize, or store images. Callers choose their budgets, dimensions, output format, and how to report metadata problems. HEIC is not currently supported.

See the Go reference for the complete API and CHANGELOG for releases.

Development

make install-tools installs the linters. make build runs lint, compilation, and the test suite. make format formats Go sources.

Lines of code

Lines of code over time

Documentation

Overview

Package imageprep prepares untrusted images for display and storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FitSize

func FitSize(w, h, maxSide int) (int, int)

FitSize preserves the aspect ratio within maxSide, without enlargement. It returns zero dimensions if any argument is non-positive.

func Supports

func Supports(mime string) bool

Supports reports whether Prepare accepts this exact MIME type.

Types

type Format

type Format string

Format selects the encoded output.

const (
	JPEG Format = "image/jpeg"
	PNG  Format = "image/png"
)

type Mode

type Mode int

Mode chooses how the decoded image is resized. Fit preserves the aspect ratio without enlargement. Square crops the centre and resizes, including enlargement.

const (
	Fit Mode = iota
	Square
)

type Options

type Options struct {
	MaxBytes    int64
	MaxPixels   int64
	Size        int
	Mode        Mode
	Format      Format
	JPEGQuality int
}

Options sets input budgets, output dimensions, and encoding. MaxBytes, MaxPixels, and Size must be positive. Size is the maximum side for Fit and the exact side for Square. JPEGQuality must be 1–100 for JPEG output. MaxPixels also bounds the output bitmap. No input or output limits are implicit.

type Result

type Result struct {
	Bytes         []byte
	ContentType   string
	Width         int
	Height        int
	MetadataError error
}

Result holds the encoded image and its dimensions after orientation and resizing. MetadataError reports unreadable EXIF: preparation succeeds with the decoded orientation in that case. Callers decide how to report this partial result.

func Prepare

func Prepare(r io.Reader, o Options) (Result, error)

Prepare decodes JPEG, PNG, GIF, or WebP, corrects EXIF orientation, resizes, and encodes an image. GIF inputs use their first frame; animated WebP is not supported. Metadata is not copied to the output. PNG preserves transparency; JPEG composites it on black. Input bytes and declared pixels are checked before full decoding, regardless of other image decoders registered by the calling program.

Jump to

Keyboard shortcuts

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