imgutil

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidInput = errors.New("imgutil: invalid input")
	ErrOpenFile     = errors.New("imgutil: open file")
	ErrDecodeImage  = errors.New("imgutil: decode image")
	ErrDecodeHash   = errors.New("imgutil: decode hash")
	ErrInvalidHash  = thumbhash.ErrInvalidHash
)

Functions

func DecodeThumbHash added in v1.4.0

func DecodeThumbHash(ctx context.Context, hashData []byte) (image.Image, error)

func DecodeThumbHashBase64 added in v1.4.0

func DecodeThumbHashBase64(ctx context.Context, hashBase64 string) (image.Image, error)

func DecodeThumbHashBase64WithCfg added in v1.4.0

func DecodeThumbHashBase64WithCfg(ctx context.Context, hashBase64 string, cfg DecodingCfg) (image.Image, error)

func DecodeThumbHashFromBytes added in v1.4.0

func DecodeThumbHashFromBytes(ctx context.Context, data []byte) (image.Image, error)

func DecodeThumbHashFromBytesWithCfg added in v1.4.0

func DecodeThumbHashFromBytesWithCfg(ctx context.Context, data []byte, cfg DecodingCfg) (image.Image, error)

func DecodeThumbHashFromReader added in v1.4.0

func DecodeThumbHashFromReader(ctx context.Context, r io.Reader) (image.Image, error)

func DecodeThumbHashFromReaderWithCfg added in v1.4.0

func DecodeThumbHashFromReaderWithCfg(ctx context.Context, r io.Reader, cfg DecodingCfg) (image.Image, error)

func DecodeThumbHashWithCfg added in v1.4.0

func DecodeThumbHashWithCfg(ctx context.Context, hashData []byte, cfg DecodingCfg) (image.Image, error)

func EncodeHash added in v1.4.0

func EncodeHash(ctx context.Context, hash *Hash) ([]byte, error)

func EncodeHashBase64 added in v1.4.0

func EncodeHashBase64(ctx context.Context, hash *Hash) (string, error)

func EncodeThumbHash added in v1.4.0

func EncodeThumbHash(ctx context.Context, img image.Image) []byte

func EncodeThumbHashBase64 added in v1.4.0

func EncodeThumbHashBase64(ctx context.Context, img image.Image) string
Example
package main

import (
	"context"
	"fmt"
	"image"
	"image/color"

	"github.com/bronystylecrazy/ultrastructure/imgutil"
)

func main() {
	ctx := context.Background()

	img := image.NewRGBA(image.Rect(0, 0, 2, 2))
	img.Set(0, 0, color.RGBA{R: 255, A: 255})
	img.Set(1, 0, color.RGBA{G: 255, A: 255})
	img.Set(0, 1, color.RGBA{B: 255, A: 255})
	img.Set(1, 1, color.RGBA{R: 255, G: 255, B: 255, A: 255})

	hashB64 := imgutil.EncodeThumbHashBase64(ctx, img)
	fmt.Println(len(hashB64) > 0)
}
Output:

true

func EncodeThumbHashBase64FromBytes added in v1.4.0

func EncodeThumbHashBase64FromBytes(ctx context.Context, data []byte) (string, error)

func EncodeThumbHashBase64FromReader added in v1.4.0

func EncodeThumbHashBase64FromReader(ctx context.Context, r io.Reader) (string, error)
Example
package main

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

	"github.com/bronystylecrazy/ultrastructure/imgutil"
)

func main() {
	ctx := context.Background()

	img := image.NewRGBA(image.Rect(0, 0, 2, 2))
	img.Set(0, 0, color.RGBA{R: 255, A: 255})
	img.Set(1, 0, color.RGBA{G: 255, A: 255})
	img.Set(0, 1, color.RGBA{B: 255, A: 255})
	img.Set(1, 1, color.RGBA{R: 255, G: 255, B: 255, A: 255})

	var buf bytes.Buffer
	_ = png.Encode(&buf, img)

	hashB64, err := imgutil.EncodeThumbHashBase64FromReader(ctx, bytes.NewReader(buf.Bytes()))
	fmt.Println(err == nil, len(hashB64) > 0)
}
Output:

