forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify thumbnail resizing logic to ensure target constraints are re…
…spected (mattermost#28012) Automatic Merge
- Loading branch information
1 parent
8c5f00d
commit 70dc488
Showing
12 changed files
with
82 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
}) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.