Skip to content

Commit

Permalink
Merge pull request #175 from LinusU/feature-min
Browse files Browse the repository at this point in the history
feature: min
  • Loading branch information
lovell committed Feb 27, 2015
2 parents 749dc61 + bcd82f4 commit 88753a6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ Preserving aspect ratio, resize the image to the maximum `width` or `height` spe

Both `width` and `height` must be provided via `resize` otherwise the behaviour will default to `crop`.

#### min()

Preserving aspect ratio, resize the image to the minimum `width` or `height` specified.

Both `width` and `height` must be provided via `resize` otherwise the behaviour will default to `crop`.

#### background(rgba)

Set the background for the `embed` and `flatten` operations.
Expand Down
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var Sharp = function(input) {
heightPost: -1,
width: -1,
height: -1,
canvas: 'c',
canvas: 'crop',
gravity: 0,
angle: 0,
rotateBeforePreExtract: false,
Expand Down Expand Up @@ -136,7 +136,7 @@ Sharp.prototype._write = function(chunk, encoding, callback) {
module.exports.gravity = {'center': 0, 'centre': 0, 'north': 1, 'east': 2, 'south': 3, 'west': 4};

Sharp.prototype.crop = function(gravity) {
this.options.canvas = 'c';
this.options.canvas = 'crop';
if (typeof gravity === 'number' && !Number.isNaN(gravity) && gravity >= 0 && gravity <= 4) {
this.options.gravity = gravity;
} else {
Expand Down Expand Up @@ -176,12 +176,17 @@ Sharp.prototype.background = function(rgba) {
};

Sharp.prototype.embed = function() {
this.options.canvas = 'e';
this.options.canvas = 'embed';
return this;
};

Sharp.prototype.max = function() {
this.options.canvas = 'm';
this.options.canvas = 'max';
return this;
};

Sharp.prototype.min = function() {
this.options.canvas = 'min';
return this;
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"Amit Pitaru <pitaru.amit@gmail.com>",
"Brandon Aaron <hello.brandon@aaron.sh>",
"Andreas Lind <andreas@one.com>",
"Maurus Cuelenaere <mcuelenaere@gmail.com>"
"Maurus Cuelenaere <mcuelenaere@gmail.com>",
"Linus Unnebäck <linus@folkdatorn.se>"
],
"description": "High performance Node.js module to resize JPEG, PNG, WebP and TIFF images using the libvips library",
"scripts": {
Expand Down
46 changes: 32 additions & 14 deletions src/resize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ using sharp::counterQueue;

enum class Canvas {
CROP,
EMBED,
MAX,
EMBED
MIN
};

enum class Angle {
Expand Down Expand Up @@ -252,14 +253,29 @@ class ResizeWorker : public NanAsyncWorker {
// Fixed width and height
double xfactor = static_cast<double>(inputWidth) / static_cast<double>(baton->width);
double yfactor = static_cast<double>(inputHeight) / static_cast<double>(baton->height);
factor = (baton->canvas == Canvas::CROP) ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor);
// if max is set, we need to compute the real size of the thumb image
if (baton->canvas == Canvas::MAX) {
if (xfactor > yfactor) {
baton->height = round(static_cast<double>(inputHeight) / xfactor);
} else {
baton->width = round(static_cast<double>(inputWidth) / yfactor);
}
switch (baton->canvas) {
case Canvas::CROP:
factor = std::min(xfactor, yfactor);
break;
case Canvas::EMBED:
factor = std::max(xfactor, yfactor);
break;
case Canvas::MAX:
factor = std::max(xfactor, yfactor);
if (xfactor > yfactor) {
baton->height = round(static_cast<double>(inputHeight) / xfactor);
} else {
baton->width = round(static_cast<double>(inputWidth) / yfactor);
}
break;
case Canvas::MIN:
factor = std::min(xfactor, yfactor);
if (xfactor < yfactor) {
baton->height = round(static_cast<double>(inputHeight) / xfactor);
} else {
baton->width = round(static_cast<double>(inputWidth) / yfactor);
}
break;
}
} else if (baton->width > 0) {
// Fixed width, auto height
Expand Down Expand Up @@ -549,7 +565,7 @@ class ResizeWorker : public NanAsyncWorker {
vips_object_local(hook, embedded);
image = embedded;
} else {
// Crop/max
// Crop/max/min
int left;
int top;
std::tie(left, top) = CalculateCrop(image->Xsize, image->Ysize, baton->width, baton->height, baton->gravity);
Expand Down Expand Up @@ -963,12 +979,14 @@ NAN_METHOD(resize) {
baton->height = options->Get(NanNew<String>("height"))->Int32Value();
// Canvas option
Local<String> canvas = options->Get(NanNew<String>("canvas"))->ToString();
if (canvas->Equals(NanNew<String>("c"))) {
if (canvas->Equals(NanNew<String>("crop"))) {
baton->canvas = Canvas::CROP;
} else if (canvas->Equals(NanNew<String>("m"))) {
baton->canvas = Canvas::MAX;
} else if (canvas->Equals(NanNew<String>("e"))) {
} else if (canvas->Equals(NanNew<String>("embed"))) {
baton->canvas = Canvas::EMBED;
} else if (canvas->Equals(NanNew<String>("max"))) {
baton->canvas = Canvas::MAX;
} else if (canvas->Equals(NanNew<String>("min"))) {
baton->canvas = Canvas::MIN;
}
// Background colour
Local<Array> background = Local<Array>::Cast(options->Get(NanNew<String>("background")));
Expand Down
33 changes: 33 additions & 0 deletions test/unit/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,39 @@ describe('Resize dimensions', function() {
});
});

it('Min width or height considering ratio (landscape)', function(done) {
sharp(fixtures.inputJpg).resize(320, 320).min().toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(392, info.width);
assert.strictEqual(320, info.height);
done();
});
});

it('Min width or height considering ratio (portrait)', function(done) {
sharp(fixtures.inputTiff).resize(320, 320).min().jpeg().toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(422, info.height);
done();
});
});

it('Provide only one dimension with min, should default to crop', function(done) {
sharp(fixtures.inputJpg).resize(320).min().toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height);
done();
});
});

it('Do not enlarge when input width is already less than output width', function(done) {
sharp(fixtures.inputJpg)
.resize(2800)
Expand Down

0 comments on commit 88753a6

Please sign in to comment.