Skip to content

Commit

Permalink
add GetImageSize and GetImageSizeFromString
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperjiang committed Jul 23, 2019
1 parent 0d52f59 commit f193a82
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
Binary file added fish.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions image.go
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
}
104 changes: 104 additions & 0 deletions image_test.go
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)
}
})
}
}

0 comments on commit f193a82

Please sign in to comment.