-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add GetImageSize and GetImageSizeFromString
- Loading branch information
1 parent
0d52f59
commit f193a82
Showing
4 changed files
with
171 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package php | ||
|
||
import ( | ||
"bytes" | ||
"image" | ||
_ "image/gif" // gif format | ||
_ "image/jpeg" // jpeg format | ||
_ "image/png" // png format | ||
"mime" | ||
"os" | ||
) | ||
|
||
// ImageInfo stores the info of an image | ||
type ImageInfo struct { | ||
Width int | ||
Height int | ||
Format string | ||
Mime string | ||
} | ||
|
||
// GetImageSize gets the size of an image | ||
func GetImageSize(filename string) (ImageInfo, error) { | ||
|
||
var info ImageInfo | ||
|
||
file, err := os.Open(filename) | ||
defer file.Close() | ||
if err != nil { | ||
return info, err | ||
} | ||
|
||
cfg, format, err := image.DecodeConfig(file) | ||
if err != nil { | ||
return info, err | ||
} | ||
|
||
info.Width = cfg.Width | ||
info.Height = cfg.Height | ||
info.Format = format | ||
info.Mime = mime.TypeByExtension("." + format) | ||
|
||
return info, nil | ||
} | ||
|
||
// GetImageSizeFromString gets the size of an image from a string | ||
func GetImageSizeFromString(data []byte) (ImageInfo, error) { | ||
var info ImageInfo | ||
|
||
cfg, format, err := image.DecodeConfig(bytes.NewReader(data)) | ||
if err != nil { | ||
return info, err | ||
} | ||
|
||
info.Width = cfg.Width | ||
info.Height = cfg.Height | ||
info.Format = format | ||
info.Mime = mime.TypeByExtension("." + format) | ||
|
||
return info, nil | ||
} |
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,104 @@ | ||
package php | ||
|
||
import ( | ||
_ "image/gif" | ||
_ "image/jpeg" | ||
_ "image/png" | ||
"io/ioutil" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetimagesize(t *testing.T) { | ||
type args struct { | ||
filename string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want ImageInfo | ||
wantErr bool | ||
}{ | ||
{ | ||
"happy flow", | ||
args{"./fish.jpg"}, | ||
ImageInfo{ | ||
Width: 559, | ||
Height: 533, | ||
Format: "jpeg", | ||
Mime: "image/jpeg", | ||
}, | ||
false, | ||
}, | ||
{ | ||
"no such file", | ||
args{"./not-exist-file"}, | ||
ImageInfo{}, | ||
true, | ||
}, | ||
{ | ||
"unknown format", | ||
args{"./LICENSE"}, | ||
ImageInfo{}, | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GetImageSize(tt.args.filename) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetImageSize() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetImageSize() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetImageSizeFromString(t *testing.T) { | ||
|
||
data1, _ := ioutil.ReadFile("./fish.jpg") | ||
data2, _ := ioutil.ReadFile("./LICENSE") | ||
|
||
type args struct { | ||
data []byte | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want ImageInfo | ||
wantErr bool | ||
}{ | ||
{ | ||
"happy flow", | ||
args{data1}, | ||
ImageInfo{ | ||
Width: 559, | ||
Height: 533, | ||
Format: "jpeg", | ||
Mime: "image/jpeg", | ||
}, | ||
false, | ||
}, | ||
{ | ||
"unknown format", | ||
args{data2}, | ||
ImageInfo{}, | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GetImageSizeFromString(tt.args.data) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetImageSizeFromString() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetImageSizeFromString() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |