feat(worker): add centered 128x128 JPEG crop
This commit is contained in:
+99
-57
@@ -22,48 +22,10 @@ import (
|
||||
|
||||
var errInvalidSource = errors.New("invalid source image")
|
||||
|
||||
func thumbnail(r io.ReadSeeker, maxPixels int) ([]byte, int, int, error) {
|
||||
cfg, format, err := image.DecodeConfig(r)
|
||||
if err != nil ||
|
||||
(format != "png" && format != "jpeg") ||
|
||||
cfg.Width <= 0 ||
|
||||
cfg.Height <= 0 ||
|
||||
cfg.Width > maxPixels/cfg.Height {
|
||||
return nil, 0, 0, errInvalidSource
|
||||
}
|
||||
|
||||
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
|
||||
src, err := imaging.Decode(r, imaging.AutoOrientation(true))
|
||||
if err != nil {
|
||||
return nil, 0, 0, errInvalidSource
|
||||
}
|
||||
|
||||
width, height := src.Bounds().Dx(), src.Bounds().Dy()
|
||||
if width > 256 || height > 256 {
|
||||
if width >= height {
|
||||
height = max(1, height*256/width)
|
||||
width = 256
|
||||
} else {
|
||||
width = max(1, width*256/height)
|
||||
height = 256
|
||||
}
|
||||
|
||||
src = imaging.Resize(src, width, height, imaging.Lanczos)
|
||||
}
|
||||
|
||||
dst := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
draw.Draw(dst, dst.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
|
||||
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Over)
|
||||
|
||||
var out bytes.Buffer
|
||||
if err := jpeg.Encode(&out, dst, &jpeg.Options{Quality: 85}); err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
|
||||
return out.Bytes(), width, height, nil
|
||||
type imageVariant struct {
|
||||
name db.AttachmentVariantName
|
||||
data []byte
|
||||
width, height int
|
||||
}
|
||||
|
||||
func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAttachmentVariantParams, error) {
|
||||
@@ -114,7 +76,7 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAtt
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, width, height, err := thumbnail(tmp, w.maxPixels)
|
||||
images, err := imageVariants(tmp, w.maxPixels)
|
||||
<-w.decodeSlot
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -132,21 +94,101 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAtt
|
||||
return nil, err
|
||||
}
|
||||
|
||||
key := "variants/" + row.ID.String() + "/v" + strconv.Itoa(row.PipelineVersion) + "/thumbnail"
|
||||
if err := w.s3.Put(ctx, key, bytes.NewReader(data), int64(len(data)), "image/jpeg"); err != nil {
|
||||
variants := make([]db.CreateAttachmentVariantParams, 0, len(images))
|
||||
for _, img := range images {
|
||||
key := "variants/" + row.ID.String() + "/v" + strconv.Itoa(row.PipelineVersion) + "/" + string(img.name)
|
||||
if err := w.s3.Put(ctx, key, bytes.NewReader(img.data), int64(len(img.data)), "image/jpeg"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sum := sha256.Sum256(img.data)
|
||||
variants = append(variants, db.CreateAttachmentVariantParams{
|
||||
AttachmentID: row.ID,
|
||||
PipelineVersion: row.PipelineVersion,
|
||||
Variant: img.name,
|
||||
S3Key: key,
|
||||
ContentType: "image/jpeg",
|
||||
SizeBytes: len(img.data),
|
||||
Width: img.width,
|
||||
Height: img.height,
|
||||
Sha256: hex.EncodeToString(sum[:]),
|
||||
})
|
||||
}
|
||||
|
||||
return variants, nil
|
||||
}
|
||||
|
||||
func imageVariants(r io.ReadSeeker, maxPixels int) ([]imageVariant, error) {
|
||||
cfg, format, err := image.DecodeConfig(r)
|
||||
if err != nil ||
|
||||
(format != "png" && format != "jpeg") ||
|
||||
cfg.Width <= 0 ||
|
||||
cfg.Height <= 0 ||
|
||||
cfg.Width > maxPixels/cfg.Height {
|
||||
return nil, errInvalidSource
|
||||
}
|
||||
|
||||
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sum := sha256.Sum256(data)
|
||||
return []db.CreateAttachmentVariantParams{{
|
||||
AttachmentID: row.ID,
|
||||
PipelineVersion: row.PipelineVersion,
|
||||
Variant: db.AttachmentVariantNameThumbnail,
|
||||
S3Key: key,
|
||||
ContentType: "image/jpeg",
|
||||
SizeBytes: len(data),
|
||||
Width: width,
|
||||
Height: height,
|
||||
Sha256: hex.EncodeToString(sum[:]),
|
||||
}}, nil
|
||||
src, err := imaging.Decode(r, imaging.AutoOrientation(true))
|
||||
if err != nil {
|
||||
return nil, errInvalidSource
|
||||
}
|
||||
|
||||
thumb := thumbnail(src)
|
||||
crop := imaging.Fill(src, 128, 128, imaging.Center, imaging.Lanczos)
|
||||
variants := make([]imageVariant, 0, 2)
|
||||
for _, item := range []struct {
|
||||
name db.AttachmentVariantName
|
||||
image image.Image
|
||||
}{
|
||||
{db.AttachmentVariantNameThumbnail, thumb},
|
||||
{db.AttachmentVariantNameCrop, crop},
|
||||
} {
|
||||
data, err := encodeJPEG(item.image)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
variants = append(variants, imageVariant{
|
||||
name: item.name,
|
||||
data: data,
|
||||
width: item.image.Bounds().Dx(),
|
||||
height: item.image.Bounds().Dy(),
|
||||
})
|
||||
}
|
||||
|
||||
return variants, nil
|
||||
}
|
||||
|
||||
func thumbnail(src image.Image) image.Image {
|
||||
width, height := src.Bounds().Dx(), src.Bounds().Dy()
|
||||
if width > 256 || height > 256 {
|
||||
if width >= height {
|
||||
height = max(1, height*256/width)
|
||||
width = 256
|
||||
} else {
|
||||
width = max(1, width*256/height)
|
||||
height = 256
|
||||
}
|
||||
|
||||
return imaging.Resize(src, width, height, imaging.Lanczos)
|
||||
}
|
||||
|
||||
return src
|
||||
}
|
||||
|
||||
func encodeJPEG(src image.Image) ([]byte, error) {
|
||||
dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Dx(), src.Bounds().Dy()))
|
||||
draw.Draw(dst, dst.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
|
||||
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Over)
|
||||
|
||||
var out bytes.Buffer
|
||||
if err := jpeg.Encode(&out, dst, &jpeg.Options{Quality: 85}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user