Skip to content

Commit

Permalink
Merge pull request #4 from Arimeka/fix-parse-exif
Browse files Browse the repository at this point in the history
FIX: Don't fail when parse image without exif data
  • Loading branch information
Arimeka committed Apr 13, 2019
2 parents 37d175e + ec99cc2 commit c815135
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
Binary file added fixtures/without-exif.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/without-exif.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 7 additions & 13 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,16 @@ func (info *Info) ParseImage() error {
return err
}

x, err := exif.Decode(file)
if err != nil {
return err
}

info.Width = img.Width
info.Height = img.Height

if orientationTag, err := x.Get(exif.Orientation); err == nil {
switch orientationTag.String() {
case "5", "6", "7", "8":
info.Width = img.Height
info.Height = img.Width
default:
info.Width = img.Width
info.Height = img.Height
if x, err := exif.Decode(file); err == nil {
if orientationTag, err := x.Get(exif.Orientation); err == nil {
switch orientationTag.String() {
case "5", "6", "7", "8":
info.Width = img.Height
info.Height = img.Width
}
}
}

Expand Down
34 changes: 31 additions & 3 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
)

const (
testImageValidImage = "./fixtures/image.jpeg"
testImageWithExifOrientation = "./fixtures/left.jpg"
testImageInvalidImage = "./fixtures/not-an-image.jpeg"
testImageValidImage = "./fixtures/image.jpeg"
testImageWithExifOrientation = "./fixtures/left.jpg"
testImageInvalidImage = "./fixtures/not-an-image.jpeg"
testImageJPEGWithoutExifImage = "./fixtures/without-exif.jpg"
testImagePNGWithoutExifImage = "./fixtures/without-exif.png"
)

func TestInfo_ParseImageNotFound(t *testing.T) {
Expand Down Expand Up @@ -51,3 +53,29 @@ func TestInfo_ParseImageWithOrientation(t *testing.T) {
t.Errorf("Filename: %s. Not expected width. Expected %d; got %d", testImageWithExifOrientation, 330, width)
}
}

func TestInfo_ParseImageJPEGWithoutExif(t *testing.T) {
info, _ := mediaprobe.New(testImageJPEGWithoutExifImage)
err := info.ParseImage()
if err != nil {
t.Errorf("Filename: %s. Unexpected error %v", testImageJPEGWithoutExifImage, err)
}

width := info.Width
if width != 200 {
t.Errorf("Filename: %s. Not expected width. Expected %d; got %d", testImageJPEGWithoutExifImage, 200, width)
}
}

func TestInfo_ParseImagePNGWithoutExif(t *testing.T) {
info, _ := mediaprobe.New(testImagePNGWithoutExifImage)
err := info.ParseImage()
if err != nil {
t.Errorf("Filename: %s. Unexpected error %v", testImagePNGWithoutExifImage, err)
}

width := info.Width
if width != 100 {
t.Errorf("Filename: %s. Not expected width. Expected %d; got %d", testImagePNGWithoutExifImage, 100, width)
}
}

0 comments on commit c815135

Please sign in to comment.