Skip to content

Commit

Permalink
Add support for image version name prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Kristian Flaatten committed Jun 7, 2015
1 parent 5f937e8 commit d911bb5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Resize a given source `image` into several `versions`.
* **integer** `height` - image pixel height
* **string** `path` - complete path to source image
* **object** `output` - image resize output config
* **string** `prefix` image versions name prefix (default `""`)
* **object[]** `versions` - array of version objects
* **string** `suffix` - suffix for the resized image (ex. `-small`)
* **integer** `maxWidth` - max width for resized image
Expand Down
19 changes: 13 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var join = require('path').join;
var sprintf = require('util').format;

module.exports = function(image, output, cb) {
var cmd = module.exports.cmd(image, output.versions);
var cmd = module.exports.cmd(image, output);
exec(cmd, {timeout: 10000}, function(e, stdout, stderr) {
if (e) { return cb(e); }
if (stderr) { return cb(new Error(stderr)); }
Expand Down Expand Up @@ -56,7 +56,7 @@ module.exports.path = function(path, opts) {
ext = '.' + opts.format;
}

return join(dir, base + opts.suffix + ext);
return join(dir, opts.prefix + base + opts.suffix + ext);
};

/**
Expand All @@ -67,13 +67,19 @@ module.exports.path = function(path, opts) {
*
* @return string convert command
*/
module.exports.cmd = function(image, versions) {
module.exports.cmd = function(image, output) {
var cmd = [
sprintf('convert %s -strip -write mpr:%s +delete', image.path, image.path)
];

for (var i = 0; i < versions.length; i++) {
cmd.push(module.exports.cmdVersion(image, versions[i], i === versions.length-1));
for (var i = 0; i < output.versions.length; i++) {
var version = output.versions[i];
var last = (i === output.versions.length-1);

version.prefix = version.prefix || output.prefix || '';
version.suffix = version.suffix || '';

cmd.push(module.exports.cmdVersion(image, version, last));
}

return cmd.join(' ');
Expand Down Expand Up @@ -118,7 +124,8 @@ module.exports.cmdVersion = function(image, version, last) {

// -write
version.path = module.exports.path(image.path, {
suffix: version.suffix || '',
prefix: version.prefix,
suffix: version.suffix,
format: version.format
});

Expand Down
45 changes: 41 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,40 @@ var resize = require('./index');

describe('resize.path()', function() {
it('returns new relative path with suffix', function() {
var path = resize.path('./foo.jpg', {suffix: '-bar'});
var path = resize.path('./foo.jpg', {prefix: '', suffix: '-bar'});

assert.equal(path, 'foo-bar.jpg');
});

it('returns new relative path with custom format', function() {
var path = resize.path('./foo.jpg', {suffix: '-bar', format: 'png'});
var path = resize.path('./foo.jpg', {
prefix: '',
suffix: '-bar',
format: 'png'
});

assert.equal(path, 'foo-bar.png');
});

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

it('returns new absolute path with custom format', function() {
var path = resize.path('/foo/bar/baz.jpg', {suffix: '-bix', format: 'png'});
var path = resize.path('/foo/bar/baz.jpg', {
prefix: '',
suffix: '-bix',
format: 'png'
});

assert.equal(path, '/foo/bar/baz-bix.png');
});

it('returns new path with prefix', function() {
var path = resize.path('/foo/bar/baz.jpg', {prefix: 'prefix-', suffix: ''});
assert.equal(path, '/foo/bar/prefix-baz.jpg');
});
});

describe('resize.crop()', function() {
Expand Down Expand Up @@ -50,6 +66,26 @@ describe('resize.crop()', function() {
describe('resize.cmd()', function() { });

describe('resize.cmdVersion()', function() {
it('returns convert command for version', function() {
var image = {
path: './a.jpg',
width: 2000,
height: 1000
};

var version = {
prefix: '',
suffix: '-b',
maxWidth: 500,
maxHeight: 500
};

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

assert.equal(cmd, out);
});

it('sets custom quality if specified', function() {
var image = {
path: './a.jpg',
Expand All @@ -58,6 +94,7 @@ describe('resize.cmdVersion()', function() {
};

var version = {
prefix: '',
suffix: '-b',
quality: 50,
maxWidth: 500,
Expand Down

0 comments on commit d911bb5

Please sign in to comment.