Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
merge(add-fit): fix conflicts in server_test.go
  • Loading branch information
h2non committed Nov 14, 2017
2 parents 1cce2f0 + 1a656e2 commit 62f8c2d
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Expand Up @@ -58,6 +58,7 @@ To get started, take a look the [installation](#installation) steps, [usage](#us
- Flop
- Zoom
- Thumbnail
- Fit
- [Pipeline](#get--post-pipeline) of multiple independent image transformations in a single HTTP request.
- Configurable image area extraction
- Embed/Extend image, supporting multiple modes (white, black, mirror, copy or custom background color)
Expand Down Expand Up @@ -758,6 +759,36 @@ Accepts: `image/*, multipart/form-data`. Content-Type: `image/*`
- minampl `float`
- field `string` - Only POST and `multipart/form` payloads

#### GET | POST /fit
Accepts: `image/*, multipart/form-data`. Content-Type: `image/*`

Resize an image to fit within width and height, without cropping. Image aspect ratio is maintained
The width and height specify a maximum bounding box for the image.

##### Allowed params

- width `int` `required`
- height `int` `required`
- quality `int` (JPEG-only)
- compression `int` (PNG-only)
- type `string`
- file `string` - Only GET method and if the `-mount` flag is present
- url `string` - Only GET method and if the `-enable-url-source` flag is present
- embed `bool`
- force `bool`
- rotate `int`
- norotation `bool`
- noprofile `bool`
- stripmeta `bool`
- flip `bool`
- flop `bool`
- extend `string`
- background `string` - Example: `?background=250,20,10`
- colorspace `string`
- sigma `float`
- minampl `float`
- field `string` - Only POST and `multipart/form` payloads

#### GET | POST /rotate
Accepts: `image/*, multipart/form-data`. Content-Type: `image/*`

Expand Down
31 changes: 31 additions & 0 deletions image.go
Expand Up @@ -25,6 +25,7 @@ var OperationsMap = map[string]Operation{
"watermark": Watermark,
"blur": GaussianBlur,
"smartcrop": SmartCrop,
"fit": Fit,
}

// Image stores an image binary buffer and its MIME type
Expand Down Expand Up @@ -95,6 +96,36 @@ func Resize(buf []byte, o ImageOptions) (Image, error) {
return Process(buf, opts)
}

func Fit(buf []byte, o ImageOptions) (Image, error) {
if o.Width == 0 || o.Height == 0 {
return Image{}, NewError("Missing required params: height, width", BadRequest)
}

dims, err := bimg.Size(buf)
if err != nil {
return Image{}, err
}

// if input ratio > output ratio
// (calculation multiplied through by denominators to avoid float division)
if dims.Width*o.Height > o.Width*dims.Height {
// constrained by width
if dims.Width != 0 {
o.Height = o.Width * dims.Height / dims.Width
}
} else {
// constrained by height
if dims.Height != 0 {
o.Width = o.Height * dims.Width / dims.Height
}
}

opts := BimgOptions(o)
opts.Embed = true

return Process(buf, opts)
}

func Enlarge(buf []byte, o ImageOptions) (Image, error) {
if o.Width == 0 || o.Height == 0 {
return Image{}, NewError("Missing required params: height, width", BadRequest)
Expand Down
17 changes: 17 additions & 0 deletions image_test.go
Expand Up @@ -21,6 +21,23 @@ func TestImageResize(t *testing.T) {
}
}

func TestImageFit(t *testing.T) {
opts := ImageOptions{Width: 300, Height: 300}
buf, _ := ioutil.ReadAll(readFile("imaginary.jpg"))

img, err := Fit(buf, opts)
if err != nil {
t.Errorf("Cannot process image: %s", err)
}
if img.Mime != "image/jpeg" {
t.Error("Invalid image MIME type")
}
// 550x740 -> 222x300
if assertSize(img.Body, 222, 300) != nil {
t.Errorf("Invalid image size, expected: %dx%d", opts.Width, opts.Height)
}
}

func TestImagePipelineOperations(t *testing.T) {
width, height := 300, 260

Expand Down
1 change: 1 addition & 0 deletions server.go
Expand Up @@ -87,6 +87,7 @@ func NewServerMux(o ServerOptions) http.Handler {

image := ImageMiddleware(o)
mux.Handle(join(o, "/resize"), image(Resize))
mux.Handle(join(o, "/fit"), image(Fit))
mux.Handle(join(o, "/enlarge"), image(Enlarge))
mux.Handle(join(o, "/extract"), image(Extract))
mux.Handle(join(o, "/crop"), image(Crop))
Expand Down
33 changes: 33 additions & 0 deletions server_test.go
Expand Up @@ -230,6 +230,39 @@ func TestTypeAuto(t *testing.T) {
}
}

func TestFit(t *testing.T) {
ts := testServer(controller(Fit))
buf := readFile("large.jpg")
url := ts.URL + "?width=300&height=300"
defer ts.Close()

res, err := http.Post(url, "image/jpeg", buf)
if err != nil {
t.Fatal("Cannot perform the request")
}

if res.StatusCode != 200 {
t.Fatalf("Invalid response status: %s", res.Status)
}

image, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if len(image) == 0 {
t.Fatalf("Empty response body")
}

err = assertSize(image, 300, 168)
if err != nil {
t.Error(err)
}

if bimg.DetermineImageTypeName(image) != "jpeg" {
t.Fatalf("Invalid image type")
}
}

func TestRemoteHTTPSource(t *testing.T) {
opts := ServerOptions{EnableURLSource: true}
fn := ImageMiddleware(opts)(Crop)
Expand Down

0 comments on commit 62f8c2d

Please sign in to comment.