Skip to content

Commit

Permalink
add ThumbnailMost method
Browse files Browse the repository at this point in the history
  • Loading branch information
matej-karolcik committed Apr 24, 2024
1 parent 20960ed commit 79dea6d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions imageref.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,17 @@ func (i *ImageRef) Thumbnail(width int) error {
return i.ref.Thumbnail(width, 0, vips.InterestingAll)
}

// ThumbnailMost makes a thumbnail of image, where the longer side is at most width pixels
func (i *ImageRef) ThumbnailMost(width int) error {
ratio := float64(i.Width()) / float64(i.Height())

if ratio > 1 {
return i.ref.Thumbnail(width, int(float64(width)/ratio), vips.InterestingAll)
}

return i.ref.Thumbnail(int(float64(width)*ratio), width, vips.InterestingAll)
}

func (i *ImageRef) Close() {
i.ref.Close()
}
Expand Down
42 changes: 42 additions & 0 deletions imageref_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package imageref

import (
"github.com/davidbyttow/govips/v2/vips"
"testing"
)

func TestImageRef_ThumbnailMost(t *testing.T) {
vipsImage, err := vips.Black(10, 5)
if err != nil {
t.Fatal(err)
}

vipsImageClone, err := vipsImage.Copy()
if err != nil {
t.Fatal(err)
}

imageRef := &ImageRef{ref: vipsImage}
err = imageRef.ThumbnailMost(8)
if err != nil {
t.Fatal(err)
}

if max(imageRef.Width(), imageRef.Height()) != 8 {
t.Fatalf("Expected thumbnail to be at most 8px, got %dx%d", imageRef.Width(), imageRef.Height())
}

err = vipsImageClone.Rotate(vips.Angle90)
if err != nil {
t.Fatal(err)
}

err = imageRef.ThumbnailMost(8)
if err != nil {
t.Fatal(err)
}

if max(imageRef.Width(), imageRef.Height()) != 8 {
t.Fatalf("Expected thumbnail to be at most 8px, got %dx%d", imageRef.Width(), imageRef.Height())
}
}

0 comments on commit 79dea6d

Please sign in to comment.