true true

func EncodeThumbHashFromBytes added in v1.4.0

func EncodeThumbHashFromBytes(ctx context.Context, data []byte) ([]byte, error)

func EncodeThumbHashFromReader added in v1.4.0

func EncodeThumbHashFromReader(ctx context.Context, r io.Reader) ([]byte, error)

func HashSize added in v1.4.0

func HashSize(ctx context.Context, hash *Hash, baseSize int) (int, int, error)

func Module added in v1.4.0

func Module(extends ...di.Node) di.Node

Types

type DecodedThumbHashResult added in v1.4.0

type DecodedThumbHashResult struct {
	Image         image.Image
	DecodedWidth  int
	DecodedHeight int
	ApproxAvgR    float64
	ApproxAvgG    float64
	ApproxAvgB    float64
	ApproxAvgA    float64
}

func BuildDecodedThumbHashResult added in v1.4.0

func BuildDecodedThumbHashResult(ctx context.Context, img image.Image) (DecodedThumbHashResult, error)

func DecodeThumbHashResult added in v1.4.0

func DecodeThumbHashResult(ctx context.Context, hashData []byte) (DecodedThumbHashResult, error)

func DecodeThumbHashResultWithCfg added in v1.4.0

func DecodeThumbHashResultWithCfg(ctx context.Context, hashData []byte, cfg DecodingCfg) (DecodedThumbHashResult, error)

type DecodingCfg added in v1.4.0

type DecodingCfg = thumbhash.DecodingCfg

type Hash added in v1.4.0

type Hash = thumbhash.Hash

func DecodeHash added in v1.4.0

func DecodeHash(ctx context.Context, hashData []byte, cfg *DecodingCfg) (*Hash, error)

func DecodeHashBase64 added in v1.4.0

func DecodeHashBase64(ctx context.Context, hashBase64 string, cfg *DecodingCfg) (*Hash, error)

type Service added in v1.4.0

type Service struct {
	otel.Telemetry
}

func NewService added in v1.4.0

func NewService() *Service

func (*Service) DecodeHash added in v1.4.0

func (s *Service) DecodeHash(ctx context.Context, hashData []byte, cfg *DecodingCfg) (*Hash, error)

func (*Service) DecodeHashBase64 added in v1.4.0

func (s *Service) DecodeHashBase64(ctx context.Context, hashBase64 string, cfg *DecodingCfg) (*Hash, error)

func (*Service) DecodeThumbHash added in v1.4.0

func (s *Service) DecodeThumbHash(ctx context.Context, hashData []byte) (image.Image, error)

func (*Service) DecodeThumbHashBase64 added in v1.4.0

func (s *Service) DecodeThumbHashBase64(ctx context.Context, hashBase64 string) (image.Image, error)

func (*Service) DecodeThumbHashBase64WithCfg added in v1.4.0

func (s *Service) DecodeThumbHashBase64WithCfg(ctx context.Context, hashBase64 string, cfg DecodingCfg) (image.Image, error)

func (*Service) DecodeThumbHashFromBytes added in v1.4.0

func (s *Service) DecodeThumbHashFromBytes(ctx context.Context, data []byte) (image.Image, error)

func (*Service) DecodeThumbHashFromBytesWithCfg added in v1.4.0

func (s *Service) DecodeThumbHashFromBytesWithCfg(ctx context.Context, data []byte, cfg DecodingCfg) (image.Image, error)

func (*Service) DecodeThumbHashFromReader added in v1.4.0

func (s *Service) DecodeThumbHashFromReader(ctx context.Context, r io.Reader) (image.Image, error)

func (*Service) DecodeThumbHashFromReaderWithCfg added in v1.4.0

func (s *Service) DecodeThumbHashFromReaderWithCfg(ctx context.Context, r io.Reader, cfg DecodingCfg) (image.Image, error)

func (*Service) DecodeThumbHashResult added in v1.4.0

func (s *Service) DecodeThumbHashResult(ctx context.Context, hashData []byte) (DecodedThumbHashResult, error)

func (*Service) DecodeThumbHashResultWithCfg added in v1.4.0

