You've already forked top-wallpaper
hmmm
This commit is contained in:
@@ -2,6 +2,7 @@ package resize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/nfnt/resize"
|
||||
i "image"
|
||||
"image/jpeg"
|
||||
@@ -9,6 +10,10 @@ import (
|
||||
)
|
||||
|
||||
func Shrink(image string, maxSize uint, quality int) (string, error) {
|
||||
if quality < 1 || quality > 100 {
|
||||
return "", fmt.Errorf("quality must be between 1 and 100, got %d", quality)
|
||||
}
|
||||
|
||||
img, _, err := i.Decode(bytes.NewReader([]byte(image)))
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
48
resize/resize_test.go
Normal file
48
resize/resize_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package resize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func createTestImage() []byte {
|
||||
img := image.NewRGBA(image.Rect(0, 0, 100, 100))
|
||||
for x := 0; x < 100; x++ {
|
||||
for y := 0; y < 100; y++ {
|
||||
img.Set(x, y, color.RGBA{uint8(x), uint8(y), 255, 255})
|
||||
}
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
_ = jpeg.Encode(&buf, img, nil)
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestShrink_Success(t *testing.T) {
|
||||
testImage := createTestImage()
|
||||
result, err := Shrink(string(testImage), 50, 80)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if len(result) == 0 {
|
||||
t.Fatalf("expected non-empty result, got empty string")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShrink_InvalidImage(t *testing.T) {
|
||||
invalidImage := "not-an-image"
|
||||
_, err := Shrink(invalidImage, 50, 80)
|
||||
if err == nil {
|
||||
t.Fatalf("expected an error for invalid image input, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShrink_InvalidQuality(t *testing.T) {
|
||||
testImage := createTestImage()
|
||||
_, err := Shrink(string(testImage), 50, -10)
|
||||
if err == nil {
|
||||
t.Fatalf("expected an error for invalid quality, got nil")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user