Comparing version 0.0.7 to 0.1.0
@@ -16,3 +16,5 @@ var gm = require('gm'); | ||
var resizeByCover; | ||
var resizeByFit; | ||
var resizeByContain; | ||
var resizeUpOrDown; | ||
var calculateCropArea; | ||
@@ -67,11 +69,11 @@ var isPortrait; | ||
doResize = function(params, fn) { | ||
var args = {width: params.width, height: params.height}; | ||
var destiny = {width: params.width, height: params.height}; | ||
if (params.method === 'cover') return resizeByCover.call(this, args, fn); | ||
return resizeByContain.call(this, args, fn); | ||
if (params.method === 'cover') return resizeByCover.call(this, destiny, fn); | ||
if (params.method === 'fit') return resizeByFit.call(this, destiny, fn); | ||
return resizeByContain.call(this, destiny, fn); | ||
}; | ||
resizeByCover = function(params, fn) { | ||
resizeByCover = function(destiny, fn) { | ||
var oenyi = this; | ||
var destiny = params; | ||
var crop; | ||
@@ -103,5 +105,4 @@ | ||
resizeByContain = function(params, fn) { | ||
resizeByFit = function(destiny, fn) { | ||
var oenyi = this; | ||
var destiny = params; | ||
@@ -113,34 +114,58 @@ this._getSize(function(err, original) { | ||
original.ratio = original.width / original.height; | ||
destiny.ratio = destiny.width / destiny.height; | ||
if (imageFits) { | ||
oenyi._calc = {resize: deepcopy(original)}; | ||
return fn(null, oenyi._image); | ||
} | ||
if (imageFits) return fn(null, oenyi._image); | ||
resizeUpOrDown.call(oenyi, original, destiny, fn); | ||
}); | ||
}; | ||
if (isPortrait(original)) { | ||
if (isSquare(destiny) || isLandscape(destiny)) { | ||
destiny.width = Math.round(destiny.height * original.ratio) | ||
} else if (original.ratio < destiny.ratio) { | ||
destiny.width = Math.round(destiny.height * original.ratio); | ||
} else { | ||
destiny.height = Math.round(destiny.width / original.ratio); | ||
} | ||
} else if(isSquare(original)) { | ||
if (destiny.width < destiny.height) { | ||
destiny.height = destiny.width; | ||
} else { | ||
destiny.width = destiny.height; | ||
} | ||
} else { | ||
if (destiny.ratio > original.ratio) { | ||
destiny.width = Math.round(destiny.height * original.ratio); | ||
} else { | ||
destiny.height = Math.round(destiny.width / original.ratio); | ||
} | ||
resizeByContain = function(destiny, fn) { | ||
var oenyi = this; | ||
this._getSize(function(err, original) { | ||
if (err) return fn(err); | ||
var imageFits = original.width <= destiny.width && original.height <= destiny.height; | ||
if (imageFits) { | ||
oenyi._calc = {resize: deepcopy(original)}; | ||
return fn(null, oenyi._image); | ||
} | ||
oenyi._calc = {resize: deepcopy(destiny)}; | ||
oenyi._image.resize(destiny.width, destiny.height); | ||
return fn(null, oenyi._image); | ||
resizeUpOrDown.call(oenyi, original, destiny, fn); | ||
}); | ||
} | ||
}; | ||
resizeUpOrDown = function(original, destiny, fn) { | ||
original.ratio = original.width / original.height; | ||
destiny.ratio = destiny.width / destiny.height; | ||
if (isPortrait(original)) { | ||
if (isSquare(destiny) || isLandscape(destiny)) { | ||
destiny.width = Math.round(destiny.height * original.ratio) | ||
} else if (original.ratio < destiny.ratio) { | ||
destiny.width = Math.round(destiny.height * original.ratio); | ||
} else { | ||
destiny.height = Math.round(destiny.width / original.ratio); | ||
} | ||
} else if(isSquare(original)) { | ||
if (destiny.width < destiny.height) { | ||
destiny.height = destiny.width; | ||
} else { | ||
destiny.width = destiny.height; | ||
} | ||
} else { | ||
if (destiny.ratio > original.ratio) { | ||
destiny.width = Math.round(destiny.height * original.ratio); | ||
} else { | ||
destiny.height = Math.round(destiny.width / original.ratio); | ||
} | ||
} | ||
this._calc = {resize: deepcopy(destiny)}; | ||
this._image.resize(destiny.width, destiny.height); | ||
return fn(null, this._image); | ||
}; | ||
doConvertToJPG = function(fn) { | ||
@@ -147,0 +172,0 @@ var oenyi = this; |
{ | ||
"name": "oenyi", | ||
"description": "A wrapper for image processing commands that provides a chainable API with asynchronous pipeline of commands.", | ||
"version": "0.0.7", | ||
"version": "0.1.0", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "image", |
@@ -5,17 +5,43 @@ # Oenyi | ||
A wrapper for a few gm methods that just provides a convenient and consistent API. | ||
A very simple wrapper for a few image processing methods that just provides: | ||
* A convenient and consistent API. | ||
* Full in memory processing support to reduce i/o and increase speed. | ||
* Three resizing methods that will make your life easier. | ||
It is built to chain all transformations you need and execute them once you call the `exec` method, returning the modified image as a `Stream`. | ||
It is designed to chain the transformations you need and execute them in order once you call the `exec` method which gives you a buffer with the image or, if you prefer, when you call the `pipe` method which accepts a stream. | ||
### Getting an oenyi image instance. | ||
Want to see the technical specification? [Go here](https://github.com/codingpains/oenyi/wiki/Oenyi%20Technical%20Spec) | ||
### Installation | ||
``` | ||
npm install oenyi | ||
``` | ||
### Require oenyi | ||
```js | ||
var oenyi = require('oenyi'); | ||
``` | ||
### Getting an oenyi image instance. | ||
#### Using a string as path. | ||
```js | ||
var image = oenyi('/path/to/image'); | ||
``` | ||
#### Using a buffer | ||
```js | ||
var fs = require('fs'); | ||
fs.readFile('/path/to/image', function(err, buffer) { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
var image = oenyi(buffer); | ||
}); | ||
``` | ||
### Convert image to jpeg. | ||
```js | ||
@@ -27,3 +53,2 @@ image.toJPG(); | ||
```js | ||
@@ -33,22 +58,43 @@ image.compress({quality: 90}); | ||
## Reszing Methods. | ||
### Resize image respecting the original aspect ratio. | ||
Oenyi gives you three resizing methods: Cover, Contain and Fit. They do what they promise. | ||
Keeps aspect ratio and just scales up or down the image until it fits the provided sizes. | ||
### Resize by Contain. | ||
It is meant to be used when you want to ensure a maximum size but you want the aspect ratio untouched and if it fits, then the original size as well. | ||
This method will only resize down when the image to resize is bigger than the size provided. | ||
```js | ||
image.resize({width: 500, height: 255, method: 'contain'}); | ||
// original size w:400, h:400 | ||
image.resize({width: 500, height: 255, method: 'contain'}); // => produces size w:255, h:225 | ||
// original size w:3000, h:600 | ||
image.resize({width: 500, height: 255, method: 'contain'}); // => produces size w:500, h:100 | ||
``` | ||
### Resize image to cover or match size and force aspect ratio with no distortion. | ||
### Resize by Fit. | ||
Crops and resizes the image to fit the provided sizes and the aspect ratio given by such sizes. | ||
So you want to grow or shrink an image to fit an area as best as it can, but want to keep the aspect ratio and the full image visible? Well, use this method. | ||
```js | ||
image.resize({width: 500, height: 500, method: 'cover'}); | ||
// original size w:640, h:2560 | ||
image.resize({width: 1024, height: 1024, method: 'fit'}); // => produces size w:256, h:1024 | ||
// original size w:5000, h:3000 | ||
image.resize({width: 1000, height: 2000, method: 'fit'}); // => produces size w:1000, h:600 | ||
``` | ||
### Execute all commands and return the Buffer with the modified image. | ||
### Resize by Cover. | ||
Resizes an image to cover or match a size and force a new aspect ratio with no distortion. | ||
```js | ||
// original size w:640, h:2560 | ||
image.resize({width: 500, height: 500, method: 'cover'}); // => produces size w:500, h:500 | ||
``` | ||
### Execute all commands and return a buffer with the modified image. | ||
```js | ||
image.exec(function(err, imageBuffer) { | ||
@@ -59,3 +105,3 @@ // Your code here. | ||
### Execute all commands and pipe to a WritableStream; | ||
### Execute all commands and pipe to a stream. | ||
@@ -85,1 +131,11 @@ ```js | ||
``` | ||
## FAQ | ||
### Why the silly name? | ||
Because of this creepy video. https://youtu.be/GzobV_qoIcQ | ||
You can blame [javierbyte](http://github.com/javierbyte) for that. | ||
### Why do this wrapper? | ||
At [VoxFeed](http://voxfeed.com) we wanted to remove image processing related logic from our platform, isolate it and | ||
provide a consistent API to make our image related code more bearable...err, readable. So we designed this wrapper. |
@@ -29,4 +29,4 @@ var test = require('tape'); | ||
test('resize by contain: should calculate correct values landscape to portrait', function(assert) { | ||
var resizeArgs = {width: 300, height: 400, method: 'contain'}; | ||
test('resize by fit: should calculate correct values landscape to portrait', function(assert) { | ||
var resizeArgs = {width: 300, height: 400, method: 'fit'}; | ||
var image = oenyi(testImages.ls.filename); | ||
@@ -52,4 +52,4 @@ | ||
test('resize by contain (buffer): should calculate correct values landscape to portrait', function(assert) { | ||
var resizeArgs = {width: 300, height: 400, method: 'contain'}; | ||
test('resize by fit (buffer): should calculate correct values landscape to portrait', function(assert) { | ||
var resizeArgs = {width: 300, height: 400, method: 'fit'}; | ||
var image = oenyi(testImages.ls.buffer); | ||
@@ -75,4 +75,4 @@ | ||
test('resize by contain: should calculate correct values landscape to square', function(assert) { | ||
var resizeArgs = {width: 200, height: 200, method: 'contain'}; | ||
test('resize by fit: should calculate correct values landscape to square', function(assert) { | ||
var resizeArgs = {width: 200, height: 200, method: 'fit'}; | ||
var image = oenyi(testImages.ls.filename); | ||
@@ -98,4 +98,4 @@ | ||
test('resize by contain (buffer): should calculate correct values landscape to square', function(assert) { | ||
var resizeArgs = {width: 200, height: 200, method: 'contain'}; | ||
test('resize by fit (buffer): should calculate correct values landscape to square', function(assert) { | ||
var resizeArgs = {width: 200, height: 200, method: 'fit'}; | ||
var image = oenyi(testImages.ls.buffer); | ||
@@ -121,4 +121,4 @@ | ||
test('resize by contain: should calculate correct values landscape to larger ratio landscape', function(assert) { | ||
var resizeArgs = {width: 400, height: 200, method: 'contain'}; | ||
test('resize by fit: should calculate correct values landscape to larger ratio landscape', function(assert) { | ||
var resizeArgs = {width: 400, height: 200, method: 'fit'}; | ||
var image = oenyi(testImages.ls.filename); | ||
@@ -144,4 +144,4 @@ | ||
test('resize by contain (buffer): should calculate correct values landscape to larger ratio landscape', function(assert) { | ||
var resizeArgs = {width: 400, height: 200, method: 'contain'}; | ||
test('resize by fit (buffer): should calculate correct values landscape to larger ratio landscape', function(assert) { | ||
var resizeArgs = {width: 400, height: 200, method: 'fit'}; | ||
var image = oenyi(testImages.ls.buffer); | ||
@@ -167,4 +167,4 @@ | ||
test('resize by contain: should calculate correct values landscape to smaller ratio landscape', function(assert) { | ||
var resizeArgs = {width: 300, height: 270, method: 'contain'}; | ||
test('resize by fit: should calculate correct values landscape to smaller ratio landscape', function(assert) { | ||
var resizeArgs = {width: 300, height: 270, method: 'fit'}; | ||
var image = oenyi(testImages.ls.filename); | ||
@@ -190,4 +190,4 @@ | ||
test('resize by contain (buffer): should calculate correct values landscape to smaller ratio landscape', function(assert) { | ||
var resizeArgs = {width: 300, height: 270, method: 'contain'}; | ||
test('resize by fit (buffer): should calculate correct values landscape to smaller ratio landscape', function(assert) { | ||
var resizeArgs = {width: 300, height: 270, method: 'fit'}; | ||
var image = oenyi(testImages.ls.buffer); | ||
@@ -213,4 +213,4 @@ | ||
test('resize by contain: should calculate correct values landscape to same ratio landscape', function(assert) { | ||
var resizeArgs = {width: 401, height: 300, method: 'contain'}; | ||
test('resize by fit: should calculate correct values landscape to same ratio landscape', function(assert) { | ||
var resizeArgs = {width: 401, height: 300, method: 'fit'}; | ||
var image = oenyi(testImages.ls.filename); | ||
@@ -236,4 +236,4 @@ | ||
test('resize by contain (buffer): should calculate correct values landscape to same ratio landscape', function(assert) { | ||
var resizeArgs = {width: 401, height: 300, method: 'contain'}; | ||
test('resize by fit (buffer): should calculate correct values landscape to same ratio landscape', function(assert) { | ||
var resizeArgs = {width: 401, height: 300, method: 'fit'}; | ||
var image = oenyi(testImages.ls.buffer); | ||
@@ -337,4 +337,4 @@ | ||
test('resize by contain: should calculate correct values square to square', function(assert) { | ||
var resizeArgs = {width: 400, height: 400, method: 'contain'}; | ||
test('resize by fit: should calculate correct values square to square', function(assert) { | ||
var resizeArgs = {width: 400, height: 400, method: 'fit'}; | ||
var image = oenyi(testImages.sq.filename); | ||
@@ -356,4 +356,4 @@ | ||
test('resize by contain (buffer): should calculate correct values square to square', function(assert) { | ||
var resizeArgs = {width: 400, height: 400, method: 'contain'}; | ||
test('resize by fit (buffer): should calculate correct values square to square', function(assert) { | ||
var resizeArgs = {width: 400, height: 400, method: 'fit'}; | ||
var image = oenyi(testImages.sq.buffer); | ||
@@ -375,4 +375,4 @@ | ||
test('resize by contain: should calculate correct values square to portrait', function(assert) { | ||
var resizeArgs = {width: 200, height: 400, method: 'contain'}; | ||
test('resize by fit: should calculate correct values square to portrait', function(assert) { | ||
var resizeArgs = {width: 200, height: 400, method: 'fit'}; | ||
var image = oenyi(testImages.sq.filename); | ||
@@ -398,4 +398,4 @@ | ||
test('resize by contain (buffer): should calculate correct values square to portrait', function(assert) { | ||
var resizeArgs = {width: 200, height: 400, method: 'contain'}; | ||
test('resize by fit (buffer): should calculate correct values square to portrait', function(assert) { | ||
var resizeArgs = {width: 200, height: 400, method: 'fit'}; | ||
var image = oenyi(testImages.sq.buffer); | ||
@@ -421,4 +421,4 @@ | ||
test('resize by contain: should calculate correct values square to landsape', function(assert) { | ||
var resizeArgs = {width: 400, height: 310, method: 'contain'}; | ||
test('resize by fit: should calculate correct values square to landsape', function(assert) { | ||
var resizeArgs = {width: 400, height: 310, method: 'fit'}; | ||
var image = oenyi(testImages.sq.filename); | ||
@@ -444,4 +444,4 @@ | ||
test('resize by contain (buffer): should calculate correct values square to landsape', function(assert) { | ||
var resizeArgs = {width: 400, height: 310, method: 'contain'}; | ||
test('resize by fit (buffer): should calculate correct values square to landsape', function(assert) { | ||
var resizeArgs = {width: 400, height: 310, method: 'fit'}; | ||
var image = oenyi(testImages.sq.buffer); | ||
@@ -517,4 +517,4 @@ | ||
test('should resize with correct values in contain method portait to square', function(assert) { | ||
var resizeArgs = {width: 400, height: 400, method: 'contain'}; | ||
test('should resize with correct values in fit method portait to square', function(assert) { | ||
var resizeArgs = {width: 400, height: 400, method: 'fit'}; | ||
var image = oenyi(testImages.pt.filename); | ||
@@ -540,4 +540,4 @@ | ||
test('should resize with correct values in contain method portait to portrait with smaller ratio', function(assert) { | ||
var resizeArgs = {width: 200, height: 800, method: 'contain'}; | ||
test('should resize with correct values in fit method portait to portrait with smaller ratio', function(assert) { | ||
var resizeArgs = {width: 200, height: 800, method: 'fit'}; | ||
var image = oenyi(testImages.pt.filename); | ||
@@ -544,0 +544,0 @@ |
280
test/test.js
@@ -236,5 +236,5 @@ var test = require('tape'); | ||
test('should apply just resize when method is contain', function(assert) { | ||
test('should apply just resize when method is fit', function(assert) { | ||
assert.plan(2); | ||
var resizeArgs = {width: 300, height: 400, method: 'contain'}; | ||
var resizeArgs = {width: 300, height: 400, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -261,4 +261,4 @@ | ||
test('resize by contain: should calculate correct values landscape to portrait', function(assert) { | ||
var resizeArgs = {width: 300, height: 400, method: 'contain'}; | ||
test('resize by fit: should calculate correct values landscape to portrait', function(assert) { | ||
var resizeArgs = {width: 300, height: 400, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -282,4 +282,4 @@ | ||
test('resize by contain: should calculate correct values landscape to square', function(assert) { | ||
var resizeArgs = {width: 200, height: 200, method: 'contain'}; | ||
test('resize by fit: should calculate correct values landscape to square', function(assert) { | ||
var resizeArgs = {width: 200, height: 200, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -303,4 +303,4 @@ | ||
test('resize by contain: should calculate correct values landscape to larger ratio landscape', function(assert) { | ||
var resizeArgs = {width: 400, height: 200, method: 'contain'}; | ||
test('resize by fit: should calculate correct values landscape to larger ratio landscape', function(assert) { | ||
var resizeArgs = {width: 400, height: 200, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -324,4 +324,4 @@ | ||
test('resize by contain: should calculate correct values landscape to smaller ratio landscape', function(assert) { | ||
var resizeArgs = {width: 300, height: 270, method: 'contain'}; | ||
test('resize by fit: should calculate correct values landscape to smaller ratio landscape', function(assert) { | ||
var resizeArgs = {width: 300, height: 270, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -345,4 +345,4 @@ | ||
test('resize by contain: should calculate correct values landscape to same ratio landscape', function(assert) { | ||
var resizeArgs = {width: 401, height: 300, method: 'contain'}; | ||
test('resize by fit: should calculate correct values landscape to same ratio landscape', function(assert) { | ||
var resizeArgs = {width: 401, height: 300, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -366,10 +366,174 @@ | ||
test('resize by cover: should calculate correct values landscape to square', function(assert) { | ||
var resizeArgs = {width: 100, height: 100, method: 'cover'}; | ||
// Resize by Contain: | ||
test('resize by contain: should caclulate correct values landscape to smaller landscape', function(assert) { | ||
var resizeArgs = {width: 500, height: 255, method: 'contain'}; | ||
var image = oenyi(''); | ||
image._size = {width: 3000, height: 600}; | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 500; | ||
var actual = calc.resize.width; | ||
assert.equal(actual, expected, 'resized with is correct'); | ||
expected = 100; | ||
actual = calc.resize.height; | ||
assert.equal(actual, expected, 'resized height is correct'); | ||
assert.end(); | ||
}); | ||
}); | ||
test('resize by contain: should caclulate correct values landscape to bigger landscape', function(assert) { | ||
var resizeArgs = {width: 6000, height: 4500, method: 'contain'}; | ||
var image = oenyi(''); | ||
image._size = {width: 3000, height: 600}; | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 3000; | ||
var actual = calc.resize.width; | ||
assert.equal(actual, expected, 'resized with is correct'); | ||
expected = 600; | ||
actual = calc.resize.height; | ||
assert.equal(actual, expected, 'resized height is correct'); | ||
assert.end(); | ||
}); | ||
}); | ||
test('resize by contain: should calculate correct values landscape to portait (example case)', function(assert) { | ||
var resizeArgs = {width: 1000, height: 2000, method: 'contain'}; | ||
var image = oenyi(''); | ||
image._size = {width: testImages.ls.size.width, height: testImages.ls.size.height}; | ||
image._size = {width: 5000, height: 3000}; | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 1000; | ||
var actual = calc.resize.width; | ||
assert.equal(actual, expected, 'resized with is correct'); | ||
expected = 600; | ||
actual = calc.resize.height; | ||
assert.equal(actual, expected, 'resized height is correct'); | ||
assert.end(); | ||
}); | ||
}); | ||
//From square resizing | ||
//By Contain | ||
test('resize by contain: should calculate correct values square to smaller square', function(assert) { | ||
var resizeArgs = {width: 10, height: 10, method: 'contain'}; | ||
var image = oenyi(''); | ||
image._size = {width: testImages.sq.size.width, height: testImages.sq.size.height}; | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 10; | ||
var actual = calc.resize.width; | ||
assert.equal(actual, expected, 'resized with is correct'); | ||
expected = 10; | ||
actual = calc.resize.height; | ||
assert.equal(actual, expected, 'resized height is correct'); | ||
assert.end(); | ||
}); | ||
}); | ||
test('resize by contain: should calculate correct values square to bigger square', function(assert) { | ||
var resizeArgs = {width: 500, height: 500, method: 'contain'}; | ||
var image = oenyi(''); | ||
image._size = {width: testImages.sq.size.width, height: testImages.sq.size.height}; | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 225; | ||
var actual = calc.resize.width; | ||
assert.equal(actual, expected, 'resized with is correct'); | ||
expected = 225; | ||
actual = calc.resize.height; | ||
assert.equal(actual, expected, 'resized height is correct'); | ||
assert.end(); | ||
}); | ||
}); | ||
test('resize by contain: should calculate correct values square to smaller landsape', function(assert) { | ||
var resizeArgs = {width: 100, height: 20, method: 'contain'}; | ||
var image = oenyi(''); | ||
image._size = {width: testImages.sq.size.width, height: testImages.sq.size.height}; | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 20; | ||
var actual = calc.resize.width; | ||
assert.equal(actual, expected, 'resized with is correct'); | ||
expected = 20; | ||
actual = calc.resize.height; | ||
assert.equal(actual, expected, 'resized height is correct'); | ||
assert.end(); | ||
}); | ||
}); | ||
test('resize by contain: should calculate correct values square to equal landsape', function(assert) { | ||
var resizeArgs = {width: 300, height: 225, method: 'contain'}; | ||
var image = oenyi(''); | ||
image._size = {width: testImages.sq.size.width, height: testImages.sq.size.height}; | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 225; | ||
var actual = calc.resize.width; | ||
assert.equal(actual, expected, 'resized with is correct'); | ||
expected = 225; | ||
actual = calc.resize.height; | ||
assert.equal(actual, expected, 'resized height is correct'); | ||
assert.end(); | ||
}); | ||
}); | ||
test('resize by contain: should calculate correct values square to bigger landsape', function(assert) { | ||
var resizeArgs = {width: 500, height: 300, method: 'contain'}; | ||
var image = oenyi(''); | ||
image._size = {width: testImages.sq.size.width, height: testImages.sq.size.height}; | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 225; | ||
var actual = calc.resize.width; | ||
assert.equal(actual, expected, 'resized with is correct'); | ||
expected = 225; | ||
actual = calc.resize.height; | ||
assert.equal(actual, expected, 'resized height is correct'); | ||
assert.end(); | ||
}); | ||
}); | ||
test('resize by contain: should calculate correct values square to smaller portrait', function(assert) { | ||
var resizeArgs = {width: 100, height: 300, method: 'contain'}; | ||
var image = oenyi(''); | ||
image._size = {width: testImages.sq.size.width, height: testImages.sq.size.height}; | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 100; | ||
@@ -383,18 +547,22 @@ var actual = calc.resize.width; | ||
expected = 194; | ||
actual = calc.crop.width; | ||
assert.equal(actual, expected, 'cropped width is correct'); | ||
assert.end(); | ||
}); | ||
}); | ||
expected = 194; | ||
actual = calc.crop.height; | ||
assert.equal(actual, expected, 'cropped height is correct'); | ||
test('resize by contain: should calculate correct values square to equal portrait', function(assert) { | ||
var resizeArgs = {width: 225, height: 300, method: 'contain'}; | ||
var image = oenyi(''); | ||
expected = 33; | ||
actual = calc.crop.x; | ||
assert.equal(actual, expected, 'crop x is correct'); | ||
image._size = {width: testImages.sq.size.width, height: testImages.sq.size.height}; | ||
expected = 0; | ||
actual = calc.crop.y; | ||
assert.equal(actual, expected, 'crop y is correct'); | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 225; | ||
var actual = calc.resize.width; | ||
assert.equal(actual, expected, 'resized with is correct'); | ||
expected = 225; | ||
actual = calc.resize.height; | ||
assert.equal(actual, expected, 'resized height is correct'); | ||
assert.end(); | ||
@@ -404,7 +572,6 @@ }); | ||
//From square resizing | ||
test('resize by contain: should calculate correct values square to square', function(assert) { | ||
// By Fit | ||
test('resize by fit: should calculate correct values square to square', function(assert) { | ||
assert.plan(2); | ||
var resizeArgs = {width: 400, height: 400, method: 'contain'}; | ||
var resizeArgs = {width: 400, height: 400, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -428,5 +595,5 @@ | ||
test('resize by contain: should calculate correct values square to portrait', function(assert) { | ||
test('resize by fit: should calculate correct values square to portrait', function(assert) { | ||
assert.plan(2); | ||
var resizeArgs = {width: 200, height: 400, method: 'contain'}; | ||
var resizeArgs = {width: 200, height: 400, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -450,5 +617,5 @@ | ||
test('resize by contain: should calculate correct values square to landsape', function(assert) { | ||
test('resize by fit: should calculate correct values square to landsape', function(assert) { | ||
assert.plan(2); | ||
var resizeArgs = {width: 400, height: 310, method: 'contain'}; | ||
var resizeArgs = {width: 400, height: 310, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -472,2 +639,3 @@ | ||
// By Cover | ||
test('resize by cover: should calculate correct values square to square', function(assert) { | ||
@@ -574,5 +742,6 @@ var resizeArgs = {width: 300, height: 300, method: 'cover'}; | ||
test('should resize with correct values in contain method portait to square', function(assert) { | ||
// To fit | ||
test('should resize with correct values in fit method portait to square', function(assert) { | ||
assert.plan(2); | ||
var resizeArgs = {width: 400, height: 400, method: 'contain'}; | ||
var resizeArgs = {width: 400, height: 400, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -596,5 +765,5 @@ | ||
test('should resize with correct values in contain method portait to portrait with smaller ratio', function(assert) { | ||
test('should resize with correct values in fit method portait to portrait with smaller ratio', function(assert) { | ||
assert.plan(2); | ||
var resizeArgs = {width: 200, height: 800, method: 'contain'}; | ||
var resizeArgs = {width: 200, height: 800, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -618,5 +787,5 @@ | ||
test('should resize with correct values in contain method portait to portrait with bigger ratio', function(assert) { | ||
test('should resize with correct values in fit method portait to portrait with bigger ratio', function(assert) { | ||
assert.plan(2); | ||
var resizeArgs = {width: 700, height: 800, method: 'contain'}; | ||
var resizeArgs = {width: 700, height: 800, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -640,5 +809,5 @@ | ||
test('should resize with correct values in contain method portait to portrait with same ratio', function(assert) { | ||
test('should resize with correct values in fit method portait to portrait with same ratio', function(assert) { | ||
assert.plan(2); | ||
var resizeArgs = {width: 559, height: 800, method: 'contain'}; | ||
var resizeArgs = {width: 559, height: 800, method: 'fit'}; | ||
var image = oenyi(''); | ||
@@ -662,6 +831,26 @@ | ||
test('should resize with correct values in contain method portait to landscape', function(assert) { | ||
var resizeArgs = {width: 600, height: 380, method: 'contain'}; | ||
test('should resize with correct values in fit method portait to square (example case)', function(assert) { | ||
var resizeArgs = {width: 1024, height: 1024, method: 'fit'}; | ||
var image = oenyi(''); | ||
image._size = {width: 640, height: 2560}; | ||
image.resize(resizeArgs) | ||
.exec(function(err, buffer, calc) { | ||
var expected = 256; | ||
var actual = calc.resize.width; | ||
assert.equal(actual, expected, 'resized with is correct'); | ||
expected = 1024; | ||
actual = calc.resize.height; | ||
assert.equal(actual, expected, 'resized height is correct'); | ||
assert.end(); | ||
}); | ||
}); | ||
test('should resize with correct values in fit method portait to landscape', function(assert) { | ||
var resizeArgs = {width: 600, height: 380, method: 'fit'}; | ||
var image = oenyi(''); | ||
image._size = {width: testImages.pt.size.width, height: testImages.pt.size.height}; | ||
@@ -683,2 +872,3 @@ | ||
// Compression | ||
@@ -685,0 +875,0 @@ |
131953
1532
137