images

package
v4.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package images provides small, pure helpers for validating, encoding, and thumbnailing images. It takes bytes/io.Reader in and gives bytes out; it has no knowledge of HTTP or object storage.

Supported formats are PNG, JPEG, and GIF. Thumbnailing preserves aspect ratio and never upscales.

Orientation: JPEG thumbnails honor the EXIF Orientation tag, so a photo captured in portrait on a phone (which stores landscape pixels plus an orientation flag) thumbnails upright. Orientation is read only from JPEG data; PNG and GIF have no equivalent tag.

Animation: animated GIFs keep their animation — every frame is resized and re-quantized, and the loop count and per-frame delays are preserved. Single-frame GIFs stay single-frame.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidImageContentType indicates the image was of an unsupported type.
	ErrInvalidImageContentType = errors.New("invalid image content type")
	// ErrInvalidThumbnailDimensions indicates a zero width or height was requested.
	ErrInvalidThumbnailDimensions = errors.New("thumbnail width and height must both be greater than zero")
	// ErrImageTooLarge indicates the image exceeds the configured size or dimension limits.
	ErrImageTooLarge = errors.New("image too large")
)

Functions

This section is empty.

Types

type Image

type Image struct {
	ContentType string
	Data        []byte
}

Image is a decoded, in-memory image with its detected content type.

func Decode

func Decode(r io.Reader) (*Image, error)

Decode reads an image from r, validating that it is a supported, decodable image and detecting its content type from the data itself (not from any filename).

Example
package main

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

	"github.com/primandproper/platform-go/v4/uploads/images"
)

func main() {
	var buf bytes.Buffer
	if err := png.Encode(&buf, image.NewRGBA(image.Rect(0, 0, 4, 4))); err != nil {
		panic(err)
	}

	// Decode detects the content type from the data itself, not from any filename.
	img, err := images.Decode(&buf)
	if err != nil {
		panic(err)
	}

	fmt.Println(img.ContentType)
}
Output:
image/png

func (*Image) DataURI

func (i *Image) DataURI() string

DataURI returns the image encoded as a base64 data URI.

Example
package main

import (
	"fmt"

	"github.com/primandproper/platform-go/v4/uploads/images"
)

func main() {
	img := &images.Image{ContentType: "text/plain", Data: []byte("hi")}

	fmt.Println(img.DataURI())
}
Output:
data:text/plain;base64,aGk=

func (*Image) Thumbnail

func (i *Image) Thumbnail(width, height uint) (*Image, error)

Thumbnail returns a resized copy of the image, re-encoded in its original format.

Example
package main

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

	"github.com/primandproper/platform-go/v4/uploads/images"
)

func main() {
	var buf bytes.Buffer
	if err := png.Encode(&buf, image.NewRGBA(image.Rect(0, 0, 8, 4))); err != nil {
		panic(err)
	}

	img, err := images.Decode(&buf)
	if err != nil {
		panic(err)
	}

	thumb, err := img.Thumbnail(4, 4)
	if err != nil {
		panic(err)
	}

	fmt.Println(thumb.ContentType)
}
Output:
image/png

Jump to

Keyboard shortcuts

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