Documentation
¶
Overview ¶
Package imaging prepares bounded JPEG, PNG, and GIF uploads without storage or HTTP dependencies.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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