2020-10-09 11:09:17 +00:00
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-10-22 15:59:01 +00:00
|
|
|
"errors"
|
2020-10-09 11:09:17 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-10-09 11:17:04 +00:00
|
|
|
func TestEncode(t *testing.T) {
|
2020-10-09 11:09:17 +00:00
|
|
|
cs := []struct {
|
|
|
|
FileName string
|
|
|
|
RenderSize int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"elephant.jpg",
|
|
|
|
1447,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"rose.webp",
|
|
|
|
11119,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"spin.gif",
|
|
|
|
2263,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"status.png",
|
|
|
|
5834,
|
|
|
|
},
|
|
|
|
}
|
2020-10-22 15:27:58 +00:00
|
|
|
options := EncodeConfig{
|
2020-10-09 11:09:17 +00:00
|
|
|
Quality: 70,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cs {
|
2020-10-09 11:17:04 +00:00
|
|
|
img, err := Decode(path + c.FileName)
|
2020-10-09 11:09:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bb := bytes.NewBuffer([]byte{})
|
2020-10-22 15:27:58 +00:00
|
|
|
err = Encode(bb, img, options)
|
2020-10-09 11:09:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Exactly(t, c.RenderSize, bb.Len())
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 15:59:01 +00:00
|
|
|
|
|
|
|
func TestEncodeToBestSize(t *testing.T) {
|
|
|
|
cs := []struct {
|
|
|
|
FileName string
|
|
|
|
RenderSize int
|
2020-10-27 14:42:42 +00:00
|
|
|
Error error
|
2020-10-22 15:59:01 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"elephant.jpg",
|
|
|
|
1467,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"rose.webp",
|
|
|
|
8513,
|
2022-09-05 14:02:10 +00:00
|
|
|
errors.New("image size after processing exceeds max, expected < '5632', received < '8513'"),
|
2020-10-22 15:59:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"spin.gif",
|
|
|
|
2407,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"status.png",
|
|
|
|
4725,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cs {
|
|
|
|
img, err := Decode(path + c.FileName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bb := bytes.NewBuffer([]byte{})
|
|
|
|
err = EncodeToBestSize(bb, img, ResizeDimensions[0])
|
|
|
|
|
|
|
|
require.Exactly(t, c.RenderSize, bb.Len())
|
|
|
|
|
|
|
|
if c.Error != nil {
|
|
|
|
require.EqualError(t, err, c.Error.Error())
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-02 13:59:52 +00:00
|
|
|
|
|
|
|
func TestCompressToFileLimits(t *testing.T) {
|
|
|
|
img, err := Decode(path + "IMG_1205.HEIC.jpg")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bb := bytes.NewBuffer([]byte{})
|
|
|
|
err = CompressToFileLimits(bb, img, FileSizeLimits{50000, 350000})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 291645, bb.Len())
|
|
|
|
}
|