Skip to content

Commit

Permalink
feat: #43 Add PNG support
Browse files Browse the repository at this point in the history
  • Loading branch information
waynezhang committed Nov 25, 2024
1 parent 43862f5 commit eb850eb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
6 changes: 4 additions & 2 deletions internal/images/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"image/jpeg"
_ "image/jpeg"
"path/filepath"
"slices"
"strings"

"github.com/disintegration/imaging"
Expand All @@ -20,8 +21,9 @@ type ImageSize struct {
}

func IsPhotoSupported(path string) bool {
lowerExt := strings.ToLower(filepath.Ext(path))
return lowerExt == ".jpeg" || lowerExt == ".jpg" || lowerExt == ".webp"
return slices.Contains(
[]string{".jpeg", ".jpg", ".webp", ".png"},
strings.ToLower(filepath.Ext(path)))
}

func GetPhotoSize(path string) (*ImageSize, error) {
Expand Down
24 changes: 23 additions & 1 deletion internal/images/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func TestPhotoSupport(t *testing.T) {
assert.True(t, IsPhotoSupported("photo.jpg"))
assert.True(t, IsPhotoSupported("photo.jpeg"))
assert.True(t, IsPhotoSupported("photo.webp"))
assert.False(t, IsPhotoSupported("photo.png"))
assert.True(t, IsPhotoSupported("photo.png"))
assert.False(t, IsPhotoSupported("photo.xxx"))
}

func TestGetPhotoSize(t *testing.T) {
Expand Down Expand Up @@ -101,3 +102,24 @@ func TestWebpSupport(t *testing.T) {
checksum, _ := files.Checksum(path)
assert.Equal(t, testdata.WebpExpectedThubmnailChecksum, *checksum)
}

func TestPngSupport(t *testing.T) {
tmp, err := os.MkdirTemp("", "foto-test")
assert.Nil(t, err)

size, err := GetPhotoSize(testdata.PngTestFile)
assert.Equal(t, testdata.PngTestfileWidth, size.Width)
assert.Equal(t, testdata.PngTestfileHeight, size.Height)

path := filepath.Join(tmp, "resized.png")

err = ResizeImage(testdata.PngTestFile, path, testdata.PngThumbnailWidth, testdata.CompressQuality)
assert.Nil(t, err)

size, err = GetPhotoSize(path)
assert.Equal(t, testdata.PngThumbnailWidth, size.Width)
assert.Equal(t, testdata.PngThumbnailHeight, size.Height)

checksum, _ := files.Checksum(path)
assert.Equal(t, testdata.PngExpectedThubmnailChecksum, *checksum)
}
10 changes: 10 additions & 0 deletions internal/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,13 @@ var (
WebpThumbnailHeight = 480
WebpExpectedThubmnailChecksum = "5a5f8fcae2e37e504d6e062ee8adefac45ec9102a410faa16d4a0278b310b0c8"
)

var (
PngTestFile = "../../testdata/png/test.png"
PngTestfileWidth = 1024
PngTestfileHeight = 768

PngThumbnailWidth = 640
PngThumbnailHeight = 480
PngExpectedThubmnailChecksum = "c52322af93c85de909aa5774f58ab2b26ad13f95e405f4ce42703468f3490ab3"
)
Binary file added testdata/png/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eb850eb

Please sign in to comment.