thumbnail

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package thumbnail provides functionality for generating image thumbnails and computing image hashes (MD5, pHash). It uses lightweight pooling for buffers and hashers to minimize allocations and improve performance.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateThumbnailAndHashes

func GenerateThumbnailAndHashes(r io.ReadSeeker) (*bytes.Buffer, *sql.NullString, *sql.NullInt64, error)

GenerateThumbnailAndHashes creates a thumbnail for the image read from r. If the image (JPEG, TIFF, or WebP) contains an embedded EXIF thumbnail, that thumbnail is decoded instead of the full image, dramatically reducing memory use and CPU time. The source (embedded thumbnail or full image) is fitted inside a 200x150 pixel box with draw.ApproxBiLinear into a pooled RGBA canvas and encoded as JPEG. It also calculates MD5 (over the full file bytes) and pHash: the gallery thumbnail is squashed to 64x64 with draw.ApproxBiLinear into a second pooled RGBA canvas, and imagehash.NewPHash64 is computed over that 64x64 canvas. Both pooled destinations are returned to their pools before the function returns. It returns the JPEG data as a bytes.Buffer, sql.NullString for MD5, and sql.NullInt64 for pHash, or an error if generation fails.

func GetBytesBuffer

func GetBytesBuffer() *bytes.Buffer

GetBytesBuffer retrieves a bytes.Buffer from the pool.

func GetMD5

func GetMD5() hash.Hash

GetMD5 retrieves an MD5 hash.Hash from the pool.

func GetNullInt64

func GetNullInt64() *sql.NullInt64

GetNullInt64 retrieves an sql.NullInt64 from the pool.

func GetNullString

func GetNullString() *sql.NullString

GetNullString retrieves an sql.NullString from the pool.

func PutBytesBuffer

func PutBytesBuffer(buf *bytes.Buffer)

PutBytesBuffer returns a bytes.Buffer to the pool, resetting it first.

func PutMD5

func PutMD5(h hash.Hash)

PutMD5 returns an MD5 hash.Hash to the pool, resetting it first.

func PutNullInt64

func PutNullInt64(ni *sql.NullInt64)

PutNullInt64 returns an sql.NullInt64 to the pool, resetting it first.

func PutNullString

func PutNullString(ns *sql.NullString)

PutNullString returns an sql.NullString to the pool, resetting it first.

Types

type Generator

type Generator interface {
	// GenerateThumbnailAndHashes creates a thumbnail and computes MD5/pHash.
	// The reader should be an image file.
	// Returns thumbnail bytes, MD5 hash, perceptual hash, and any error.
	GenerateThumbnailAndHashes(r io.ReadSeeker) (*bytes.Buffer, *sql.NullString, *sql.NullInt64, error)
}

Generator abstracts thumbnail generation and hash computation.

type MockGenerator

type MockGenerator struct {
	Thumbnail *bytes.Buffer
	MD5       *sql.NullString
	PHash     *sql.NullInt64
	Err       error
}

MockGenerator is a mock implementation of Generator for testing.

func (*MockGenerator) GenerateThumbnailAndHashes

func (m *MockGenerator) GenerateThumbnailAndHashes(r io.ReadSeeker) (*bytes.Buffer, *sql.NullString, *sql.NullInt64, error)

GenerateThumbnailAndHashes returns the mock values.

Jump to

Keyboard shortcuts

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