imageutil

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxResizeWidth is the maximum width before downscaling.
	MaxResizeWidth = 2048
	// MaxResizeHeight is the maximum height before downscaling.
	MaxResizeHeight = 768
)
View Source
const MaxImageBytes = 20 * 1024 * 1024

MaxImageBytes is the maximum file size allowed for image loading (20MB).

Variables

View Source
var SupportedExtensions = map[string]string{
	".png":  "image/png",
	".jpg":  "image/jpeg",
	".jpeg": "image/jpeg",
	".gif":  "image/gif",
	".webp": "image/webp",
}

SupportedExtensions maps file extensions to their MIME types.

Functions

func ContentPartFromBytes

func ContentPartFromBytes(raw []byte, mime string, fileName string, cache *Cache) (model.ContentPart, error)

ContentPartFromBytes creates a ContentPart from raw image bytes, resizing if needed and caching the result.

func ExtractClipboardImage

func ExtractClipboardImage() ([]byte, string, error)

ExtractClipboardImage checks the Linux clipboard for image data using xclip. Returns (data, mimeType, nil) on success, (nil, "", nil) if no image, or (nil, "", err) on failure.

func IsImagePath

func IsImagePath(path string) bool

IsImagePath returns true if the file path has a recognized image extension.

func Key

func Key(data []byte) string

Key computes the cache key for raw image bytes.

func LoadAsContentPart

func LoadAsContentPart(absPath string) (model.ContentPart, error)

LoadAsContentPart reads an image file from disk and returns it as a ContentPart. No caching or resizing is applied.

func LoadAsContentPartCached

func LoadAsContentPartCached(absPath string, cache *Cache) (model.ContentPart, error)

LoadAsContentPartCached reads an image file, resizes if needed, and returns a cached ContentPart. If cache is nil, behaves like LoadAsContentPart with resize applied.

func MimeTypeForPath

func MimeTypeForPath(path string) (string, bool)

MimeTypeForPath returns the MIME type for a recognized image file path.

Types

type Cache

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

Cache is an LRU cache for processed image ContentParts. The cache key is the SHA-1 hex digest of the raw image bytes.

func NewCache

func NewCache(capacity int) *Cache

NewCache creates a new image cache with the given capacity.

func (*Cache) Get

func (c *Cache) Get(key string) (model.ContentPart, bool)

Get returns a cached ContentPart for the given key, if present. Moves the entry to the front of the LRU list.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of entries in the cache.

func (*Cache) Put

func (c *Cache) Put(key string, part model.ContentPart)

Put inserts a ContentPart into the cache, evicting the oldest entry if full.

type ResizeResult

type ResizeResult struct {
	Data     []byte
	MimeType string
	Width    int
	Height   int
}

ResizeResult holds the output of ResizeIfNeeded.

func ResizeIfNeeded

func ResizeIfNeeded(data []byte, srcMime string) (ResizeResult, error)

ResizeIfNeeded decodes the image, downscales it if it exceeds the max dimensions, and re-encodes it. If the image is within bounds the original data is returned unchanged.

Jump to

Keyboard shortcuts

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