Skip to content

Commit 276ba52

Browse files
committed
Add usage example and further unit test for new max option
Simplify max vs crop logic
1 parent ad7735a commit 276ba52

3 files changed

Lines changed: 40 additions & 18 deletions

File tree

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,25 @@ sharp('input.jpg').resize(300, 200).write('output.jpg', function(err) {
8585
```
8686

8787
```javascript
88-
sharp('input.jpg').resize(null, 200).progressive().toBuffer(function(err, buffer) {
88+
sharp('input.jpg').resize(null, 200).progressive().toBuffer(function(err, outputBuffer) {
8989
if (err) {
9090
throw err;
9191
}
92-
// buffer contains progressive JPEG image data, 200 pixels high
92+
// outputBuffer contains progressive JPEG image data, 200 pixels high
9393
});
9494
```
9595

9696
```javascript
97-
sharp('input.png').resize(300).sharpen().quality(90).webp(function(err, buffer) {
97+
sharp('input.png').resize(300).sharpen().quality(90).webp(function(err, outputBuffer) {
9898
if (err) {
9999
throw err;
100100
}
101-
// buffer contains 300 pixels wide, sharpened, 90% quality WebP image data
101+
// outputBuffer contains 300 pixels wide, sharpened, 90% quality WebP image data
102102
});
103103
```
104104

105105
```javascript
106-
sharp(buffer).resize(200, 300).embedWhite().write('output.tiff', function(err) {
106+
sharp(inputBuffer).resize(200, 300).embedWhite().write('output.tiff', function(err) {
107107
if (err) {
108108
throw err;
109109
}
@@ -113,15 +113,25 @@ sharp(buffer).resize(200, 300).embedWhite().write('output.tiff', function(err) {
113113
```
114114

115115
```javascript
116-
sharp('input.gif').resize(200, 300).embedBlack().webp(function(err, buffer) {
116+
sharp('input.gif').resize(200, 300).embedBlack().webp(function(err, outputBuffer) {
117117
if (err) {
118118
throw err;
119119
}
120-
// buffer contains WebP image data of a 200 pixels wide and 300 pixels high image
120+
// outputBuffer contains WebP image data of a 200 pixels wide and 300 pixels high
121121
// containing a scaled version, embedded on a black canvas, of input.gif
122122
});
123123
```
124124

125+
```javascript
126+
sharp(inputBuffer).resize(200, 200).max().jpeg(function(err, outputBuffer) {
127+
if (err) {
128+
throw err;
129+
}
130+
// outputBuffer contains JPEG image data no wider than 200 pixels and no higher
131+
// than 200 pixels regardless of the inputBuffer image dimensions
132+
});
133+
```
134+
125135
## API
126136

127137
### sharp(input)
@@ -147,6 +157,8 @@ Crop the resized image to the exact size specified, the default behaviour.
147157

148158
Preserving aspect ratio, resize the image to the maximum width or height specified.
149159

160+
Both `width` and `height` must be provided via `resize` otherwise the behaviour will default to `crop`.
161+
150162
### embedWhite()
151163

152164
Embed the resized image on a white background of the exact size specified.

src/sharp.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ class ResizeWorker : public NanAsyncWorker {
140140
double yfactor = static_cast<double>(in->Ysize) / static_cast<double>(baton->height);
141141
factor = baton->crop ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor);
142142
// if max is set, we need to compute the real size of the thumb image
143-
if(baton->max) {
144-
if(xfactor > yfactor) {
145-
baton->height = round(in->Ysize/factor);
143+
if (baton->max) {
144+
if (xfactor > yfactor) {
145+
baton->height = round(static_cast<double>(in->Ysize) / xfactor);
146146
} else {
147-
baton->width = round(in->Xsize/factor);
147+
baton->width = round(static_cast<double>(in->Xsize) / yfactor);
148148
}
149149
}
150150
} else if (baton->width > 0) {
@@ -235,8 +235,8 @@ class ResizeWorker : public NanAsyncWorker {
235235
// Crop/embed
236236
VipsImage *canvased = vips_image_new();
237237
if (affined->Xsize != baton->width || affined->Ysize != baton->height) {
238-
if (baton->crop || baton->max) {
239-
// Crop
238+
if (baton->crop) {
239+
// Crop/max
240240
int width = std::min(affined->Xsize, baton->width);
241241
int height = std::min(affined->Ysize, baton->height);
242242
int left = (affined->Xsize - width + 1) / 2;
@@ -355,13 +355,11 @@ NAN_METHOD(resize) {
355355
if (canvas->Equals(NanSymbol("c"))) {
356356
baton->crop = true;
357357
} else if (canvas->Equals(NanSymbol("w"))) {
358-
baton->crop = false;
359358
baton->extend = VIPS_EXTEND_WHITE;
360359
} else if (canvas->Equals(NanSymbol("b"))) {
361-
baton->crop = false;
362360
baton->extend = VIPS_EXTEND_BLACK;
363361
} else if (canvas->Equals(NanSymbol("m"))) {
364-
baton->crop = false;
362+
baton->crop = true;
365363
baton->max = true;
366364
}
367365
baton->sharpen = args[6]->BooleanValue();

tests/unit.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ async.series([
112112
},
113113
// Resize to max width or height considering ratio (landscape)
114114
function(done) {
115-
sharp(inputJpg).resize(320,320).max().write(outputJpg, function(err) {
115+
sharp(inputJpg).resize(320, 320).max().write(outputJpg, function(err) {
116116
if (err) throw err;
117117
imagemagick.identify(outputJpg, function(err, features) {
118118
if (err) throw err;
@@ -124,7 +124,7 @@ async.series([
124124
},
125125
// Resize to max width or height considering ratio (portrait)
126126
function(done) {
127-
sharp(inputTiff).resize(320,320).max().write(outputJpg, function(err) {
127+
sharp(inputTiff).resize(320, 320).max().write(outputJpg, function(err) {
128128
if (err) throw err;
129129
imagemagick.identify(outputJpg, function(err, features) {
130130
if (err) throw err;
@@ -133,5 +133,17 @@ async.series([
133133
done();
134134
});
135135
});
136+
},
137+
// Attempt to resize to max but only provide one dimension, so should default to crop
138+
function(done) {
139+
sharp(inputJpg).resize(320).max().write(outputJpg, function(err) {
140+
if (err) throw err;
141+
imagemagick.identify(outputJpg, function(err, features) {
142+
if (err) throw err;
143+
assert.strictEqual(320, features.width);
144+
assert.strictEqual(261, features.height);
145+
done();
146+
});
147+
});
136148
}
137149
]);

0 commit comments

Comments
 (0)