aspectratio
Advanced tools
Comparing version 2.1.1 to 2.2.1
31
index.js
@@ -0,1 +1,3 @@ | ||
var deprecate = require('util').deprecate; | ||
exports.crop = function(x, y, r) { | ||
@@ -6,5 +8,5 @@ var orient = r.split('!')[1]; | ||
var vertical = y > x; | ||
var rotate = y > x && orient === 'h' || x > y && orient === 'v'; | ||
var rotate = y > x && orient === 'h' || x > y && orient === 'v'; | ||
if ((vertical || rotate) && !(vertical && rotate)) { | ||
if ((vertical || rotate) && !(vertical && rotate)) { | ||
x = x + y; | ||
@@ -18,3 +20,3 @@ y = x - y; | ||
if (yʹ > y || rotate && yʹ > x) { | ||
if (yʹ > y || rotate && yʹ > x) { | ||
yʹ = y; | ||
@@ -32,3 +34,3 @@ xʹ = y * (ratio[1] / ratio[0]); | ||
if ((vertical || rotate) && !(vertical && rotate)) { | ||
if ((vertical || rotate) && !(vertical && rotate)) { | ||
return [ | ||
@@ -50,2 +52,21 @@ Δy, // crop top left x | ||
exports.fixed = require('util').deprecate(exports.crop, 'aspect.fixed: Use aspect.crop instead'); | ||
exports.fixed = deprecate(exports.crop, 'aspect.fixed: Use aspect.crop instead'); | ||
exports.resize = function(x, y, xMax, yMax) { | ||
if (xMax && yMax) { | ||
// Maximum values of height and width given, aspect ratio preserved. | ||
if (y > x) { | ||
return [Math.floor(yMax * x / y), yMax]; | ||
} else { | ||
return [xMax, Math.floor(xMax * y / x)]; | ||
} | ||
} else if (xMax) { | ||
// Width given, height automagically selected to preserve aspect ratio. | ||
return [xMax, Math.floor(xMax * y / x)]; | ||
} else { | ||
// Height given, width automagically selected to preserve aspect ratio. | ||
return [Math.floor(yMax * x / y), yMax]; | ||
} | ||
}; |
{ | ||
"name": "aspectratio", | ||
"version": "2.1.1", | ||
"version": "2.2.1", | ||
"description": "Image aspect ratio utility", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -25,3 +25,3 @@ # aspectratio | ||
Apply a fixed aspect `ratio` crop without distoring the image. | ||
Apply a fixed aspect `ratio` crop without distoring the image aspect ratio. | ||
@@ -58,2 +58,19 @@ * **integer** `width` - original image width | ||
### resize(**integer** `x`, **integer** `y`, **integer** `maxX`, **integer** `maxY`) | ||
Get resized height and width of an image while perserving the aspect ratio of | ||
the image. | ||
* **integer** `x` - original image width | ||
* **integer** `y` - original image height | ||
* **integer** `maxX` - max image width | ||
* **integer** `maxY` - max image height | ||
### Return | ||
Returns an `Array` of the resized `x` and `y` values: | ||
* **integer** `x` - resized image width | ||
* **integer** `y` - resized image height | ||
## [MIT License](https://github.com/Turistforeningen/node-aspectratio/blob/master/LICENSE) |
46
test.js
var assert = require('assert'); | ||
var aspect = require('./index'); | ||
describe('aspect.resize', function() { | ||
var vertical = { x: 3456, y: 5184 }; | ||
var horizontal = { x: 5184, y: 3456 }; | ||
var maxX = 500; | ||
var maxY = 500; | ||
describe('maxX only', function() { | ||
it('returns bounds for horizontal image', function() { | ||
var bounds = aspect.resize(horizontal.x, horizontal.y, maxX); | ||
assert.deepEqual(bounds, [ 500, 333 ]); | ||
}); | ||
it('returns bounds for vertical image', function() { | ||
var bounds = aspect.resize(vertical.x, vertical.y, maxX); | ||
assert.deepEqual(bounds, [ 500, 750 ]); | ||
}); | ||
}); | ||
describe('maxY only', function() { | ||
it('returns bounds for horizontal image', function() { | ||
var bounds = aspect.resize(horizontal.x, horizontal.y, undefined, maxY); | ||
assert.deepEqual(bounds, [ 750, 500 ]); | ||
}); | ||
it('returns bounds for vertical image', function() { | ||
var bounds = aspect.resize(vertical.x, vertical.y, undefined, maxY); | ||
assert.deepEqual(bounds, [ 333, 500 ]); | ||
}); | ||
}); | ||
describe('maxX and maxY', function() { | ||
it('returns bounds for horizontal image', function() { | ||
var bounds = aspect.resize(horizontal.x, horizontal.y, 500, 500); | ||
assert.deepEqual(bounds, [ 500, 333 ]); | ||
}); | ||
it('returns bounds for vertical image', function() { | ||
var bounds = aspect.resize(vertical.x, vertical.y, 500, 500); | ||
assert.deepEqual(bounds, [ 333, 500 ]); | ||
}); | ||
}); | ||
}); | ||
describe('horizontal image', function() { | ||
@@ -147,3 +191,3 @@ // 5184 × 3456 | ||
describe('vertical orientation', function() { | ||
describe('horizontal orientation', function() { | ||
it('returns crop for 1:1 aspect ratio', function() { | ||
@@ -150,0 +194,0 @@ // 3456 × 3456 |
13657
252
75