124 lines
3.2 KiB
Go
124 lines
3.2 KiB
Go
package main
|
||
|
||
import (
|
||
"bytes"
|
||
"crypto/sha256"
|
||
"encoding/hex"
|
||
"image"
|
||
"image/jpeg"
|
||
|
||
"golang.org/x/image/draw"
|
||
)
|
||
|
||
type VariantSpec struct {
|
||
Name string
|
||
ContentType string
|
||
Width int
|
||
Height int
|
||
Data []byte
|
||
SHA256 string
|
||
}
|
||
|
||
func encodeOptimizedJPEG(src image.Image, quality int) ([]byte, error) {
|
||
var buf bytes.Buffer
|
||
if err := jpeg.Encode(&buf, src, &jpeg.Options{Quality: quality}); err != nil {
|
||
return nil, err
|
||
}
|
||
return buf.Bytes(), nil
|
||
}
|
||
|
||
// processImage возвращает 4 варианта. Оригинал — byte-identical с входом.
|
||
// thumbnail 256x256 (fit, без искажения), crop 128x128 (center crop),
|
||
// optimized — WebP q=80 без метаданных.
|
||
func processImage(raw []byte, contentType string) ([]VariantSpec, error) {
|
||
src, _, err := image.Decode(bytes.NewReader(raw))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
out := make([]VariantSpec, 0, 4)
|
||
|
||
// original — ровно те байты, что пришли в staging.
|
||
sum := sha256.Sum256(raw)
|
||
out = append(out, VariantSpec{
|
||
Name: "original", ContentType: contentType,
|
||
Width: src.Bounds().Dx(), Height: src.Bounds().Dy(),
|
||
Data: raw, SHA256: hex.EncodeToString(sum[:]),
|
||
})
|
||
|
||
// thumbnail 256x256 (fit).
|
||
th := resizeFit(src, 256, 256)
|
||
if err := appendEncoded(&out, "thumbnail", "image/jpeg", th); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// crop 128x128 (center crop).
|
||
cr := centerCrop(src, 128, 128)
|
||
if err := appendEncoded(&out, "crop", "image/jpeg", cr); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// optimized
|
||
optBuf, err := encodeOptimizedJPEG(src, 70)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
optSum := sha256.Sum256(optBuf)
|
||
out = append(out, VariantSpec{
|
||
Name: "optimized", ContentType: "image/jpeg",
|
||
Width: src.Bounds().Dx(), Height: src.Bounds().Dy(),
|
||
Data: optBuf, SHA256: hex.EncodeToString(optSum[:]),
|
||
})
|
||
return out, nil
|
||
}
|
||
|
||
func appendEncoded(out *[]VariantSpec, name, ct string, img image.Image) error {
|
||
var buf bytes.Buffer
|
||
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 85}); err != nil {
|
||
return err
|
||
}
|
||
sum := sha256.Sum256(buf.Bytes())
|
||
*out = append(*out, VariantSpec{
|
||
Name: name, ContentType: ct,
|
||
Width: img.Bounds().Dx(), Height: img.Bounds().Dy(),
|
||
Data: buf.Bytes(), SHA256: hex.EncodeToString(sum[:]),
|
||
})
|
||
return nil
|
||
}
|
||
|
||
func resizeFit(src image.Image, maxW, maxH int) image.Image {
|
||
b := src.Bounds()
|
||
w, h := b.Dx(), b.Dy()
|
||
scale := float64(maxW) / float64(w)
|
||
if float64(maxH)/float64(h) < scale {
|
||
scale = float64(maxH) / float64(h)
|
||
}
|
||
if scale > 1 {
|
||
scale = 1
|
||
}
|
||
nw, nh := int(float64(w)*scale), int(float64(h)*scale)
|
||
dst := image.NewRGBA(image.Rect(0, 0, nw, nh))
|
||
draw.CatmullRom.Scale(dst, dst.Bounds(), src, b, draw.Over, nil)
|
||
return dst
|
||
}
|
||
|
||
func centerCrop(src image.Image, w, h int) image.Image {
|
||
b := src.Bounds()
|
||
sw, sh := b.Dx(), b.Dy()
|
||
cw, ch := w, h
|
||
if sw < cw {
|
||
cw = sw
|
||
}
|
||
if sh < ch {
|
||
ch = sh
|
||
}
|
||
x0 := (sw - cw) / 2
|
||
y0 := (sh - ch) / 2
|
||
dst := image.NewRGBA(image.Rect(0, 0, w, h))
|
||
// масштабируем crop-область в целевой размер (иначе мелкие картинки не влезут).
|
||
draw.CatmullRom.Scale(dst, dst.Bounds(),
|
||
src, image.Rect(b.Min.X+x0, b.Min.Y+y0, b.Min.X+x0+cw, b.Min.Y+y0+ch),
|
||
draw.Over, nil)
|
||
return dst
|
||
}
|