diff --git a/README.md b/README.md index 754ee61..96ad022 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,13 @@ This package implements some PHP functions by Golang. Please note that it's impo | [file_get_contents](https://www.php.net/manual/en/function.file-get-contents.php) | [FileGetContents](https://godoc.org/github.com/hyperjiang/php#FileGetContents) | | [file_put_contents](https://www.php.net/manual/en/function.file-put-contents.php) | [FilePutContents](https://godoc.org/github.com/hyperjiang/php#FilePutContents) | +### Image Functions + +| PHP function | Golang function | +| ---------------------------------------------------- | ---------------------------------------------------------- | +| [getimagesize](https://www.php.net/manual/en/function.getimagesize.php) | [GetImageSize](https://godoc.org/github.com/hyperjiang/php#GetImageSize) | +| [getimagesizefromstring](https://www.php.net/manual/en/function.getimagesizefromstring.php) | [GetImageSizeFromString](https://godoc.org/github.com/hyperjiang/php#GetImageSizeFromString) | + ### Network Functions | PHP function | Golang function | diff --git a/fish.jpg b/fish.jpg new file mode 100644 index 0000000..7496933 Binary files /dev/null and b/fish.jpg differ diff --git a/image.go b/image.go new file mode 100644 index 0000000..b6c4597 --- /dev/null +++ b/image.go @@ -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 +} diff --git a/image_test.go b/image_test.go new file mode 100644 index 0000000..c3a09ae --- /dev/null +++ b/image_test.go @@ -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) + } + }) + } +}