diff --git a/internal/images/image.go b/internal/images/image.go index 2ed222b..dd97610 100644 --- a/internal/images/image.go +++ b/internal/images/image.go @@ -6,6 +6,7 @@ import ( "image/jpeg" _ "image/jpeg" "path/filepath" + "slices" "strings" "github.com/disintegration/imaging" @@ -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) { diff --git a/internal/images/image_test.go b/internal/images/image_test.go index 7e280fc..79fa437 100644 --- a/internal/images/image_test.go +++ b/internal/images/image_test.go @@ -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) { @@ -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) +} diff --git a/internal/testdata/testdata.go b/internal/testdata/testdata.go index 3924948..c553e4d 100644 --- a/internal/testdata/testdata.go +++ b/internal/testdata/testdata.go @@ -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" +) diff --git a/testdata/png/test.png b/testdata/png/test.png new file mode 100644 index 0000000..6230df1 Binary files /dev/null and b/testdata/png/test.png differ