Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
71 changes: 71 additions & 0 deletions images/thumbnail.go
Original file line number Diff line number Diff line change
@@ -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
}
72 changes: 72 additions & 0 deletions images/thumbnail_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading