imageconv

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 29 Imported by: 0 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.

Documentation

Overview

Package imageconv converts cached JPEG and PNG responses to WebP and AVIF (ADR-0043). The edge layer sends a request for a variant to the agent's image socket with X-Edgeweir-Image; Server passes it on to the origin layer it names and converts the answer in a child process (edgeweir-node imgconv), or answers with the original when it cannot.

Index

Constants

View Source
const (
	DefaultMemory  int64 = 1 << 30
	DefaultTimeout       = 8 * time.Second
	// TransientTTL caps how long an original answered for want of a
	// conversion slot stays cached under the variant's key.
	TransientTTL = 60
)

Defaults of the node's converter flags (ADR-0043).

View Source
const ChildCommand = "imgconv"

ChildCommand is the hidden subcommand that converts one image.

View Source
const Header = "X-Edgeweir-Image"

Header is the request header the edge layer sets on variant requests (lua/edgeweir/imageconv.lua).

View Source
const SavedHeader = "X-Edgeweir-Image-Saved"

SavedHeader carries the bytes a variant saved against its original on converted responses; it is cached with them and hidden from clients (the edge layer counts it, MinuteStats.image_bytes_saved).

Variables

View Source
var ErrUnsupported = errors.New("image not convertible")

ErrUnsupported marks images that are never converted: the original is served (and cached) as it is.

Functions

func Convert

func Convert(data []byte, kind Kind, format Format, quality int, info Info, maxOutput int64) ([]byte, error)

Convert decodes a JPEG or PNG image, turns it upright by its EXIF orientation and encodes it as format at quality. The output carries no metadata. Images that do not decode, whose size differs from what inspection read, or whose output would exceed maxOutput bytes are ErrUnsupported.

func DefaultWorkers

func DefaultWorkers() int

DefaultWorkers is min(4, max(1, CPUs / 2)).

func Estimate

func Estimate(format Format, pixels int64) int64

Estimate returns the memory a conversion of pixels to format is assumed to need (measured peaks plus margin, ADR-0043).

func Orient

func Orient(img image.Image, orientation int) image.Image

Orient returns img turned upright for an EXIF orientation (2-8: mirror and / or rotate; anything else returns img unchanged).

func RunChild

func RunChild(args []string, stdin io.Reader, stdout, stderr io.Writer) int

RunChild is the child process: it limits itself (address space for memory plus headroom, no files, a few descriptors, no capabilities, no_new_privs), reads the image from stdin and writes the converted image to stdout. Exit status 3 means the image is not convertible, which includes an output over --max-output bytes (nothing is written then).

Types

type Config

type Config struct {
	// Upstreams maps the origin layers' upstream names to their sockets.
	Upstreams map[string]string
	// Workers bounds concurrent conversions, Memory the sum of their
	// estimates; Timeout bounds one conversion (the child is killed).
	Workers int
	Memory  int64
	Timeout time.Duration
	// Command returns the child process for args (the agent's own binary
	// with ChildCommand) bound to ctx (exec.CommandContext: cancelling ctx
	// kills it); tests run their own.
	Command func(ctx context.Context, args []string) *exec.Cmd
	Log     *slog.Logger
}

Config configures a Server.

type Format

type Format string

Format is an output format.

const (
	WebP Format = "webp"
	AVIF Format = "avif"
)

Formats.

func (Format) MediaType

func (f Format) MediaType() string

MediaType returns the format's media type.

type Info

type Info struct {
	Width, Height int
	// Orientation is the EXIF orientation (1-8; 1 when absent): the child
	// turns the pixels so that the variant, which carries no EXIF, looks
	// like the original does in browsers.
	Orientation int
}

Info is what inspection learns from an image's headers.

func Inspect

func Inspect(data []byte, kind Kind) (Info, error)

Inspect reads the headers of a JPEG or PNG image without decoding it and rejects (ErrUnsupported) what a conversion could not reproduce: other color spaces than sRGB (an ICC profile that is not sRGB, CMYK, a PNG gamma other than 1/2.2), animation (APNG), and JPEG flavours Go's decoder lacks (arithmetic coding, other precisions than 8 bits).

func (Info) Pixels

func (i Info) Pixels() int64

Pixels returns the image's pixel count.

type Kind

type Kind string

Kind is a source type.

const (
	JPEG Kind = "jpeg"
	PNG  Kind = "png"
)

Source types.

type Params

type Params struct {
	Format    Format
	Quality   int
	MinSize   int64
	MaxSize   int64
	MaxPixels int64
	JPEG, PNG bool
	// Upstream is the origin layer the request would have gone to
	// ($edgeweir_origin_layer).
	Upstream string
}

Params are a variant request's parameters.

func ParseHeader

func ParseHeader(v string) (Params, error)

ParseHeader parses "<webp|avif>;q=<1-100>;min=<n>;max=<n>;px=<n>; src=<jpeg|png|jpeg,png>;up=<upstream>" (every field required, in any order after the format).

func (Params) Accepts

func (p Params) Accepts(k Kind) bool

Accepts reports whether the params convert kind.

type Server

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

Server is the image converter's HTTP handler.

func New

func New(cfg Config) *Server

New returns a Server.

func (*Server) Close

func (s *Server) Close()

Close releases idle connections.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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