imaging

package
v0.0.0-...-419985f Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 11 Imported by: 1 module, 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 imaging prepares bounded JPEG, PNG, and GIF uploads without storage or HTTP dependencies.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrByteLimit    = errors.New("image exceeds encoded byte limit")
	ErrPixelLimit   = errors.New("image exceeds decoded pixel limit")
	ErrFormat       = errors.New("unsupported or unrecognized image format")
	ErrInvalidImage = errors.New("incomplete or damaged image")
	ErrOrientation  = errors.New("invalid image orientation metadata")
	ErrAnimation    = errors.New("animated PNG is not supported")
)

Processing errors let callers choose their own response text.

Functions

This section is empty.

Types

type Options

type Options struct {
	// MaxBytes limits the encoded upload size.
	MaxBytes int
	// MaxPixels limits decoded width multiplied by height.
	MaxPixels int64
	// DisplaySize and ThumbnailSize limit each copy's longest edge.
	// ThumbnailSize must not exceed DisplaySize.
	DisplaySize, ThumbnailSize int
	// JPEGQuality must be from 1 to 100. PNG encoding is lossless.
	JPEGQuality int
}

Options sets upload limits and output dimensions. Every field must be set.

type Result

type Result struct {
	MediaType                    string
	Original, Display, Thumbnail []byte
}

Result contains validated original bytes and ready-to-serve copies. Original aliases the input. Copies may share backing storage, especially for small images and GIFs. Treat all returned bytes as read-only.

func Process

func Process(ctx context.Context, data []byte, options Options) (Result, error)

Process validates an upload, corrects orientation, and creates JPEG/PNG copies without metadata. It never enlarges or crops images. GIF bytes, including all frames and metadata, remain unchanged after first-frame validation. Calls are independent and safe to run concurrently. Callers must bound concurrent work to control memory use. Cancellation is checked between I/O and processing stages, not during every decoder or resizer operation.

Example
package main

import (
	"bytes"
	"context"
	"fmt"
	"image"
	"image/png"

	"github.com/trentnix/hyperserver/pkg/imaging"
)

func main() {
	var input bytes.Buffer
	if err := png.Encode(&input, image.NewRGBA(image.Rect(0, 0, 100, 50))); err != nil {
		panic(err)
	}
	result, err := imaging.Process(context.Background(), input.Bytes(), imaging.Options{
		MaxBytes: 8 * 1024 * 1024, MaxPixels: 20_000_000,
		DisplaySize: 2048, ThumbnailSize: 480, JPEGQuality: 85,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	// The caller chooses storage, access rules, and how to serve these copies.
	fmt.Println(result.MediaType, len(result.Original) > 0, len(result.Thumbnail) > 0)
}
Output:
image/png true true

Jump to

Keyboard shortcuts

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