func (s *Service) DecodeThumbHashResultWithCfg(ctx context.Context, hashData []byte, cfg DecodingCfg) (DecodedThumbHashResult, error)

func (*Service) DecodeThumbHashWithCfg added in v1.4.0

func (s *Service) DecodeThumbHashWithCfg(ctx context.Context, hashData []byte, cfg DecodingCfg) (image.Image, error)

func (*Service) EncodeHash added in v1.4.0

func (s *Service) EncodeHash(ctx context.Context, hash *Hash) ([]byte, error)

func (*Service) EncodeHashBase64 added in v1.4.0

func (s *Service) EncodeHashBase64(ctx context.Context, hash *Hash) (string, error)

func (*Service) EncodeThumbHash added in v1.4.0

func (s *Service) EncodeThumbHash(ctx context.Context, img image.Image) []byte

func (*Service) EncodeThumbHashBase64 added in v1.4.0

func (s *Service) EncodeThumbHashBase64(ctx context.Context, img image.Image) string

func (*Service) EncodeThumbHashBase64FromBytes added in v1.4.0

func (s *Service) EncodeThumbHashBase64FromBytes(ctx context.Context, data []byte) (string, error)

func (*Service) EncodeThumbHashBase64FromReader added in v1.4.0

func (s *Service) EncodeThumbHashBase64FromReader(ctx context.Context, r io.Reader) (string, error)

func (*Service) EncodeThumbHashFromBytes added in v1.4.0

func (s *Service) EncodeThumbHashFromBytes(ctx context.Context, data []byte) ([]byte, error)

func (*Service) EncodeThumbHashFromReader added in v1.4.0

func (s *Service) EncodeThumbHashFromReader(ctx context.Context, r io.Reader) ([]byte, error)

func (*Service) EncodeThumbHashResult added in v1.4.0

func (s *Service) EncodeThumbHashResult(ctx context.Context, img image.Image) (ThumbHashResult, error)

func (*Service) EncodeThumbHashResultFromBytes added in v1.4.0

func (s *Service) EncodeThumbHashResultFromBytes(ctx context.Context, data []byte) (ThumbHashResult, error)

func (*Service) EncodeThumbHashResultFromReader added in v1.4.0

func (s *Service) EncodeThumbHashResultFromReader(ctx context.Context, r io.Reader) (ThumbHashResult, error)

func (*Service) GenerateThumbHash added in v1.4.0

func (s *Service) GenerateThumbHash(ctx context.Context, fileHeader *multipart.FileHeader) (ThumbHashResult, error)

func (*Service) HashSize added in v1.4.0

func (s *Service) HashSize(ctx context.Context, hash *Hash, baseSize int) (int, int, error)

type ThumbHashResult added in v1.4.0

type ThumbHashResult struct {
	HashBase64 string
	Width      int
	Height     int
	AvgR       float64
	AvgG       float64
	AvgB       float64
	AvgA       float64
}

func BuildThumbHashResult added in v1.4.0

func BuildThumbHashResult(ctx context.Context, img image.Image) (ThumbHashResult, error)

func EncodeThumbHashResult added in v1.4.0

func EncodeThumbHashResult(ctx context.Context, img image.Image) (ThumbHashResult, error)

func EncodeThumbHashResultFromBytes added in v1.4.0

func EncodeThumbHashResultFromBytes(ctx context.Context, data []byte) (ThumbHashResult, error)

func EncodeThumbHashResultFromReader added in v1.4.0

func EncodeThumbHashResultFromReader(ctx context.Context, r io.Reader) (ThumbHashResult, error)

func GenerateThumbHash

func GenerateThumbHash(ctx context.Context, fileHeader *multipart.FileHeader) (ThumbHashResult, error)

GenerateThumbHash is a multipart adapter that returns ThumbHash plus image metadata.

Example
package main

import (
	"bytes"
	"context"
	"fmt"
	"image"
	"image/color"
	"image/png"
	"mime/multipart"
	"net/http/httptest"

	"github.com/bronystylecrazy/ultrastructure/imgutil"
)

