From 5c65f51ca09614c83a7278fde620f3c79c22f500 Mon Sep 17 00:00:00 2001 From: Arjen de Vos Date: Tue, 22 Sep 2026 10:39:07 +0200 Subject: [PATCH] feat(images): make a thumbnail of an image MakeThumbnail scales an image down to a longest side, keeping its proportions, png when the source is png so transparency survives and jpeg otherwise. Scaling is golang.org/x/image's Catmull-Rom. --- go.mod | 5 ++- go.sum | 2 ++ images/thumbnail.go | 71 +++++++++++++++++++++++++++++++++++++++ images/thumbnail_test.go | 72 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 images/thumbnail.go create mode 100644 images/thumbnail_test.go diff --git a/go.mod b/go.mod index 9660341..9eef089 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/gonstruct/providers -go 1.22 - -toolchain go1.24.4 +go 1.26.0 require ( github.com/aws/aws-sdk-go-v2 v1.38.1 @@ -12,6 +10,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/sesv2 v1.52.1 github.com/cubewise-code/go-mime v0.0.0-20200519001935-8c5762b177d8 github.com/google/uuid v1.6.0 + golang.org/x/image v0.46.0 gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df ) diff --git a/go.sum b/go.sum index 75f19bd..aedf59c 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,8 @@ github.com/cubewise-code/go-mime v0.0.0-20200519001935-8c5762b177d8 h1:Z9lwXumT5 github.com/cubewise-code/go-mime v0.0.0-20200519001935-8c5762b177d8/go.mod h1:4abs/jPXcmJzYoYGF91JF9Uq9s/KL5n1jvFDix8KcqY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +golang.org/x/image v0.46.0 h1:b1+oYj0Jbp6K5MDT4i4/eZpYlk3V8SJhhDKh6LBHAyQ= +golang.org/x/image v0.46.0/go.mod h1:3B3W05VGVQyuXucLINLjXKrqISASfi4Xj+iCVkLMwew= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= diff --git a/images/thumbnail.go b/images/thumbnail.go new file mode 100644 index 0000000..2c350d0 --- /dev/null +++ b/images/thumbnail.go @@ -0,0 +1,71 @@ +// Package images is what an application does to pictures: small copies +// for lists, made once where the file is stored. +package images + +import ( + "bytes" + "errors" + "image" + _ "image/gif" // a format a thumbnail can be read from + "image/jpeg" + "image/png" + "io" + + "golang.org/x/image/draw" +) + +// ErrNotAnImage is a source that no known image format decodes. +var ErrNotAnImage = errors.New("images: not an image") + +// jpegQuality is what a thumbnail is encoded at: small, and no artefact +// shows at thumbnail size. +const jpegQuality = 82 + +// Thumbnail is a small copy of an image. +type Thumbnail struct { + Bytes []byte + Width int + Height int + // Extension says what the bytes are: ".png" when the source was png, + // so transparency survives, ".jpg" otherwise. + Extension string +} + +// MakeThumbnail scales an image down so its longest side is at most +// `longest` pixels, keeping its proportions. An image already small enough +// comes back re-encoded at its own size. +func MakeThumbnail(source io.Reader, longest int) (*Thumbnail, error) { + decoded, format, err := image.Decode(source) + if err != nil { + return nil, errors.Join(ErrNotAnImage, err) + } + + bounds := decoded.Bounds() + + width, height := bounds.Dx(), bounds.Dy() + if width > longest || height > longest { + if width >= height { + width, height = longest, max(1, height*longest/bounds.Dx()) + } else { + width, height = max(1, width*longest/bounds.Dy()), longest + } + } + + scaled := image.NewRGBA(image.Rect(0, 0, width, height)) + draw.CatmullRom.Scale(scaled, scaled.Bounds(), decoded, bounds, draw.Over, nil) + + buffer := new(bytes.Buffer) + if format == "png" { + if err := png.Encode(buffer, scaled); err != nil { + return nil, err + } + + return &Thumbnail{Bytes: buffer.Bytes(), Width: width, Height: height, Extension: ".png"}, nil + } + + if err := jpeg.Encode(buffer, scaled, &jpeg.Options{Quality: jpegQuality}); err != nil { + return nil, err + } + + return &Thumbnail{Bytes: buffer.Bytes(), Width: width, Height: height, Extension: ".jpg"}, nil +} diff --git a/images/thumbnail_test.go b/images/thumbnail_test.go new file mode 100644 index 0000000..e0c9c11 --- /dev/null +++ b/images/thumbnail_test.go @@ -0,0 +1,72 @@ +package images_test + +import ( + "bytes" + "errors" + "image" + "image/color" + "image/jpeg" + "image/png" + "testing" + + "github.com/gonstruct/providers/images" +) + +func TestMakeThumbnailShrinksAWidePNG(t *testing.T) { + source := image.NewRGBA(image.Rect(0, 0, 640, 400)) + + for y := range 400 { + for x := range 640 { + source.Set(x, y, color.RGBA{R: 200, G: 40, B: 40, A: 255}) + } + } + + encoded := new(bytes.Buffer) + if err := png.Encode(encoded, source); err != nil { + t.Fatal(err) + } + + thumbnail, err := images.MakeThumbnail(encoded, 320) + if err != nil || thumbnail.Extension != ".png" || thumbnail.Width != 320 || thumbnail.Height != 200 { + t.Fatalf("expected a 320 by 200 png, got %+v and %v", thumbnail, err) + } + + decoded, err := png.Decode(bytes.NewReader(thumbnail.Bytes)) + if err != nil { + t.Fatal(err) + } + + if r, _, _, _ := decoded.At(100, 100).RGBA(); r>>8 != 200 { + t.Fatalf("expected the red to survive, got %d", r>>8) + } +} + +func TestMakeThumbnailShrinksATallJPEGByHeight(t *testing.T) { + tall := new(bytes.Buffer) + if err := jpeg.Encode(tall, image.NewRGBA(image.Rect(0, 0, 300, 900)), nil); err != nil { + t.Fatal(err) + } + + thumbnail, err := images.MakeThumbnail(tall, 320) + if err != nil || thumbnail.Extension != ".jpg" || thumbnail.Width != 106 || thumbnail.Height != 320 { + t.Fatalf("expected a 106 by 320 jpeg, got %+v and %v", thumbnail, err) + } +} + +func TestMakeThumbnailKeepsASmallImageItsSize(t *testing.T) { + small := new(bytes.Buffer) + if err := png.Encode(small, image.NewRGBA(image.Rect(0, 0, 12, 8))); err != nil { + t.Fatal(err) + } + + thumbnail, err := images.MakeThumbnail(small, 320) + if err != nil || thumbnail.Width != 12 || thumbnail.Height != 8 { + t.Fatalf("expected 12 by 8, got %+v and %v", thumbnail, err) + } +} + +func TestMakeThumbnailRefusesWhatIsNotAnImage(t *testing.T) { + if _, err := images.MakeThumbnail(bytes.NewReader([]byte("png-one")), 320); !errors.Is(err, images.ErrNotAnImage) { + t.Fatalf("expected ErrNotAnImage, got %v", err) + } +}