hostr/lib/resize.js

34 lines
787 B
JavaScript
Raw Normal View History

2015-07-09 23:01:43 +01:00
import debugname from 'debug';
const debug = debugname('hostr-api:resize');
2016-05-24 21:33:09 +01:00
import lwip from 'lwip';
import imageType from 'image-type';
const supported = ['jpg', 'png', 'gif'];
2015-07-09 23:01:43 +01:00
export default function(input, size) {
debug('Resizing');
2016-05-24 21:33:09 +01:00
const type = imageType(input);
if (!type.ext || supported.indexOf(type.ext) < 0) {
throw new Error('Not a supported image.');
}
return new Promise((resolve, reject) => {
lwip.open(input, type.ext, (errIn, image) => {
if (errIn) {
return reject(errIn);
}
image.cover(size.width, size.height, (errOut, resized) => {
if (errOut) {
return reject(errOut);
}
resized.toBuffer(type.ext, (errBuf, buffer) => {
resolve(buffer);
});
});
});
});
2015-07-09 23:01:43 +01:00
}