images

package
v0.0.0-...-78d3d1f Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package images provides image processing and resizing capabilities to enforce per-model input limits (maxWidth, maxHeight, maxBytes, animated GIF flattening).

Format conversions and transparency trade-offs: When an image must be re-encoded (animated, BMP, exceeding dimension or byte limits), each candidate size first attempts a PNG encoding — which preserves alpha/transparency — and falls back to JPEG with a descending quality ladder only when PNG does not fit within maxBytes. If nothing fits, dimensions are progressively shrunk (x0.75) with the same PNG-then-JPEG strategy, ending in a minimal-quality JPEG fallback. Images that fit within all limits pass through byte-identical, preserving their original format and alpha channel.

EXIF orientation: JPEG and WebP inputs carry their EXIF orientation tag through the pass-through path untouched; when an image is re-encoded, the orientation transform is applied to the decoded pixels before resizing so the reported and output dimensions match the visually oriented image.

Index

Constants

View Source
const (
	DefaultMaxWidth       = 2000
	DefaultMaxHeight      = 2000
	DefaultMaxBase64Bytes = int64(4718592) // 4.5 MiB
	DefaultJPEGQuality    = 80
)

Default limits for image processing.

Variables

This section is empty.

Functions

func DetectMimeType

func DetectMimeType(data []byte) string

DetectMimeType reports the supported image MIME type of data based on its content, or "" if data does not look like a supported image.

Types

type ProcessedImage

type ProcessedImage struct {
	Data           string   `json:"data"`
	MimeType       string   `json:"mimeType"`
	OriginalWidth  int      `json:"originalWidth"`
	OriginalHeight int      `json:"originalHeight"`
	Width          int      `json:"width"`
	Height         int      `json:"height"`
	WasResized     bool     `json:"wasResized"`
	Notes          []string `json:"notes,omitempty"`
}

ProcessedImage carries the result of image inspection, normalization, and resizing.

func ProcessImage

func ProcessImage(data []byte, mimeType string, limits *types.ModelImageInputLimits) (*ProcessedImage, error)

ProcessImage decodes the input image, validates its limits, scales dimensions keeping aspect ratio if necessary, down-samples GIF animations to the first frame, and compresses payload bytes to satisfy maxBytes.

Jump to

Keyboard shortcuts

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