github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/toolkit/imgutils/imgutils_test.go (about)

     1  package imgutils_test
     2  
     3  import (
     4  	"github.com/unionj-cloud/go-doudou/toolkit/imgutils"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  const testDir = "testdata"
    11  
    12  func TestResizeKeepAspectRatioPng(t *testing.T) {
    13  	file, err := os.Open(filepath.Join(testDir, "test.png"))
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  	defer file.Close()
    18  	_, err = imgutils.ResizeKeepAspectRatio(file, 0.5, filepath.Join(testDir, "test_result"))
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  }
    23  
    24  func TestResizeKeepAspectRatioJpeg(t *testing.T) {
    25  	file, err := os.Open(filepath.Join(testDir, "test.jpg"))
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  	defer file.Close()
    30  	_, err = imgutils.ResizeKeepAspectRatio(file, 0.5, filepath.Join(testDir, "test_result.jpg"))
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  }
    35  
    36  func TestResizeKeepAspectRatioGif1(t *testing.T) {
    37  	file, err := os.Open(filepath.Join(testDir, "rgb.gif"))
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	defer file.Close()
    42  	_, err = imgutils.ResizeKeepAspectRatio(file, 0.5, filepath.Join(testDir, "rgb_result"))
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  }
    47  
    48  func TestResizeKeepAspectRatioGif2(t *testing.T) {
    49  	file, err := os.Open(filepath.Join(testDir, "test.gif"))
    50  	if err != nil {
    51  		panic(err)
    52  	}
    53  	defer file.Close()
    54  	_, err = imgutils.ResizeKeepAspectRatio(file, 1, filepath.Join(testDir, "test_result"))
    55  	if err != nil {
    56  		panic(err)
    57  	}
    58  }
    59  
    60  func TestResizeKeepAspectRatioGif3(t *testing.T) {
    61  	file, err := os.Open(filepath.Join(testDir, "test.gif"))
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	defer file.Close()
    66  	_, err = imgutils.ResizeKeepAspectRatio(file, 0.5, filepath.Join(testDir, "test_result"))
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  }