fix(image): tests

This commit is contained in:
Tomas Aparicio 2015-04-07 23:53:48 +02:00
parent abd6679f58
commit 4330593138
4 changed files with 36 additions and 24 deletions

View file

@ -9,6 +9,7 @@ func (i *Image) Resize(width, height int) ([]byte, error) {
options := Options{ options := Options{
Width: width, Width: width,
Height: height, Height: height,
Embed: true,
} }
return i.Process(options) return i.Process(options)
} }

View file

@ -1,6 +1,7 @@
package bimg package bimg
import ( import (
"fmt"
"path" "path"
"testing" "testing"
) )
@ -11,8 +12,9 @@ func TestImageResize(t *testing.T) {
t.Errorf("Cannot process the image: %#v", err) t.Errorf("Cannot process the image: %#v", err)
} }
if assertSize(buf, 300, 240) { err = assertSize(buf, 300, 240)
t.Error("Invalid image size") if err != nil {
t.Error(err)
} }
Write("fixtures/test_resize_out.jpg", buf) Write("fixtures/test_resize_out.jpg", buf)
@ -24,21 +26,23 @@ func TestImageExtract(t *testing.T) {
t.Errorf("Cannot process the image: %#v", err) t.Errorf("Cannot process the image: %#v", err)
} }
if assertSize(buf, 300, 300) { err = assertSize(buf, 300, 300)
t.Error("Invalid image size") if err != nil {
t.Error(err)
} }
Write("fixtures/test_extract_out.jpg", buf) Write("fixtures/test_extract_out.jpg", buf)
} }
func TestImageEnlarge(t *testing.T) { func TestImageEnlarge(t *testing.T) {
buf, err := initImage("test.png").Enlarge(500, 380) buf, err := initImage("test.png").Enlarge(500, 375)
if err != nil { if err != nil {
t.Errorf("Cannot process the image: %#v", err) t.Errorf("Cannot process the image: %#v", err)
} }
if assertSize(buf, 500, 380) { err = assertSize(buf, 500, 375)
t.Error("Invalid image size") if err != nil {
t.Error(err)
} }
Write("fixtures/test_enlarge_out.jpg", buf) Write("fixtures/test_enlarge_out.jpg", buf)
@ -50,8 +54,9 @@ func TestImageCrop(t *testing.T) {
t.Errorf("Cannot process the image: %#v", err) t.Errorf("Cannot process the image: %#v", err)
} }
if assertSize(buf, 800, 600) { err = assertSize(buf, 800, 600)
t.Error("Invalid image size") if err != nil {
t.Error(err)
} }
Write("fixtures/test_crop_out.jpg", buf) Write("fixtures/test_crop_out.jpg", buf)
@ -63,8 +68,9 @@ func TestImageCropByWidth(t *testing.T) {
t.Errorf("Cannot process the image: %#v", err) t.Errorf("Cannot process the image: %#v", err)
} }
if assertSize(buf, 600, 375) { err = assertSize(buf, 600, 375)
t.Error("Invalid image size") if err != nil {
t.Error(err)
} }
Write("fixtures/test_crop_width_out.jpg", buf) Write("fixtures/test_crop_width_out.jpg", buf)
@ -76,8 +82,9 @@ func TestImageCropByHeight(t *testing.T) {
t.Errorf("Cannot process the image: %#v", err) t.Errorf("Cannot process the image: %#v", err)
} }
if assertSize(buf, 800, 480) { err = assertSize(buf, 480, 300)
t.Error("Invalid image size") if err != nil {
t.Error(err)
} }
Write("fixtures/test_crop_height_out.jpg", buf) Write("fixtures/test_crop_height_out.jpg", buf)
@ -89,8 +96,9 @@ func TestImageThumbnail(t *testing.T) {
t.Errorf("Cannot process the image: %#v", err) t.Errorf("Cannot process the image: %#v", err)
} }
if assertSize(buf, 100, 100) { err = assertSize(buf, 100, 100)
t.Error("Invalid image size") if err != nil {
t.Error(err)
} }
Write("fixtures/test_thumbnail_out.jpg", buf) Write("fixtures/test_thumbnail_out.jpg", buf)
@ -141,13 +149,13 @@ func initImage(file string) *Image {
return NewImage(buf) return NewImage(buf)
} }
func assertSize(buf []byte, width, height int) bool { func assertSize(buf []byte, width, height int) error {
size, err := NewImage(buf).Size() size, err := NewImage(buf).Size()
if err != nil { if err != nil {
return false return err
} }
if size.Width != 220 || size.Height != 300 { if size.Width != width || size.Height != height {
return false return fmt.Errorf("Invalid image size: %dx%d", size.Width, size.Height)
} }
return true return nil
} }

View file

@ -62,12 +62,12 @@ type Options struct {
AreaWidth int AreaWidth int
Top int Top int
Left int Left int
Crop bool
Enlarge bool
Extend int Extend int
Embed bool
Quality int Quality int
Compression int Compression int
Crop bool
Enlarge bool
Embed bool
Type ImageType Type ImageType
Rotate Angle Rotate Angle
Flip Direction Flip Direction

View file

@ -82,7 +82,10 @@ func Resize(buf []byte, o Options) ([]byte, error) {
} }
// Transform image if necessary // Transform image if necessary
if o.Width != inWidth || o.Height != inHeight { debug(">>>>> Resolution %dx%d -> %dx%d", o.Width, o.Height, inWidth, inHeight)
shouldExtract := o.Width != inWidth || o.Height != inHeight || o.AreaWidth > 0 || o.AreaHeight > 0
if shouldExtract {
// Use vips_shrink with the integral reduction // Use vips_shrink with the integral reduction
if shrink > 1 { if shrink > 1 {
image, residual, err = shrinkImage(image, o, residual, shrink) image, residual, err = shrinkImage(image, o, residual, shrink)