Create thumbnails from image using max width/height, optionnaly saving it to disk.
$ npm install lwip-image-thumbnails-creator --save
Single thumbnail:
var thumbnailsCreator = require('lwip-image-thumbnails-creator');
var options = { outputbasepath: '/output/thumbnail.jpg'};
return thumbnailsCreator.createThumbnail('image.jpg', {
maxWidth: 100,
maxHeight: 50
}, options).then(function (res) {
// ok
console.log(res.thumbnail);
/* output:
{
image: [Object],
width: 100,
height: 40,
maxWidth: 100,
maxHeight: 50,
outputpath: '/output/thumbnail-w150-h100.jpg'
}
*/
}, function (err) {
// unexpected error
});
Multiple thumbnails:
return thumbnailsCreator.createThumbnail('image.jpg', [{
maxWidth: 100,
maxHeight: 50
},{
maxWidth: 200,
maxHeight: 100
}], options).then(function (res) {
// ok
console.log(res.thumbnails);
/* output:
[ {
image: [Object],
width: 100,
height: 40,
maxWidth: 100,
maxHeight: 50,
outputpath: '/output/thumbnail-w100-h40.jpg'
}, ...]
*/
}, function (err) {
// unexpected error
});
You can also have more control about what append with the thumbnail buffer by setting the saveToDisk to false, then using directly the lwip image object:
var options = {
saveToDisk: false
};
var inputpath = path.resolve(__dirname, './images/original-image.jpg');
thumbnailsCreator.createThumbnail(inputpath, {
maxWidth: 600,
maxHeight: 300
}, options).then(function (res) {
var outputpath = path.resolve(__dirname, './images/output/thumbnail-exact-name.jpg');
res.thumbnail.image.writeFile(outputpath, function(err){
// if (err) ...
});
Simple log messages are managed using debugging utility
To display them, set env variable:
DEBUG=litc.*
Tests are run by Mocha test framework and Chai assertion library.
Test images are provided by EXIF Orientation-flag example images under MIT licence.
To run tests + coverage:
npm test
or
gulp
To run tests only
gulp test
To run tests in watch mode:
gulp test-auto
To enable logs:
DEBUG=litc.* gulp test
Project generated with Yeoman Node Generator.
Apache-2.0 © Nicolas Toublanc