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 ¶
- func GenerateThumbnailAndHashes(r io.ReadSeeker, srcW, srcH int) (*bytes.Buffer, *sql.NullString, *sql.NullInt64, error)
- func GetBytesBuffer() *bytes.Buffer
- func GetMD5() hash.Hash
- func GetNullInt64() *sql.NullInt64
- func GetNullString() *sql.NullString
- func PutBytesBuffer(buf *bytes.Buffer)
- func PutMD5(h hash.Hash)
- func PutNullInt64(ni *sql.NullInt64)
- func PutNullString(ns *sql.NullString)
- type Generator
- type MockGenerator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateThumbnailAndHashes ¶
func GenerateThumbnailAndHashes(r io.ReadSeeker, srcW, srcH int) (*bytes.Buffer, *sql.NullString, *sql.NullInt64, error)
GenerateThumbnailAndHashes creates a thumbnail for the image read from r. The full source image is always decoded via fullImageDecodeHook; srcW and srcH are the source image dimensions in pixels and drive an adaptive JPEG DCT scale: chooseJPEGDCTSize picks a DCTSizeScaled from srcW/srcH so the decoded JPEG is at least the 200×150 gallery-thumb fit size (large JPEGs stay at 1/8; non-JPEG formats ignore the dims and use stdlib image.Decode). The decoded source 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 ¶
GetBytesBuffer retrieves a bytes.Buffer from the pool.
func GetNullInt64 ¶
GetNullInt64 retrieves an sql.NullInt64 from the pool.
func GetNullString ¶
func GetNullString() *sql.NullString
GetNullString retrieves an sql.NullString from the pool.
func PutBytesBuffer ¶
PutBytesBuffer returns a bytes.Buffer to the pool, resetting it first.
func PutNullInt64 ¶
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; srcW and srcH are its dimensions in
// pixels and drive the adaptive JPEG DCT decode scale.
// Returns thumbnail bytes, MD5 hash, perceptual hash, and any error.
GenerateThumbnailAndHashes(r io.ReadSeeker, srcW, srcH int) (*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, srcW, srcH int) (*bytes.Buffer, *sql.NullString, *sql.NullInt64, error)
GenerateThumbnailAndHashes returns the mock values.