func main() {
	ctx := context.Background()

	img := image.NewRGBA(image.Rect(0, 0, 2, 2))
	img.Set(0, 0, color.RGBA{R: 255, A: 255})
	img.Set(1, 0, color.RGBA{G: 255, A: 255})
	img.Set(0, 1, color.RGBA{B: 255, A: 255})
	img.Set(1, 1, color.RGBA{R: 255, G: 255, B: 255, A: 255})

	var imgBuf bytes.Buffer
	_ = png.Encode(&imgBuf, img)

	var body bytes.Buffer
	writer := multipart.NewWriter(&body)
	part, _ := writer.CreateFormFile("file", "sample.png")
	_, _ = part.Write(imgBuf.Bytes())
	_ = writer.Close()

	req := httptest.NewRequest("POST", "/", &body)
	req.Header.Set("Content-Type", writer.FormDataContentType())
	_ = req.ParseMultipartForm(1 << 20)
	fileHeader := req.MultipartForm.File["file"][0]

	result, err := imgutil.GenerateThumbHash(ctx, fileHeader)
	fmt.Println(err == nil, len(result.HashBase64) > 0, result.Width > 0, result.Height > 0)
}
Output:

true true true true

type ThumbHasher added in v1.4.0

type ThumbHasher interface {
	EncodeThumbHash(ctx context.Context, img image.Image) []byte
	EncodeThumbHashBase64(ctx context.Context, img image.Image) string
	EncodeThumbHashResult(ctx context.Context, img image.Image) (ThumbHashResult, error)
	EncodeThumbHashFromBytes(ctx context.Context, data []byte) ([]byte, error)
	EncodeThumbHashBase64FromBytes(ctx context.Context, data []byte) (string, error)
	EncodeThumbHashResultFromBytes(ctx context.Context, data []byte) (ThumbHashResult, error)
	EncodeThumbHashFromReader(ctx context.Context, r io.Reader) ([]byte, error)
	EncodeThumbHashBase64FromReader(ctx context.Context, r io.Reader) (string, error)
	EncodeThumbHashResultFromReader(ctx context.Context, r io.Reader) (ThumbHashResult, error)
	DecodeThumbHash(ctx context.Context, hashData []byte) (image.Image, error)
	DecodeThumbHashFromBytes(ctx context.Context, data []byte) (image.Image, error)
	DecodeThumbHashFromBytesWithCfg(ctx context.Context, data []byte, cfg DecodingCfg) (image.Image, error)
	DecodeThumbHashFromReader(ctx context.Context, r io.Reader) (image.Image, error)
	DecodeThumbHashFromReaderWithCfg(ctx context.Context, r io.Reader, cfg DecodingCfg) (image.Image, error)
	DecodeThumbHashWithCfg(ctx context.Context, hashData []byte, cfg DecodingCfg) (image.Image, error)
	DecodeThumbHashBase64(ctx context.Context, hashBase64 string) (image.Image, error)
	DecodeThumbHashBase64WithCfg(ctx context.Context, hashBase64 string, cfg DecodingCfg) (image.Image, error)
	DecodeThumbHashResult(ctx context.Context, hashData []byte) (DecodedThumbHashResult, error)
	DecodeThumbHashResultWithCfg(ctx context.Context, hashData []byte, cfg DecodingCfg) (DecodedThumbHashResult, error)
	DecodeHash(ctx context.Context, hashData []byte, cfg *DecodingCfg) (*Hash, error)
	DecodeHashBase64(ctx context.Context, hashBase64 string, cfg *DecodingCfg) (*Hash, error)
	EncodeHash(ctx context.Context, hash *Hash) ([]byte, error)
	EncodeHashBase64(ctx context.Context, hash *Hash) (string, error)
	HashSize(ctx context.Context, hash *Hash, baseSize int) (int, int, error)
	GenerateThumbHash(ctx context.Context, fileHeader *multipart.FileHeader) (ThumbHashResult, error)
}

Directories

Path Synopsis
internal
thumbhash
Adapted from go.n16f.net/thumbhash (ISC license), with local optimizations.
Adapted from go.n16f.net/thumbhash (ISC license), with local optimizations.

Jump to

Keyboard shortcuts

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