diff --git a/server/channels/app/file_test.go b/server/channels/app/file_test.go index fdda2d7ce06d..e03ce4175087 100644 --- a/server/channels/app/file_test.go +++ b/server/channels/app/file_test.go @@ -396,7 +396,7 @@ func TestGenerateThumbnailImage(t *testing.T) { // then outputImage, err := os.Stat(thumbnailPath) assert.NoError(t, err) - assert.Equal(t, int64(957), outputImage.Size()) + assert.Equal(t, int64(721), outputImage.Size()) }) } diff --git a/server/channels/app/imaging/preview.go b/server/channels/app/imaging/preview.go index 0159921c79a3..fd61d06bcda7 100644 --- a/server/channels/app/imaging/preview.go +++ b/server/channels/app/imaging/preview.go @@ -26,32 +26,15 @@ func GeneratePreview(img image.Image, width int) image.Image { // GenerateThumbnail generates the thumbnail for the given image. func GenerateThumbnail(img image.Image, targetWidth, targetHeight int) image.Image { - thumb := img width := img.Bounds().Dx() height := img.Bounds().Dy() - expectedRatio := float64(targetHeight) / float64(targetWidth) - // If both dimensions are over the target size, we scale down until one is not. - // If one dimension is over the target size, we scale down until both are not. - // If neither dimension is over the target size, we return the image as is. - if height > targetHeight || width > targetWidth { - ratio := float64(height) / float64(width) - if ratio < expectedRatio { - if height >= targetHeight { - thumb = imaging.Resize(img, 0, targetHeight, imaging.Lanczos) - } else { - thumb = imaging.Resize(img, targetWidth, 0, imaging.Lanczos) - } - } else { - if width >= targetWidth { - thumb = imaging.Resize(img, targetWidth, 0, imaging.Lanczos) - } else { - thumb = imaging.Resize(img, 0, targetHeight, imaging.Lanczos) - } - } + // We keep aspect ratio and ensure the output dimensions are never higher than the provided targets. + if width > height { + return imaging.Resize(img, targetWidth, 0, imaging.Lanczos) } - return thumb + return imaging.Resize(img, 0, targetHeight, imaging.Lanczos) } // GenerateMiniPreviewImage generates the mini preview for the given image. diff --git a/server/channels/app/imaging/preview_test.go b/server/channels/app/imaging/preview_test.go new file mode 100644 index 000000000000..68de50223ac1 --- /dev/null +++ b/server/channels/app/imaging/preview_test.go @@ -0,0 +1,77 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package imaging + +import ( + "image" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGenerateThumbnail(t *testing.T) { + tcs := []struct { + name string + inputImg image.Image + targetWidth int + targetHeight int + expectedWidth int + expectedHeight int + }{ + { + name: "empty image", + inputImg: image.NewRGBA(image.Rect(0, 0, 0, 0)), + targetWidth: 120, + targetHeight: 100, + }, + { + name: "both dimensions lower than targets", + inputImg: image.NewRGBA(image.Rect(0, 0, 100, 50)), + targetWidth: 120, + targetHeight: 100, + expectedWidth: 120, + expectedHeight: 60, + }, + { + name: "both dimensions equal to targets", + inputImg: image.NewRGBA(image.Rect(0, 0, 120, 100)), + targetWidth: 120, + targetHeight: 100, + expectedWidth: 120, + expectedHeight: 100, + }, + { + name: "both dimensions higher than targets", + inputImg: image.NewRGBA(image.Rect(0, 0, 1000, 500)), + targetWidth: 120, + targetHeight: 100, + expectedWidth: 120, + expectedHeight: 60, + }, + { + name: "width higher than target", + inputImg: image.NewRGBA(image.Rect(0, 0, 200, 100)), + targetWidth: 120, + targetHeight: 100, + expectedWidth: 120, + expectedHeight: 60, + }, + { + name: "height higher than target", + inputImg: image.NewRGBA(image.Rect(0, 0, 100, 200)), + targetWidth: 120, + targetHeight: 100, + expectedWidth: 50, + expectedHeight: 100, + }, + } + + for _, tc := range tcs { + t.Run(tc.name, func(t *testing.T) { + thumb := GenerateThumbnail(tc.inputImg, tc.targetWidth, tc.targetHeight) + require.Equal(t, tc.expectedWidth, thumb.Bounds().Dx(), "expectedWidth") + require.Equal(t, tc.expectedHeight, thumb.Bounds().Dy(), "expectedHeight") + }) + } +} diff --git a/server/tests/orientation_test_1_expected_thumb.jpeg b/server/tests/orientation_test_1_expected_thumb.jpeg index 787d02a9716e..52e9656520b1 100644 Binary files a/server/tests/orientation_test_1_expected_thumb.jpeg and b/server/tests/orientation_test_1_expected_thumb.jpeg differ diff --git a/server/tests/orientation_test_2_expected_thumb.jpeg b/server/tests/orientation_test_2_expected_thumb.jpeg index 480098e2f400..3ed1010dc91c 100644 Binary files a/server/tests/orientation_test_2_expected_thumb.jpeg and b/server/tests/orientation_test_2_expected_thumb.jpeg differ diff --git a/server/tests/orientation_test_3_expected_thumb.jpeg b/server/tests/orientation_test_3_expected_thumb.jpeg index aada2f26aa83..22f00c0f9797 100644 Binary files a/server/tests/orientation_test_3_expected_thumb.jpeg and b/server/tests/orientation_test_3_expected_thumb.jpeg differ diff --git a/server/tests/orientation_test_4_expected_thumb.jpeg b/server/tests/orientation_test_4_expected_thumb.jpeg index 1367c9eaa7ea..1523bca93bc3 100644 Binary files a/server/tests/orientation_test_4_expected_thumb.jpeg and b/server/tests/orientation_test_4_expected_thumb.jpeg differ diff --git a/server/tests/orientation_test_5_expected_thumb.jpeg b/server/tests/orientation_test_5_expected_thumb.jpeg index 159d55d52c6d..d65bd9c5b013 100644 Binary files a/server/tests/orientation_test_5_expected_thumb.jpeg and b/server/tests/orientation_test_5_expected_thumb.jpeg differ diff --git a/server/tests/orientation_test_6_expected_thumb.jpeg b/server/tests/orientation_test_6_expected_thumb.jpeg index 2aa6b6727b39..4af5241aaf33 100644 Binary files a/server/tests/orientation_test_6_expected_thumb.jpeg and b/server/tests/orientation_test_6_expected_thumb.jpeg differ diff --git a/server/tests/orientation_test_7_expected_thumb.jpeg b/server/tests/orientation_test_7_expected_thumb.jpeg index 453402e8c3db..00f63dc42b7e 100644 Binary files a/server/tests/orientation_test_7_expected_thumb.jpeg and b/server/tests/orientation_test_7_expected_thumb.jpeg differ diff --git a/server/tests/orientation_test_8_expected_thumb.jpeg b/server/tests/orientation_test_8_expected_thumb.jpeg index 78f156c416b8..a583d0b334e6 100644 Binary files a/server/tests/orientation_test_8_expected_thumb.jpeg and b/server/tests/orientation_test_8_expected_thumb.jpeg differ diff --git a/server/tests/test_expected_tiff_thumb.jpeg b/server/tests/test_expected_tiff_thumb.jpeg index 36d4ab758b02..313f483b0a5b 100644 Binary files a/server/tests/test_expected_tiff_thumb.jpeg and b/server/tests/test_expected_tiff_thumb.jpeg differ