Comparing version 0.9.6 to 0.9.7
@@ -13,4 +13,7 @@ /** | ||
var Bucks = require('bucks'); | ||
var logger = require('../logger'); | ||
var constant = require('../constant'); | ||
var fsys = require('../fsys'); | ||
@@ -20,3 +23,6 @@ var imagemagick = require('./imagemagick'); | ||
var DEFAULT_OPTIONS = constant.IMAGE_RATIORESIZE_DEFAULT_OPTIONS; | ||
var REG_OPTIPNG_FILE = constant.REG_OPTIPNG_FILE; | ||
var REG_JPEGOPTIM_FILE = constant.REG_JPEGOPTIM_FILE; | ||
/** | ||
@@ -39,3 +45,3 @@ * @name image | ||
* @name optipng | ||
* @memberof imagemagick | ||
* @memberof image | ||
* @method | ||
@@ -77,3 +83,3 @@ * @param {String} filepath PING file path. | ||
* @name jpegoptim | ||
* @memberof imagemagick | ||
* @memberof image | ||
* @method | ||
@@ -148,3 +154,115 @@ * @param {String} filepath JPEG file path. | ||
})); | ||
}, | ||
/** | ||
* run the optipng and jpegoptim | ||
* | ||
* @name optim | ||
* @memberof image | ||
* @method | ||
* @param {String} path JPEG|PNG file path. | ||
* @param {String} options options. | ||
* @param {function} callback | ||
* @example | ||
* var filepath = './test/image/logo.jpg'; | ||
* var options = { | ||
* optipng: { use: true, options: "-o 2"}, | ||
* jpegoptim: { use: true, options: "--strip-all"} | ||
* } | ||
*/ | ||
optim: function (path, options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = undefined; | ||
} | ||
options = options || {}; | ||
options.optipng = options.optipng || {use: true}; | ||
options.jpegoptim = options.jpegoptim || {use: true}; | ||
if (REG_OPTIPNG_FILE.test(path) && options.optipng.use) { // optipng | ||
this.optipng(path, options.optipng.level, function(err) { | ||
if (err) { | ||
logger.error('optipng error.', err); | ||
callback && callback(err); | ||
return; | ||
} | ||
logger.message('optipng:', path); | ||
callback && callback(null, path); | ||
}); | ||
} else if (REG_JPEGOPTIM_FILE.test(path) && options.jpegoptim) { // jpegoptim | ||
this.jpegoptim(path, options.jpegoptim.options, function(err) { | ||
if (err) { | ||
logger.error('jpegoptim error.', err); | ||
callback && callback(err); | ||
return; | ||
} | ||
logger.message('jpegoptim:', path); | ||
callback && callback(null, path); | ||
}); | ||
} else { | ||
logger.info('no match extension. path: ' + path); | ||
callback && callback(null, path); | ||
} | ||
}, | ||
/** | ||
* run the recursive directory of optipng and jpegoptim | ||
* | ||
* @name optimdir | ||
* @memberof image | ||
* @method | ||
* @param {String} dirpath directory root path | ||
* @param {String} options options. | ||
* @param {function} callback | ||
* @example | ||
* var filepath = './test/image/logo.jpg'; | ||
* var options = { | ||
* optipng: { use: true, options: "-o 2"}, | ||
* jpegoptim: { use: true, options: "--strip-all"} | ||
* } | ||
*/ | ||
optimdir: function (dirpath, options, callback) { | ||
var self = this; | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = undefined; | ||
} | ||
if (!fsys.isDirectorySync(dirpath)) { | ||
logger.error('not a directory. path:', dirpath); | ||
callback && callback(new Error('not a directory. path: ' + dirpath)); | ||
return; | ||
} | ||
var bucks = new Bucks(); | ||
bucks.empty(); | ||
var tasks = []; | ||
fsys.walk(dirpath, function filefn(prefix, dir, file, stats) { | ||
var src = path.join(dir, file); | ||
tasks.push(function (err, res, next) { | ||
self.optim(src, options, next); | ||
}); | ||
}); | ||
bucks.parallel(tasks); | ||
bucks.end(function(err, ress) { | ||
callback && callback(err, ress); | ||
}); | ||
} | ||
}; |
{ | ||
"name": "beezlib", | ||
"version": "0.9.6", | ||
"version": "0.9.7", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -19,3 +19,3 @@ beezlib | ||
- [ImageMagick](http://www.imagemagick.org/script/index.php) | ||
- [Underscore.js](http://underscorejs.org/) | ||
- [Lo-Dash](http://lodash.com/) | ||
- [mkdirp](https://github.com/substack/node-mkdirp) | ||
@@ -67,3 +67,3 @@ - [suns.js](https://github.com/CyberAgent/suns.js) | ||
- obj | ||
- cp -rf フォルダ内を再帰的にコピー | ||
- オブジェクトのコピー | ||
- template | ||
@@ -70,0 +70,0 @@ - handlebars |
@@ -5,4 +5,7 @@ var path = require('path'); | ||
var _ = require('lodash'); | ||
beezlib.logger.level = 1; | ||
describe('beezlib.image', function () { | ||
it('getSize/resize', function (done) { | ||
@@ -125,2 +128,92 @@ var src = 'test/image/logo.png'; | ||
it('optim normal', function(done) { | ||
beezlib.image.optim('test/image/logo.jpg', function(err, res) { | ||
res.should.equal("test/image/logo.jpg").be.ok; | ||
should.not.exist(err); | ||
beezlib.image.optim('test/image/logo.png', function(err1, res1) { | ||
res1.should.equal("test/image/logo.png").be.ok; | ||
should.not.exist(err1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('optim set options', function(done) { | ||
var options = { | ||
optipng: { | ||
use: true, | ||
level: 3 | ||
}, | ||
jpegoptim: { | ||
use: true, | ||
options: '--strip-all' | ||
} | ||
}; | ||
beezlib.image.optim('test/image/logo.jpg', options, function(err, res) { | ||
res.should.equal("test/image/logo.jpg").be.ok; | ||
should.not.exist(err); | ||
beezlib.image.optim('test/image/logo.png', options, function(err1, res1) { | ||
res1.should.equal("test/image/logo.png").be.ok; | ||
should.not.exist(err1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('optim set error', function(done) { | ||
var options = { | ||
optipng: { | ||
use: true, | ||
level: 3 | ||
}, | ||
jpegoptim: { | ||
use: true, | ||
options: '--strip-all' | ||
} | ||
}; | ||
beezlib.image.optim('test/image/logo.error.jpg', options, function(err, res) { | ||
should.exist(err); | ||
beezlib.image.optim('test/image/logo.error.png', options, function(err1, res1) { | ||
should.exist(err1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('optimdir', function(done) { | ||
var options = { | ||
optipng: { | ||
use: true, | ||
level: 3 | ||
}, | ||
jpegoptim: { | ||
use: true, | ||
options: '--strip-all' | ||
} | ||
}; | ||
beezlib.image.optimdir('test/image', options, function(err, ress) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
398694
3281