Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit a142696

Browse files
author
Hans Kristian Flaatten
committed
Merge branch 'version-path'
2 parents 487b675 + 085c417 commit a142696

File tree

3 files changed

+142
-32
lines changed

3 files changed

+142
-32
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Resize a given source `image` into several `versions`.
3535
* **string** `path` - complete path to source image
3636
* **object** `output` - image resize output config
3737
* **string** `prefix` image versions name prefix (default `""`)
38+
* **string** `path` image versions directory path
3839
* **integrer** `quality` - global version quality (default `80`)
3940
* **object[]** `versions` - array of version objects
4041
* **string** `suffix` - suffix for the resized image (ex. `-small`)

index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ module.exports.crop = function(image, ratio) {
4040
/**
4141
* Get new path with suffix
4242
*
43-
* @param string path - image path
43+
* @param string src - source image path
4444
* @param string opts - output path transformations
4545
* * format
4646
* * suffix
4747
*
4848
* @return string path
4949
*/
50-
module.exports.path = function(path, opts) {
51-
var dir = dirname(path);
52-
var ext = extname(path);
53-
var base = basename(path, ext);
50+
module.exports.path = function(src, opts) {
51+
var dir = opts.path || dirname(src);
52+
var ext = extname(src);
53+
var base = basename(src, ext);
5454

5555
if (opts.format) {
5656
ext = '.' + opts.format;
@@ -76,10 +76,15 @@ module.exports.cmd = function(image, output) {
7676
var version = output.versions[i];
7777
var last = (i === output.versions.length-1);
7878

79-
version.prefix = version.prefix || output.prefix || '';
80-
version.suffix = version.suffix || '';
8179
version.quality = version.quality || output.quality || 80;
8280

81+
version.path = module.exports.path(image.path, {
82+
format: version.format,
83+
path: output.path,
84+
prefix: version.prefix || output.prefix || '',
85+
suffix: version.suffix || ''
86+
});
87+
8388
cmd.push(module.exports.cmdVersion(image, version, last));
8489
}
8590

@@ -126,12 +131,6 @@ module.exports.cmdVersion = function(image, version, last) {
126131
cmd.push(sprintf('-resize "%dx%d"', version.maxWidth, version.maxHeight));
127132

128133
// -write
129-
version.path = module.exports.path(image.path, {
130-
prefix: version.prefix,
131-
suffix: version.suffix,
132-
format: version.format
133-
});
134-
135134
if (last) {
136135
cmd.push(version.path);
137136
} else {

test.js

Lines changed: 129 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ describe('resize.path()', function() {
3939
var path = resize.path('/foo/bar/baz.jpg', {prefix: 'prefix-', suffix: ''});
4040
assert.equal(path, '/foo/bar/prefix-baz.jpg');
4141
});
42+
43+
it('returns new path with custom directory', function() {
44+
var path = resize.path('/foo/bar/baz.jpg', {
45+
prefix: 'im-',
46+
suffix: '',
47+
path: '/tmp'
48+
});
49+
50+
assert.equal(path, '/tmp/im-baz.jpg');
51+
});
4252
});
4353

4454
describe('resize.crop()', function() {
@@ -63,49 +73,149 @@ describe('resize.crop()', function() {
6373
});
6474
});
6575

66-
describe('resize.cmd()', function() { });
76+
describe('resize.cmd()', function() {
77+
var output, image;
78+
79+
beforeEach(function() {
80+
image = {
81+
path: './assets/horizontal.jpg',
82+
width: 5184,
83+
height: 2623
84+
};
85+
86+
output = {
87+
versions: [{
88+
suffix: '-full',
89+
maxHeight: 1920,
90+
maxWidth: 1920
91+
},{
92+
suffix: '-1200',
93+
maxHeight: 1200,
94+
maxWidth: 1200,
95+
aspect: "3:2"
96+
}]
97+
};
98+
});
99+
100+
it('sets global path to each version', function() {
101+
output.path = '/tmp';
102+
resize.cmd(image, output);
103+
104+
assert.equal(output.versions[0].path, '/tmp/horizontal-full.jpg');
105+
assert.equal(output.versions[1].path, '/tmp/horizontal-1200.jpg');
106+
});
107+
108+
it('sets global prefix to each version', function() {
109+
output.prefix = 'im-';
110+
resize.cmd(image, output);
111+
112+
assert.equal(output.versions[0].path, 'assets/im-horizontal-full.jpg');
113+
assert.equal(output.versions[1].path, 'assets/im-horizontal-1200.jpg');
114+
});
115+
116+
it('sets default quality to each version', function() {
117+
resize.cmd(image, output);
118+
119+
assert.equal(output.versions[0].quality, 80);
120+
assert.equal(output.versions[1].quality, 80);
121+
});
122+
123+
it('sets global quality to each version', function() {
124+
output.quality = 20;
125+
resize.cmd(image, output);
126+
127+
assert.equal(output.versions[0].quality, 20);
128+
assert.equal(output.versions[1].quality, 20);
129+
});
130+
131+
it('preserves local version quality', function() {
132+
output.quality = 30;
133+
output.versions[1].quality = 99;
134+
135+
resize.cmd(image, output);
136+
137+
assert.equal(output.versions[0].quality, 30);
138+
assert.equal(output.versions[1].quality, 99);
139+
});
140+
141+
it('returns convert command', function() {
142+
var cmd = resize.cmd(image, output);
143+
assert.equal(cmd, [
144+
// original image
145+
'convert ./assets/horizontal.jpg',
146+
'-strip',
147+
'-write mpr:./assets/horizontal.jpg +delete',
148+
149+
// version[0]
150+
'mpr:./assets/horizontal.jpg',
151+
'-quality 80',
152+
'-resize "1920x1920"',
153+
'-write assets/horizontal-full.jpg +delete',
154+
155+
// version[1]
156+
'mpr:./assets/horizontal.jpg',
157+
'-quality 80',
158+
'-crop "3936x2623+624+0"',
159+
'-resize "1200x1200"',
160+
'assets/horizontal-1200.jpg'
161+
].join(' '));
162+
});
163+
});
67164

68165
describe('resize.cmdVersion()', function() {
69-
it('returns convert command for version', function() {
70-
var image = {
166+
var image, version;
167+
168+
beforeEach(function() {
169+
image = {
71170
path: './a.jpg',
72171
width: 2000,
73172
height: 1000
74173
};
75174

76-
var version = {
77-
prefix: '',
78-
suffix: '-b',
175+
version = {
176+
path: 'a-b.jpg',
79177
maxWidth: 500,
80178
maxHeight: 500
81179
};
180+
});
82181

182+
it('returns convert command for version', function() {
83183
var cmd = resize.cmdVersion(image, version);
84184
var out = 'mpr:./a.jpg -resize "500x500" -write a-b.jpg +delete';
85185

86186
assert.equal(cmd, out);
87187
});
88188

89-
it('sets custom quality if specified', function() {
90-
var image = {
91-
path: './a.jpg',
92-
width: 2000,
93-
height: 1000
94-
};
189+
it('returns convert command for last version', function() {
190+
var cmd = resize.cmdVersion(image, version, true);
191+
var out = 'mpr:./a.jpg -resize "500x500" a-b.jpg';
95192

96-
var version = {
97-
prefix: '',
98-
suffix: '-b',
99-
quality: 50,
100-
maxWidth: 500,
101-
maxHeight: 500
102-
};
193+
assert.equal(cmd, out);
194+
});
195+
196+
it('sets quality if specified', function() {
197+
version.quality = 50;
103198

104199
var cmd = resize.cmdVersion(image, version);
105200
var out = 'mpr:./a.jpg -quality 50 -resize "500x500" -write a-b.jpg +delete';
106201

107202
assert.equal(cmd, out);
108203
});
204+
205+
it('sets crop if aspect ratio is defined', function() {
206+
version.aspect = '4:3';
207+
208+
var cmd = resize.cmdVersion(image, version);
209+
var out = [
210+
'mpr:./a.jpg',
211+
'-crop "1334x1000+333+0"',
212+
'-resize "500x500"',
213+
'-write a-b.jpg',
214+
'+delete'
215+
].join(' ');
216+
217+
assert.equal(cmd, out);
218+
});
109219
});
110220

111221
describe('resize()', function() {

0 commit comments

Comments
 (0)