Files
mtusi2026lab6/internal/messenger/transform.go
T

105 lines
3.1 KiB
Go

package messenger
import (
"encoding/json"
"fmt"
"github.com/disintegration/imaging"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"math"
"os"
"os/exec"
"path/filepath"
)
type Variant struct {
Name string `json:"name"`
ContentType string `json:"content_type"`
Size int64 `json:"size_bytes"`
Width int `json:"width"`
Height int `json:"height"`
SHA256 string `json:"sha256"`
}
func fitDimensions(w, h, max int) (int, int) {
scale := math.Min(1, math.Min(float64(max)/float64(w), float64(max)/float64(h)))
return int(math.Max(1, math.Floor(float64(w)*scale))), int(math.Max(1, math.Floor(float64(h)*scale)))
}
// Runs in a disposable child process, killed by the parent's 20s deadline.
// The only paths are server-generated temp paths. No client arguments/shell.
func Transform(input, dir string, versions ...int) error {
version := 1
if len(versions) > 0 {
version = versions[0]
}
if version < 1 || version > 2 {
return fmt.Errorf("unknown pipeline version")
}
quality, webpQuality := 85, "80"
if version == 2 {
quality, webpQuality = 90, "82"
}
f, e := os.Open(input)
if e != nil {
return e
}
c, format, e := image.DecodeConfig(f)
f.Close()
if e != nil || c.Width < 1 || c.Height < 1 || int64(c.Width)*int64(c.Height) > maxPixels {
return ErrInvalidImage
}
img, e := imaging.Open(input, imaging.AutoOrientation(true))
if e != nil {
return ErrInvalidImage
}
var oriented image.Image = img
if format == "png" {
oriented = orientPNG(img, pngOrientation(input))
}
bounds := oriented.Bounds()
white := image.NewNRGBA(bounds)
draw.Draw(white, bounds, &image.Uniform{C: color.White}, image.Point{}, draw.Src)
draw.Draw(white, bounds, oriented, bounds.Min, draw.Over)
w, h := fitDimensions(bounds.Dx(), bounds.Dy(), 256)
thumbnail := imaging.Resize(white, w, h, imaging.Lanczos)
crop := imaging.Fill(white, 128, 128, imaging.Center, imaging.Lanczos)
for name, result := range map[string]image.Image{"thumbnail": thumbnail, "crop": crop} {
f, e := os.Create(filepath.Join(dir, name))
if e != nil {
return e
}
e = jpeg.Encode(f, result, &jpeg.Options{Quality: quality})
ce := f.Close()
if e != nil {
return e
}
if ce != nil {
return ce
}
}
ow, oh := fitDimensions(bounds.Dx(), bounds.Dy(), 1280)
optimized := imaging.Resize(white, ow, oh, imaging.Lanczos)
p := filepath.Join(dir, "optimized.png")
f, e = os.Create(p)
if e != nil {
return e
}
e = png.Encode(f, optimized)
f.Close()
if e != nil {
return e
}
// Decoding/re-encoding removes EXIF and other source metadata. WebP quality 80.
cmd := exec.Command("cwebp", "-quiet", "-q", webpQuality, "-m", "4", "-metadata", "none", p, "-o", filepath.Join(dir, "optimized"))
if e = cmd.Run(); e != nil {
return fmt.Errorf("webp_encode")
}
result := []Variant{{Name: "thumbnail", ContentType: "image/jpeg", Width: w, Height: h}, {Name: "crop", ContentType: "image/jpeg", Width: 128, Height: 128}, {Name: "optimized", ContentType: "image/webp", Width: ow, Height: oh}}
b, _ := json.Marshal(result)
return os.WriteFile(filepath.Join(dir, "variants.json"), b, 0600)
}