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

Commit 487b675

Browse files
author
Hans Kristian Flaatten
committed
Merge branch 'version-prefix'
Close #3
2 parents 9fac306 + f2dc167 commit 487b675

File tree

3 files changed

+66
-17
lines changed

3 files changed

+66
-17
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ Resize a given source `image` into several `versions`.
3434
* **integer** `height` - image pixel height
3535
* **string** `path` - complete path to source image
3636
* **object** `output` - image resize output config
37-
* **object[]** `versions` - Array of version objects
38-
* **string** `suffix` - suffix for the resized image (ex. "-small")
37+
* **string** `prefix` image versions name prefix (default `""`)
38+
* **integrer** `quality` - global version quality (default `80`)
39+
* **object[]** `versions` - array of version objects
40+
* **string** `suffix` - suffix for the resized image (ex. `-small`)
3941
* **integer** `maxWidth` - max width for resized image
4042
* **integer** `maxHeight` - max height for resized image
41-
* **integer** `quality` - quality for resized image
42-
* **string** `ratio` - force aspectratio on resized image (ex. "4:3")
43+
* **integer** `quality` - quality for resized image (default `80`)
44+
* **string** `ratio` - force aspectratio on resized image (ex. `4:3`)
4345
* **boolean** `flatten` - used in conjunction with background
44-
* **string** `background` - set background to transparent image
45-
* **string** `format` - image format for resized image (ex. "png")
46+
* **string** `background` - set background to transparent image (ex. `red`)
47+
* **string** `format` - image format for resized image (ex. `png`)
4648
* **function** `cb` - callback function (**Error** `error`, **object[]** `versions`)
4749
* **Error** `error` - error output if command failed
4850
* **object[]** `versions` - resized image versions

index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var join = require('path').join;
77
var sprintf = require('util').format;
88

99
module.exports = function(image, output, cb) {
10-
var cmd = module.exports.cmd(image, output.versions);
10+
var cmd = module.exports.cmd(image, output);
1111
exec(cmd, {timeout: 10000}, function(e, stdout, stderr) {
1212
if (e) { return cb(e); }
1313
if (stderr) { return cb(new Error(stderr)); }
@@ -56,7 +56,7 @@ module.exports.path = function(path, opts) {
5656
ext = '.' + opts.format;
5757
}
5858

59-
return join(dir, base + opts.suffix + ext);
59+
return join(dir, opts.prefix + base + opts.suffix + ext);
6060
};
6161

6262
/**
@@ -67,13 +67,20 @@ module.exports.path = function(path, opts) {
6767
*
6868
* @return string convert command
6969
*/
70-
module.exports.cmd = function(image, versions) {
70+
module.exports.cmd = function(image, output) {
7171
var cmd = [
7272
sprintf('convert %s -strip -write mpr:%s +delete', image.path, image.path)
7373
];
7474

75-
for (var i = 0; i < versions.length; i++) {
76-
cmd.push(module.exports.cmdVersion(image, versions[i], i === versions.length-1));
75+
for (var i = 0; i < output.versions.length; i++) {
76+
var version = output.versions[i];
77+
var last = (i === output.versions.length-1);
78+
79+
version.prefix = version.prefix || output.prefix || '';
80+
version.suffix = version.suffix || '';
81+
version.quality = version.quality || output.quality || 80;
82+
83+
cmd.push(module.exports.cmdVersion(image, version, last));
7784
}
7885

7986
return cmd.join(' ');
@@ -95,7 +102,9 @@ module.exports.cmdVersion = function(image, version, last) {
95102
cmd.push(sprintf('mpr:%s', image.path));
96103

97104
// -quality
98-
cmd.push(sprintf('-quality %d', version.quality || 80));
105+
if (version.quality) {
106+
cmd.push(sprintf('-quality %d', version.quality));
107+
}
99108

100109
// -background
101110
if (version.background) {
@@ -118,7 +127,8 @@ module.exports.cmdVersion = function(image, version, last) {
118127

119128
// -write
120129
version.path = module.exports.path(image.path, {
121-
suffix: version.suffix || '',
130+
prefix: version.prefix,
131+
suffix: version.suffix,
122132
format: version.format
123133
});
124134

test.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,40 @@ var resize = require('./index');
55

66
describe('resize.path()', function() {
77
it('returns new relative path with suffix', function() {
8-
var path = resize.path('./foo.jpg', {suffix: '-bar'});
8+
var path = resize.path('./foo.jpg', {prefix: '', suffix: '-bar'});
9+
910
assert.equal(path, 'foo-bar.jpg');
1011
});
1112

1213
it('returns new relative path with custom format', function() {
13-
var path = resize.path('./foo.jpg', {suffix: '-bar', format: 'png'});
14+
var path = resize.path('./foo.jpg', {
15+
prefix: '',
16+
suffix: '-bar',
17+
format: 'png'
18+
});
19+
1420
assert.equal(path, 'foo-bar.png');
1521
});
1622

1723
it('returns new absolute path with suffix', function() {
18-
var path = resize.path('/foo/bar/baz.jpg', {suffix: '-bix'});
24+
var path = resize.path('/foo/bar/baz.jpg', {prefix: '', suffix: '-bix'});
1925
assert.equal(path, '/foo/bar/baz-bix.jpg');
2026
});
2127

2228
it('returns new absolute path with custom format', function() {
23-
var path = resize.path('/foo/bar/baz.jpg', {suffix: '-bix', format: 'png'});
29+
var path = resize.path('/foo/bar/baz.jpg', {
30+
prefix: '',
31+
suffix: '-bix',
32+
format: 'png'
33+
});
34+
2435
assert.equal(path, '/foo/bar/baz-bix.png');
2536
});
37+
38+
it('returns new path with prefix', function() {
39+
var path = resize.path('/foo/bar/baz.jpg', {prefix: 'prefix-', suffix: ''});
40+
assert.equal(path, '/foo/bar/prefix-baz.jpg');
41+
});
2642
});
2743

2844
describe('resize.crop()', function() {
@@ -50,6 +66,26 @@ describe('resize.crop()', function() {
5066
describe('resize.cmd()', function() { });
5167

5268
describe('resize.cmdVersion()', function() {
69+
it('returns convert command for version', function() {
70+
var image = {
71+
path: './a.jpg',
72+
width: 2000,
73+
height: 1000
74+
};
75+
76+
var version = {
77+
prefix: '',
78+
suffix: '-b',
79+
maxWidth: 500,
80+
maxHeight: 500
81+
};
82+
83+
var cmd = resize.cmdVersion(image, version);
84+
var out = 'mpr:./a.jpg -resize "500x500" -write a-b.jpg +delete';
85+
86+
assert.equal(cmd, out);
87+
});
88+
5389
it('sets custom quality if specified', function() {
5490
var image = {
5591
path: './a.jpg',
@@ -58,6 +94,7 @@ describe('resize.cmdVersion()', function() {
5894
};
5995

6096
var version = {
97+
prefix: '',
6198
suffix: '-b',
6299
quality: 50,
63100
maxWidth: 500,

0 commit comments

Comments
 (0)