Documentation
¶
Index ¶
- Variables
- func DecodeThumbHash(ctx context.Context, hashData []byte) (image.Image, error)
- func DecodeThumbHashBase64(ctx context.Context, hashBase64 string) (image.Image, error)
- func DecodeThumbHashBase64WithCfg(ctx context.Context, hashBase64 string, cfg DecodingCfg) (image.Image, error)
- func DecodeThumbHashFromBytes(ctx context.Context, data []byte) (image.Image, error)
- func DecodeThumbHashFromBytesWithCfg(ctx context.Context, data []byte, cfg DecodingCfg) (image.Image, error)
- func DecodeThumbHashFromReader(ctx context.Context, r io.Reader) (image.Image, error)
- func DecodeThumbHashFromReaderWithCfg(ctx context.Context, r io.Reader, cfg DecodingCfg) (image.Image, error)
- func DecodeThumbHashWithCfg(ctx context.Context, hashData []byte, cfg DecodingCfg) (image.Image, error)
- func EncodeHash(ctx context.Context, hash *Hash) ([]byte, error)
- func EncodeHashBase64(ctx context.Context, hash *Hash) (string, error)
- func EncodeThumbHash(ctx context.Context, img image.Image) []byte
- func EncodeThumbHashBase64(ctx context.Context, img image.Image) string
- func EncodeThumbHashBase64FromBytes(ctx context.Context, data []byte) (string, error)
- func EncodeThumbHashBase64FromReader(ctx context.Context, r io.Reader) (string, error)
- func EncodeThumbHashFromBytes(ctx context.Context, data []byte) ([]byte, error)
- func EncodeThumbHashFromReader(ctx context.Context, r io.Reader) ([]byte, error)
- func HashSize(ctx context.Context, hash *Hash, baseSize int) (int, int, error)
- func Module(extends ...di.Node) di.Node
- type DecodedThumbHashResult
- func BuildDecodedThumbHashResult(ctx context.Context, img image.Image) (DecodedThumbHashResult, error)
- func DecodeThumbHashResult(ctx context.Context, hashData []byte) (DecodedThumbHashResult, error)
- func DecodeThumbHashResultWithCfg(ctx context.Context, hashData []byte, cfg DecodingCfg) (DecodedThumbHashResult, error)
- type DecodingCfg
- type Hash
- type Service
- func (s *Service) DecodeHash(ctx context.Context, hashData []byte, cfg *DecodingCfg) (*Hash, error)
- func (s *Service) DecodeHashBase64(ctx context.Context, hashBase64 string, cfg *DecodingCfg) (*Hash, error)
- func (s *Service) DecodeThumbHash(ctx context.Context, hashData []byte) (image.Image, error)
- func (s *Service) DecodeThumbHashBase64(ctx context.Context, hashBase64 string) (image.Image, error)
- func (s *Service) DecodeThumbHashBase64WithCfg(ctx context.Context, hashBase64 string, cfg DecodingCfg) (image.Image, error)
- func (s *Service) DecodeThumbHashFromBytes(ctx context.Context, data []byte) (image.Image, error)
- func (s *Service) DecodeThumbHashFromBytesWithCfg(ctx context.Context, data []byte, cfg DecodingCfg) (image.Image, error)
- func (s *Service) DecodeThumbHashFromReader(ctx context.Context, r io.Reader) (image.Image, error)
- func (s *Service) DecodeThumbHashFromReaderWithCfg(ctx context.Context, r io.Reader, cfg DecodingCfg) (image.Image, error)
- func (s *Service) DecodeThumbHashResult(ctx context.Context, hashData []byte) (DecodedThumbHashResult, error)
- func (s *Service) DecodeThumbHashResultWithCfg(ctx context.Context, hashData []byte, cfg DecodingCfg) (DecodedThumbHashResult, error)
- func (s *Service) DecodeThumbHashWithCfg(ctx context.Context, hashData []byte, cfg DecodingCfg) (image.Image, error)
- func (s *Service) EncodeHash(ctx context.Context, hash *Hash) ([]byte, error)
- func (s *Service) EncodeHashBase64(ctx context.Context, hash *Hash) (string, error)
- func (s *Service) EncodeThumbHash(ctx context.Context, img image.Image) []byte
- func (s *Service) EncodeThumbHashBase64(ctx context.Context, img image.Image) string
- func (s *Service) EncodeThumbHashBase64FromBytes(ctx context.Context, data []byte) (string, error)
- func (s *Service) EncodeThumbHashBase64FromReader(ctx context.Context, r io.Reader) (string, error)
- func (s *Service) EncodeThumbHashFromBytes(ctx context.Context, data []byte) ([]byte, error)
- func (s *Service) EncodeThumbHashFromReader(ctx context.Context, r io.Reader) ([]byte, error)
- func (s *Service) EncodeThumbHashResult(ctx context.Context, img image.Image) (ThumbHashResult, error)
- func (s *Service) EncodeThumbHashResultFromBytes(ctx context.Context, data []byte) (ThumbHashResult, error)
- func (s *Service) EncodeThumbHashResultFromReader(ctx context.Context, r io.Reader) (ThumbHashResult, error)
- func (s *Service) GenerateThumbHash(ctx context.Context, fileHeader *multipart.FileHeader) (ThumbHashResult, error)
- func (s *Service) HashSize(ctx context.Context, hash *Hash, baseSize int) (int, int, error)
- type ThumbHashResult
- func BuildThumbHashResult(ctx context.Context, img image.Image) (ThumbHashResult, error)
- func EncodeThumbHashResult(ctx context.Context, img image.Image) (ThumbHashResult, error)
- func EncodeThumbHashResultFromBytes(ctx context.Context, data []byte) (ThumbHashResult, error)
- func EncodeThumbHashResultFromReader(ctx context.Context, r io.Reader) (ThumbHashResult, error)
- func GenerateThumbHash(ctx context.Context, fileHeader *multipart.FileHeader) (ThumbHashResult, error)
- type ThumbHasher
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func DecodeThumbHash ¶ added in v1.4.0
func DecodeThumbHashBase64 ¶ added in v1.4.0
func DecodeThumbHashBase64WithCfg ¶ added in v1.4.0
func DecodeThumbHashFromBytes ¶ added in v1.4.0
func DecodeThumbHashFromBytesWithCfg ¶ added in v1.4.0
func DecodeThumbHashFromReader ¶ added in v1.4.0
func DecodeThumbHashFromReaderWithCfg ¶ added in v1.4.0
func DecodeThumbHashWithCfg ¶ added in v1.4.0
func EncodeHashBase64 ¶ added in v1.4.0
func EncodeThumbHash ¶ added in v1.4.0
func EncodeThumbHashBase64 ¶ added in v1.4.0
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 EncodeThumbHashBase64FromReader ¶ added in v1.4.0
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 EncodeThumbHashFromReader ¶ added in v1.4.0
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 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
func DecodeHash ¶ added in v1.4.0
func DecodeHashBase64 ¶ added in v1.4.0
type Service ¶ added in v1.4.0
func NewService ¶ added in v1.4.0
func NewService() *Service
func (*Service) DecodeHash ¶ added in v1.4.0
func (*Service) DecodeHashBase64 ¶ added in v1.4.0
func (*Service) DecodeThumbHash ¶ added in v1.4.0
func (*Service) DecodeThumbHashBase64 ¶ added in v1.4.0
func (*Service) DecodeThumbHashBase64WithCfg ¶ added in v1.4.0
func (*Service) DecodeThumbHashFromBytes ¶ added in v1.4.0
func (*Service) DecodeThumbHashFromBytesWithCfg ¶ added in v1.4.0
func (*Service) DecodeThumbHashFromReader ¶ added in v1.4.0
func (*Service) DecodeThumbHashFromReaderWithCfg ¶ added in v1.4.0
func (*Service) DecodeThumbHashResult ¶ added in v1.4.0
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 (*Service) EncodeHash ¶ added in v1.4.0
func (*Service) EncodeHashBase64 ¶ added in v1.4.0
func (*Service) EncodeThumbHash ¶ added in v1.4.0
func (*Service) EncodeThumbHashBase64 ¶ added in v1.4.0
func (*Service) EncodeThumbHashBase64FromBytes ¶ added in v1.4.0
func (*Service) EncodeThumbHashBase64FromReader ¶ added in v1.4.0
func (*Service) EncodeThumbHashFromBytes ¶ added in v1.4.0
func (*Service) EncodeThumbHashFromReader ¶ added in v1.4.0
func (*Service) EncodeThumbHashResult ¶ added in v1.4.0
func (*Service) EncodeThumbHashResultFromBytes ¶ added in v1.4.0
func (*Service) EncodeThumbHashResultFromReader ¶ added in v1.4.0
func (*Service) GenerateThumbHash ¶ added in v1.4.0
func (s *Service) GenerateThumbHash(ctx context.Context, fileHeader *multipart.FileHeader) (ThumbHashResult, 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 EncodeThumbHashResult ¶ added in v1.4.0
func EncodeThumbHashResultFromBytes ¶ added in v1.4.0
func EncodeThumbHashResultFromBytes(ctx context.Context, data []byte) (ThumbHashResult, error)
func EncodeThumbHashResultFromReader ¶ added in v1.4.0
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)
}
Click to show internal directories.
Click to hide internal directories.