diff --git a/README.md b/README.md index 60d6c17..af0571b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# bimg [![Build Status](https://travis-ci.org/h2non/bimg.png)](https://travis-ci.org/h2non/bimg) [![GitHub release](http://img.shields.io/github/tag/h2non/bimg.svg?style=flat-square)](https://github.com/h2non/bimg/releases) [![GoDoc](https://godoc.org/github.com/h2non/bimg?status.svg)](https://godoc.org/github.com/h2non/bimg) +# bimg [![Build Status](https://travis-ci.org/h2non/bimg.png)](https://travis-ci.org/h2non/bimg) [![GoDoc](https://godoc.org/github.com/h2non/bimg?status.svg)](https://godoc.org/github.com/h2non/bimg) [![Go Report Card](http://goreportcard.com/badge/h2non/bimg)](http://goreportcard.com/report/h2non/bimg) Small [Go](http://golang.org) package for fast high-level image processing using [libvips](https://github.com/jcupitt/libvips) via C bindings, providing a simple, elegant and fluent [programmatic API](#examples). @@ -297,6 +297,21 @@ Enable libvips traces (note that a lot of data will be written in stdout): VIPS_TRACE=1 ./app ``` +You can also dump a core on failure, as [John Cuppit](https://github.com/jcupitt) said: +```c +g_log_set_always_fatal( + G_LOG_FLAG_RECURSION | + G_LOG_FLAG_FATAL | + G_LOG_LEVEL_ERROR | + G_LOG_LEVEL_CRITICAL | + G_LOG_LEVEL_WARNING ); +``` + +Or set the G_DEBUG environment variable: +``` +export G_DEBUG=fatal-warnings,fatal-criticals +``` + ## API See [godoc reference](https://godoc.org/github.com/h2non/bimg) for detailed API documentation. diff --git a/fixtures/vertical.jpg b/fixtures/vertical.jpg new file mode 100644 index 0000000..3e12e65 Binary files /dev/null and b/fixtures/vertical.jpg differ diff --git a/image.go b/image.go index 6b6e179..fae447e 100644 --- a/image.go +++ b/image.go @@ -1,5 +1,6 @@ package bimg +// Image encapsulates the whole image buffer type Image struct { buffer []byte } diff --git a/metadata.go b/metadata.go index c1e311a..9be6dac 100644 --- a/metadata.go +++ b/metadata.go @@ -6,11 +6,13 @@ package bimg */ import "C" +// ImageSize represents the image width and height values type ImageSize struct { Width int Height int } +// ImageMedatada represents the basic metadata fields type ImageMetadata struct { Orientation int Channels int diff --git a/resize_test.go b/resize_test.go index d57852c..7b4759a 100644 --- a/resize_test.go +++ b/resize_test.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "os" "path" + "strconv" "testing" ) @@ -28,27 +29,70 @@ func TestResize(t *testing.T) { Write("fixtures/test_out.jpg", newImg) } +func TestResizeVerticalImage(t *testing.T) { + tests := []struct { + format ImageType + options Options + }{ + {JPEG, Options{Width: 800, Height: 600}}, + {JPEG, Options{Width: 1000, Height: 1000}}, + {JPEG, Options{Width: 1000, Height: 1500}}, + {JPEG, Options{Width: 1000}}, + {JPEG, Options{Height: 1500}}, + {JPEG, Options{Width: 100, Height: 50}}, + {JPEG, Options{Width: 2000, Height: 2000}}, + {JPEG, Options{Width: 500, Height: 1000}}, + {JPEG, Options{Width: 500}}, + {JPEG, Options{Height: 500}}, + {JPEG, Options{Crop: true, Width: 500, Height: 1000}}, + {JPEG, Options{Crop: true, Enlarge: true, Width: 2000, Height: 1400}}, + {JPEG, Options{Enlarge: true, Force: true, Width: 2000, Height: 2000}}, + {JPEG, Options{Force: true, Width: 2000, Height: 2000}}, + } + + buf, _ := Read("fixtures/vertical.jpg") + for _, test := range tests { + image, err := Resize(buf, test.options) + if err != nil { + t.Errorf("Resize(imgData, %#v) error: %#v", test.options, err) + } + + if DetermineImageType(image) != test.format { + t.Fatal("Image format is invalid. Expected: %s", test.format) + } + + size, _ := Size(image) + if test.options.Height > 0 && size.Height != test.options.Height { + t.Fatalf("Invalid height: %d", size.Height) + } + if test.options.Width > 0 && size.Width != test.options.Width { + t.Fatalf("Invalid width: %d", size.Width) + } + + Write("fixtures/test_vertical_"+strconv.Itoa(test.options.Width)+"x"+strconv.Itoa(test.options.Height)+".jpg", image) + } +} + func TestResizeCustomSizes(t *testing.T) { tests := []struct { - file string format ImageType options Options }{ - {"test.jpg", JPEG, Options{Width: 800, Height: 600}}, - {"test.jpg", JPEG, Options{Width: 1000, Height: 1000}}, - {"test.jpg", JPEG, Options{Width: 100, Height: 50}}, - {"test.jpg", JPEG, Options{Width: 2000, Height: 2000}}, - {"test.jpg", JPEG, Options{Width: 500, Height: 1000}}, - {"test.jpg", JPEG, Options{Width: 500}}, - {"test.jpg", JPEG, Options{Height: 500}}, - {"test.jpg", JPEG, Options{Crop: true, Width: 500, Height: 1000}}, - {"test.jpg", JPEG, Options{Crop: true, Enlarge: true, Width: 2000, Height: 1400}}, - {"test.jpg", JPEG, Options{Enlarge: true, Force: true, Width: 2000, Height: 2000}}, - {"test.jpg", JPEG, Options{Force: true, Width: 2000, Height: 2000}}, + {JPEG, Options{Width: 800, Height: 600}}, + {JPEG, Options{Width: 1000, Height: 1000}}, + {JPEG, Options{Width: 100, Height: 50}}, + {JPEG, Options{Width: 2000, Height: 2000}}, + {JPEG, Options{Width: 500, Height: 1000}}, + {JPEG, Options{Width: 500}}, + {JPEG, Options{Height: 500}}, + {JPEG, Options{Crop: true, Width: 500, Height: 1000}}, + {JPEG, Options{Crop: true, Enlarge: true, Width: 2000, Height: 1400}}, + {JPEG, Options{Enlarge: true, Force: true, Width: 2000, Height: 2000}}, + {JPEG, Options{Force: true, Width: 2000, Height: 2000}}, } + buf, _ := Read("fixtures/test.jpg") for _, test := range tests { - buf, _ := Read("fixtures/" + test.file) image, err := Resize(buf, test.options) if err != nil { t.Errorf("Resize(imgData, %#v) error: %#v", test.options, err)